feat: merge fiscal-year periods from GitHub pull request 4
Refs #77. The cog-api step of that issue remains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
17 files changed
+1077
-17
No files matched your search
+14
-3
@@ -53,6 +53,11 @@
|
||||
#' `total_rows`.
|
||||
#' @param offset Rows to skip before `limit` starts counting (0-based).
|
||||
#' Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.
|
||||
#' @param fiscal_period If `TRUE`, add `fiscal_year_end`, `period_start` and
|
||||
#' `period_end` to every row, as [cog_spending()] does. Holdings are a stock
|
||||
#' measured at the end of the fiscal year, so `period_end` is the date the
|
||||
#' balance describes and `period_start` is the start of that fiscal year.
|
||||
#' Defaults to `FALSE`, which leaves the result unchanged.
|
||||
#'
|
||||
#' @return Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
#' `balance_subtype`, `category`, `amt_nominal`, `codes_included`,
|
||||
@@ -60,8 +65,9 @@
|
||||
#' `pop_source` (when `per_capita = TRUE`), optional `amt_real` (when
|
||||
#' `adjust_to_year` is set), and optional `amt_per_capita_real` (only when
|
||||
#' **both** `per_capita = TRUE` and `adjust_to_year` are set -- there is no
|
||||
#' nominal per-capita column to deflate otherwise). Amounts are full US
|
||||
#' dollars.
|
||||
#' nominal per-capita column to deflate otherwise), and `fiscal_year_end`,
|
||||
#' `period_start` and `period_end` when `fiscal_period = TRUE`. Amounts are
|
||||
#' full US dollars.
|
||||
#'
|
||||
#' Carries a `provenance` attribute matching
|
||||
#' `inst/schemas/provenance-v1.json`, whose `balance_caveats` block reports
|
||||
@@ -79,7 +85,7 @@ cog_balances <- function(govid = NULL, years, category = NULL,
|
||||
per_capita = FALSE, adjust_to_year = NULL,
|
||||
basis = c("harmonized", "raw"), recipe = NULL,
|
||||
state = NULL, type = NULL,
|
||||
limit = NULL, offset = NULL) {
|
||||
limit = NULL, offset = NULL, fiscal_period = FALSE) {
|
||||
call <- match.call()
|
||||
basis <- match.arg(basis, c("harmonized", "raw"))
|
||||
# Coerce FIRST, validate second: .validate_verb_inputs() asserts
|
||||
@@ -102,6 +108,7 @@ cog_balances <- function(govid = NULL, years, category = NULL,
|
||||
# validator's own doc comment for the incident that made that matter.
|
||||
.validate_verb_inputs(govid, years, category, per_capita, adjust_to_year,
|
||||
recipe)
|
||||
.validate_fiscal_period_arg(fiscal_period)
|
||||
|
||||
# Same semantics as the money verbs (R/pagination.R). Only the `recipe`
|
||||
# conflict applies here: cog_balances() has no `complete` argument, and a
|
||||
@@ -174,6 +181,7 @@ cog_balances <- function(govid = NULL, years, category = NULL,
|
||||
if (!is.null(adjust_to_year)) {
|
||||
result <- .attach_real_dollars(result, adjust_to_year, per_capita)
|
||||
}
|
||||
if (fiscal_period) result <- .attach_fiscal_period(result, con)
|
||||
|
||||
prov <- .build_provenance(
|
||||
verb = "cog_balances", call = call, govid = govid, years = years,
|
||||
@@ -189,6 +197,9 @@ cog_balances <- function(govid = NULL, years, category = NULL,
|
||||
prov$scope$govids_found <- scope$found
|
||||
prov$scope$govids_missing <- scope$missing
|
||||
prov$scope$cohort <- .cohort_provenance(con, cohort)
|
||||
if (fiscal_period) {
|
||||
prov$transformations$fiscal_period <- .fiscal_period_provenance(result)
|
||||
}
|
||||
|
||||
prov$balance_caveats <- .balance_caveats(
|
||||
con, prov$codes_summed$observed, years
|
||||
|
||||
+377
@@ -0,0 +1,377 @@
|
||||
# R/fiscal.R
|
||||
#
|
||||
# Fiscal-year timing (uscogdata#77). The corpus's `year` is the Census SURVEY
|
||||
# year, and one survey year covers different calendar months for different
|
||||
# governments. Census's rule (Local Government Finance FAQ, Q6): survey year Y
|
||||
# holds each government's fiscal year that ENDED between July 1 of Y-1 and
|
||||
# June 30 of Y. `long.fiscal_year_end` (an MMDD integer such as 630 or 1231)
|
||||
# says which months that is for a given government-year.
|
||||
#
|
||||
# Two kinds of government follow a different rule: survey year Y holds the
|
||||
# fiscal year ending in calendar year Y, even when that is after June 30.
|
||||
#
|
||||
# * State governments (type 0). Census's 2004 and 2006 technical
|
||||
# documentation names the Alabama and Michigan (September 30) and Texas
|
||||
# (August 31) state governments as exceptions "included as though they
|
||||
# were part of the June 30 group". The published corpus agrees for survey
|
||||
# year 2020: those three states' federal aid jumps with the CARES Act in
|
||||
# 2020, as the June-30 states' does, while New York (March 31) and 15
|
||||
# large September-30 cities and counties jump in 2021 (measurements in
|
||||
# uscogdata#77). For the other 47 states both rules give the same months.
|
||||
# * The District of Columbia (type 2 here), which the same documentation
|
||||
# lists with the September-30 exceptions. The 2020 data neither confirms
|
||||
# nor rules this out, so it rests on the documentation alone.
|
||||
|
||||
# canonical_govids that follow the state-government rule without being type 0.
|
||||
.SURVEY_YEAR_SAME_YEAR_GOVIDS <- c(
|
||||
"112001124214" # Washington, D.C.
|
||||
)
|
||||
|
||||
#' Days in a month, vectorized over year and month.
|
||||
#' @noRd
|
||||
.days_in_month <- function(year, month) {
|
||||
dim <- c(31L, 28L, 31L, 30L, 31L, 30L, 31L, 31L, 30L, 31L, 30L, 31L)
|
||||
leap <- (year %% 4L == 0L & year %% 100L != 0L) | year %% 400L == 0L
|
||||
out <- dim[month]
|
||||
out[month == 2L & leap] <- 29L
|
||||
out
|
||||
}
|
||||
|
||||
#' Build Dates from year/month/day, clamping the day to the month's length
|
||||
#' (so a February 29 year end falls on February 28 in a common year).
|
||||
#' @noRd
|
||||
.make_date <- function(year, month, day) {
|
||||
out <- rep(as.Date(NA), length(year))
|
||||
ok <- !is.na(year) & !is.na(month) & !is.na(day)
|
||||
if (any(ok)) {
|
||||
d <- pmin(day[ok], .days_in_month(year[ok], month[ok]))
|
||||
out[ok] <- as.Date(sprintf("%04d-%02d-%02d", year[ok], month[ok], d))
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#' TRUE where `fiscal_year_end` is a usable MMDD value.
|
||||
#' @noRd
|
||||
.valid_fiscal_year_end <- function(fye) {
|
||||
fye <- as.integer(fye)
|
||||
m <- fye %/% 100L
|
||||
d <- fye %% 100L
|
||||
ok <- !is.na(fye) & !is.na(m) & m >= 1L & m <= 12L & d >= 1L
|
||||
ok[ok] <- d[ok] <= .days_in_month(2000L, m[ok]) # 2000 is a leap year
|
||||
ok
|
||||
}
|
||||
|
||||
#' The fiscal period a survey year covers for one government-year.
|
||||
#'
|
||||
#' Vectorized over its arguments, which must have equal length.
|
||||
#'
|
||||
#' @param year Integer survey year (the corpus's `year`).
|
||||
#' @param fiscal_year_end Integer MMDD from `long.fiscal_year_end`.
|
||||
#' @param gov_type Integer government type (0 = state).
|
||||
#' @param canonical_govid Character canonical id.
|
||||
#' @return List of Date vectors `period_start` and `period_end`; `NA` where
|
||||
#' `fiscal_year_end` is missing or not a valid MMDD.
|
||||
#' @noRd
|
||||
.survey_period <- function(year, fiscal_year_end, gov_type, canonical_govid) {
|
||||
year <- as.integer(year)
|
||||
fye <- as.integer(fiscal_year_end)
|
||||
ok <- .valid_fiscal_year_end(fye)
|
||||
month <- fye %/% 100L
|
||||
day <- fye %% 100L
|
||||
|
||||
same_year <- (!is.na(gov_type) & as.integer(gov_type) == 0L) |
|
||||
canonical_govid %in% .SURVEY_YEAR_SAME_YEAR_GOVIDS
|
||||
end_year <- ifelse(same_year | month <= 6L, year, year - 1L)
|
||||
|
||||
month[!ok] <- NA_integer_
|
||||
# The period starts the day after the same month-day one year earlier,
|
||||
# which is when the government's previous fiscal year ended.
|
||||
list(
|
||||
period_start = .make_date(end_year - 1L, month, day) + 1L,
|
||||
period_end = .make_date(end_year, month, day)
|
||||
)
|
||||
}
|
||||
|
||||
#' Look up each government-year's fiscal_year_end and type in `long`.
|
||||
#'
|
||||
#' A government-year whose rows carry more than one distinct value gets `NA`
|
||||
#' rather than an arbitrary pick. None does in the fixture's four years.
|
||||
#'
|
||||
#' @return Tibble with `canonical_govid`, `year` (integer), `gov_type`
|
||||
#' (integer) and `fiscal_year_end` (integer), one row per government-year
|
||||
#' found in the corpus.
|
||||
#' @noRd
|
||||
.fiscal_year_lookup <- function(con, govids, years) {
|
||||
govids <- unique(govids[!is.na(govids) & nzchar(govids)])
|
||||
years <- unique(as.integer(years[!is.na(years)]))
|
||||
if (length(govids) == 0L || length(years) == 0L) {
|
||||
return(tibble::tibble(canonical_govid = character(0), year = integer(0),
|
||||
gov_type = integer(0),
|
||||
fiscal_year_end = integer(0)))
|
||||
}
|
||||
out <- tibble::as_tibble(DBI::dbGetQuery(con, sprintf(
|
||||
"SELECT canonical_govid, year,
|
||||
MIN(type) AS gov_type,
|
||||
CASE WHEN COUNT(DISTINCT fiscal_year_end) = 1
|
||||
THEN MAX(fiscal_year_end) END AS fiscal_year_end
|
||||
FROM long
|
||||
WHERE canonical_govid IN (%s) AND year IN (%s)
|
||||
GROUP BY canonical_govid, year",
|
||||
.sql_lit_chr(govids), paste(years, collapse = ",")
|
||||
)))
|
||||
out$year <- as.integer(out$year)
|
||||
out$gov_type <- as.integer(out$gov_type)
|
||||
out$fiscal_year_end <- as.integer(out$fiscal_year_end)
|
||||
out
|
||||
}
|
||||
|
||||
#' Fiscal-year columns for every (canonical_govid, year) pair, in input order.
|
||||
#' @return Tibble with `fiscal_year_end`, `period_start`, `period_end`.
|
||||
#' @noRd
|
||||
.fiscal_periods_for <- function(con, govids, years) {
|
||||
years <- as.integer(years)
|
||||
fy <- .fiscal_year_lookup(con, govids, years)
|
||||
idx <- match(paste(govids, years), paste(fy$canonical_govid, fy$year))
|
||||
fye <- fy$fiscal_year_end[idx]
|
||||
per <- .survey_period(years, fye, fy$gov_type[idx], govids)
|
||||
tibble::tibble(
|
||||
fiscal_year_end = ifelse(.valid_fiscal_year_end(fye), fye, NA_integer_),
|
||||
period_start = per$period_start,
|
||||
period_end = per$period_end
|
||||
)
|
||||
}
|
||||
|
||||
#' Append `fiscal_year_end`, `period_start` and `period_end` to a verb result.
|
||||
#'
|
||||
#' Joined by position rather than with `dplyr::left_join()`, so row order and
|
||||
#' the result's attributes are untouched. Rows with no `canonical_govid`
|
||||
#' (none today) get `NA`.
|
||||
#' @noRd
|
||||
.attach_fiscal_period <- function(result, con) {
|
||||
if (nrow(result) == 0L) {
|
||||
result$fiscal_year_end <- integer(0)
|
||||
result$period_start <- as.Date(character(0))
|
||||
result$period_end <- as.Date(character(0))
|
||||
return(result)
|
||||
}
|
||||
fp <- .fiscal_periods_for(con, as.character(result$canonical_govid),
|
||||
result$year)
|
||||
result$fiscal_year_end <- fp$fiscal_year_end
|
||||
result$period_start <- fp$period_start
|
||||
result$period_end <- fp$period_end
|
||||
result
|
||||
}
|
||||
|
||||
#' Provenance block recorded when a verb is called with `fiscal_period = TRUE`.
|
||||
#' Absent otherwise, so default output keeps its existing shape.
|
||||
#' @noRd
|
||||
.fiscal_period_provenance <- function(result) {
|
||||
list(
|
||||
applied = TRUE,
|
||||
source = "long.fiscal_year_end (MMDD), per government-year",
|
||||
rule = paste(
|
||||
"Survey year Y covers the fiscal year that ended between July 1 of Y-1",
|
||||
"and June 30 of Y; state governments and the District of Columbia",
|
||||
"cover the fiscal year ending in calendar year Y."
|
||||
),
|
||||
rows_without_period = sum(is.na(result$period_end))
|
||||
)
|
||||
}
|
||||
|
||||
#' @noRd
|
||||
.validate_fiscal_period_arg <- function(fiscal_period) {
|
||||
if (!is.logical(fiscal_period) || length(fiscal_period) != 1L ||
|
||||
is.na(fiscal_period)) {
|
||||
cli::cli_abort("{.arg fiscal_period} must be {.code TRUE} or {.code FALSE}.")
|
||||
}
|
||||
invisible(fiscal_period)
|
||||
}
|
||||
|
||||
#' Fiscal-year periods behind survey years
|
||||
#'
|
||||
#' Returns the calendar months each survey year covers for each government.
|
||||
#' The corpus's `year` is the Census survey year, and a survey year covers
|
||||
#' different months for different governments.
|
||||
#'
|
||||
#' @section The survey-year rule:
|
||||
#' Census assigns each government's fiscal year to a survey year by when the
|
||||
#' fiscal year ended. Survey year Y holds the fiscal year that ended between
|
||||
#' July 1 of Y-1 and June 30 of Y. The corpus records each government-year's
|
||||
#' fiscal year end as `fiscal_year_end`, an MMDD integer such as `630` or
|
||||
#' `1231`.
|
||||
#'
|
||||
#' Survey year 2017 therefore covers, for a county, city or township:
|
||||
#'
|
||||
#' * `fiscal_year_end = 630` (June 30): the fiscal year ending June 30,
|
||||
#' 2017, so July 2016 to June 2017.
|
||||
#' * `930` (September 30): the fiscal year ending September 30, 2016, so
|
||||
#' October 2015 to September 2016.
|
||||
#' * `1231` (December 31): the fiscal year ending December 31, 2016, so
|
||||
#' January to December 2016.
|
||||
#'
|
||||
#' A city with a December year end and a city with a June year end are both
|
||||
#' "2017" in the corpus, but their figures are six months apart. A September
|
||||
#' year end is nine months behind a June one.
|
||||
#'
|
||||
#' State governments (type 0) follow a different rule: survey year Y holds the
|
||||
#' fiscal year ending in calendar year Y. For Alabama and Michigan (September
|
||||
#' 30) that means October 2016 to September 2017, and for Texas (August 31)
|
||||
#' September 2016 to August 2017. Census's 2004 and 2006 technical
|
||||
#' documentation lists these three states as exceptions to the June 30 window,
|
||||
#' and the corpus agrees in survey year 2020, the one year checked. The same
|
||||
#' documentation lists the District of Columbia, so it is treated the same
|
||||
#' way. That treatment has not been checked against the data. For New York
|
||||
#' (March 31) and the 46 states with a June 30 year end, the two rules give
|
||||
#' the same months.
|
||||
#'
|
||||
#' A government can change its fiscal year end, so the periods are looked up
|
||||
#' per government-year. Where the corpus has no `fiscal_year_end` for a
|
||||
#' government-year, or the government has no rows that year, the period is
|
||||
#' `NA`.
|
||||
#'
|
||||
#' From FY2023 the source files mark some rows as estimates carried forward
|
||||
#' from an earlier year. The periods here describe the survey year, not the
|
||||
#' year a carried-forward row's data pertain to.
|
||||
#'
|
||||
#' @param govid Character vector of `canonical_govid` values, or a data frame
|
||||
#' with a `canonical_govid` column.
|
||||
#' @param years Integer vector of survey years.
|
||||
#' @return Tibble with one row per requested government and year, in the
|
||||
#' order `govid` x `years`: `canonical_govid`, `year`, `fiscal_year_end`
|
||||
#' (integer MMDD), `period_start` and `period_end` (Dates, inclusive).
|
||||
#' @seealso [cog_survey_year()] for the reverse mapping; the `fiscal_period`
|
||||
#' argument of [cog_spending()], [cog_revenue()] and [cog_balances()] to get
|
||||
#' the same columns on every row.
|
||||
#' @examples
|
||||
#' \dontrun{
|
||||
#' # A June 30 state, a September 30 state and a September 30 county
|
||||
#' cog_fiscal_periods(
|
||||
#' c("130000226087", "010000226085", "121011212191"),
|
||||
#' years = 2020
|
||||
#' )
|
||||
#' }
|
||||
#' @export
|
||||
cog_fiscal_periods <- function(govid, years) {
|
||||
if (missing(govid)) cli::cli_abort("{.arg govid} is required.")
|
||||
if (missing(years)) cli::cli_abort("{.arg years} is required.")
|
||||
govid <- .coerce_govid_input(govid, arg = "govid")
|
||||
if (length(govid) == 0L || anyNA(govid) || !all(nzchar(govid))) {
|
||||
cli::cli_abort("{.arg govid} must name at least one government, with no missing or empty ids.")
|
||||
}
|
||||
if (!is.numeric(years) || length(years) == 0L || anyNA(years)) {
|
||||
cli::cli_abort("{.arg years} must be a non-empty integer vector with no missing values.")
|
||||
}
|
||||
govid <- unique(govid)
|
||||
years <- unique(as.integer(years))
|
||||
|
||||
con <- .ensure_session()
|
||||
grid <- expand.grid(year = years, canonical_govid = govid,
|
||||
stringsAsFactors = FALSE)
|
||||
fp <- .fiscal_periods_for(con, grid$canonical_govid, grid$year)
|
||||
tibble::tibble(
|
||||
canonical_govid = grid$canonical_govid,
|
||||
year = as.integer(grid$year),
|
||||
fiscal_year_end = fp$fiscal_year_end,
|
||||
period_start = fp$period_start,
|
||||
period_end = fp$period_end
|
||||
)
|
||||
}
|
||||
|
||||
#' Survey year that covers a calendar date for a government
|
||||
#'
|
||||
#' Maps a date to the survey year whose fiscal period, for that government,
|
||||
#' contains it. This is the reverse of [cog_fiscal_periods()]: use it to place
|
||||
#' an event that happened on a known date against the corpus's `year`.
|
||||
#'
|
||||
#' The date can fall in survey year Y, Y+1 or Y+2 of its own calendar year Y,
|
||||
#' depending on the government's fiscal year end. Each candidate is looked up
|
||||
#' with the `fiscal_year_end` the corpus records for that government-year, so
|
||||
#' a change of fiscal year is respected. When the government has no rows, or
|
||||
#' no `fiscal_year_end`, in the survey year that would cover the date, the
|
||||
#' result is `NA`; the function does not borrow another year's value. If a
|
||||
#' change of fiscal year makes two survey years' periods overlap, the earlier
|
||||
#' survey year is returned.
|
||||
#'
|
||||
#' @inheritSection cog_fiscal_periods The survey-year rule
|
||||
#' @param govid Character vector of `canonical_govid` values, or a data frame
|
||||
#' with a `canonical_govid` column.
|
||||
#' @param date Dates, or character strings in `"YYYY-MM-DD"` form. `govid`
|
||||
#' and `date` must have the same length, or one of them must have length 1.
|
||||
#' @return Tibble with one row per (`govid`, `date`) pair, in input order:
|
||||
#' `canonical_govid`, `date`, `year` (the survey year, `NA` when it cannot
|
||||
#' be determined), `fiscal_year_end`, `period_start` and `period_end`.
|
||||
#' @seealso [cog_fiscal_periods()]
|
||||
#' @examples
|
||||
#' \dontrun{
|
||||
#' # April 15, 2020 for a September 30 county and a September 30 state
|
||||
#' cog_survey_year(c("121011212191", "010000226085"), as.Date("2020-04-15"))
|
||||
#' }
|
||||
#' @export
|
||||
cog_survey_year <- function(govid, date) {
|
||||
if (missing(govid)) cli::cli_abort("{.arg govid} is required.")
|
||||
if (missing(date)) cli::cli_abort("{.arg date} is required.")
|
||||
govid <- .coerce_govid_input(govid, arg = "govid")
|
||||
if (length(govid) == 0L || anyNA(govid) || !all(nzchar(govid))) {
|
||||
cli::cli_abort("{.arg govid} must name at least one government, with no missing or empty ids.")
|
||||
}
|
||||
date <- .coerce_date_input(date)
|
||||
n <- max(length(govid), length(date))
|
||||
if (!length(govid) %in% c(1L, n) || !length(date) %in% c(1L, n)) {
|
||||
cli::cli_abort(c(
|
||||
"{.arg govid} and {.arg date} must have the same length, or one must have length 1.",
|
||||
"x" = "{.arg govid} has length {length(govid)}; {.arg date} has length {length(date)}."
|
||||
))
|
||||
}
|
||||
govid <- rep_len(govid, n)
|
||||
date <- rep_len(date, n)
|
||||
|
||||
con <- .ensure_session()
|
||||
cal_year <- as.integer(format(date, "%Y"))
|
||||
cand <- data.frame(
|
||||
i = rep(seq_len(n), each = 3L),
|
||||
canonical_govid = rep(govid, each = 3L),
|
||||
year = rep(cal_year, each = 3L) + rep(0:2, times = n),
|
||||
stringsAsFactors = FALSE
|
||||
)
|
||||
cand <- cand[!is.na(cand$year), , drop = FALSE]
|
||||
fp <- .fiscal_periods_for(con, cand$canonical_govid, cand$year)
|
||||
cand <- cbind(cand, fp)
|
||||
d <- date[cand$i]
|
||||
hit <- !is.na(cand$period_start) & d >= cand$period_start &
|
||||
d <= cand$period_end
|
||||
cand <- cand[hit, , drop = FALSE]
|
||||
cand <- cand[order(cand$i, cand$year), , drop = FALSE]
|
||||
first <- cand[!duplicated(cand$i), , drop = FALSE]
|
||||
idx <- match(seq_len(n), first$i)
|
||||
|
||||
tibble::tibble(
|
||||
canonical_govid = govid,
|
||||
date = date,
|
||||
year = as.integer(first$year[idx]),
|
||||
fiscal_year_end = first$fiscal_year_end[idx],
|
||||
period_start = first$period_start[idx],
|
||||
period_end = first$period_end[idx]
|
||||
)
|
||||
}
|
||||
|
||||
#' Coerce `date` to Date, refusing values that do not parse.
|
||||
#' @noRd
|
||||
.coerce_date_input <- function(date) {
|
||||
if (length(date) == 0L) cli::cli_abort("{.arg date} must not be empty.")
|
||||
if (inherits(date, "POSIXt")) date <- as.Date(date)
|
||||
if (!inherits(date, "Date")) {
|
||||
if (!is.character(date)) {
|
||||
cli::cli_abort("{.arg date} must be a Date or a character vector of dates.")
|
||||
}
|
||||
parsed <- as.Date(date, format = "%Y-%m-%d")
|
||||
bad <- !is.na(date) & is.na(parsed)
|
||||
if (any(bad)) {
|
||||
cli::cli_abort(c(
|
||||
"{.arg date} has values that are not dates.",
|
||||
"x" = "Could not parse: {.val {unique(date[bad])}}."
|
||||
))
|
||||
}
|
||||
date <- parsed
|
||||
}
|
||||
date
|
||||
}
|
||||
+5
-3
@@ -48,14 +48,15 @@
|
||||
#' `revenue_subtype`, `category`, `amt_nominal`, optional `amt_real`,
|
||||
#' optional `amt_per_capita_nominal`, optional `amt_per_capita_real`,
|
||||
#' optional `pop_source`, `codes_included`, `aggregate_fallback`, `notes`,
|
||||
#' and `value_source` when `complete = TRUE`.
|
||||
#' `value_source` when `complete = TRUE`, and `fiscal_year_end`,
|
||||
#' `period_start` and `period_end` when `fiscal_period = TRUE`.
|
||||
#' @export
|
||||
cog_revenue <- function(govid = NULL, years, category = NULL,
|
||||
per_capita = FALSE, adjust_to_year = NULL,
|
||||
basis = c("harmonized", "raw"), recipe = NULL,
|
||||
revenue_concept = c("general", "total"),
|
||||
complete = FALSE, limit = NULL, offset = NULL,
|
||||
state = NULL, type = NULL) {
|
||||
state = NULL, type = NULL, fiscal_period = FALSE) {
|
||||
# flow_prefixes no longer classifies rows (crosswalk revenue_subtype
|
||||
# membership does -- General Revenue, i.e. everything except
|
||||
# insurance_trust) -- it only scopes the recipe-suggestion machinery to
|
||||
@@ -78,6 +79,7 @@ cog_revenue <- function(govid = NULL, years, category = NULL,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
state = state,
|
||||
type = type
|
||||
type = type,
|
||||
fiscal_period = fiscal_period
|
||||
)
|
||||
}
|
||||
+25
-4
@@ -210,11 +210,23 @@
|
||||
#' and `govids_missing` are empty -- there is no id list to report against --
|
||||
#' and `provenance$scope$cohort` carries `state`, `type` and
|
||||
#' `n_governments` instead. A `govid`-named cohort reports exactly as before.
|
||||
#' @param fiscal_period If `TRUE`, add three columns to every row:
|
||||
#' `fiscal_year_end` (the government's fiscal year end that survey year, an
|
||||
#' MMDD integer such as `630`), and `period_start` and `period_end` (Dates,
|
||||
#' inclusive), the calendar months the row covers. `year` is the Census
|
||||
#' survey year, which covers different months for different governments;
|
||||
#' see "The survey-year rule" in [cog_fiscal_periods()]. The columns come
|
||||
#' from each government-year's own `fiscal_year_end`, so a change of fiscal
|
||||
#' year is respected, and are `NA` where the corpus has no value. The result
|
||||
#' also gains `provenance$transformations$fiscal_period`. Defaults to
|
||||
#' `FALSE`, which returns exactly the columns and provenance this verb
|
||||
#' returned before the argument existed.
|
||||
#' @return Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
#' `spend_subtype`, `category`, `amt_nominal`, optional `amt_real`,
|
||||
#' optional `amt_per_capita_nominal`, optional `amt_per_capita_real`,
|
||||
#' optional `pop_source`, `codes_included`, `aggregate_fallback`, `notes`,
|
||||
#' and `value_source` when `complete = TRUE`.
|
||||
#' `value_source` when `complete = TRUE`, and `fiscal_year_end`,
|
||||
#' `period_start` and `period_end` when `fiscal_period = TRUE`.
|
||||
#' Carries a `provenance` attribute matching `inst/schemas/provenance-v1.json`,
|
||||
#' whose `completion` block reports `applied`, `rows_filled`, and the
|
||||
#' per-year `absence_means` rule that was applied. When `limit` is set,
|
||||
@@ -228,7 +240,7 @@ cog_spending <- function(govid = NULL, years, category = NULL,
|
||||
basis = c("harmonized", "raw"), recipe = NULL,
|
||||
expenditure_concept = c("primary", "direct", "total"),
|
||||
complete = FALSE, limit = NULL, offset = NULL,
|
||||
state = NULL, type = NULL) {
|
||||
state = NULL, type = NULL, fiscal_period = FALSE) {
|
||||
# flow_prefixes no longer classifies rows (crosswalk subtype membership
|
||||
# does, per expenditure_concept) -- it only scopes the recipe-suggestion
|
||||
# machinery to this verb's recipe families (see R/suggestions.R; the
|
||||
@@ -251,7 +263,8 @@ cog_spending <- function(govid = NULL, years, category = NULL,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
state = state,
|
||||
type = type
|
||||
type = type,
|
||||
fiscal_period = fiscal_period
|
||||
)
|
||||
}
|
||||
|
||||
@@ -280,7 +293,7 @@ cog_spending <- function(govid = NULL, years, category = NULL,
|
||||
expenditure_concept = c("primary", "direct", "total"),
|
||||
revenue_concept = c("general", "total"),
|
||||
complete = FALSE, limit = NULL, offset = NULL,
|
||||
state = NULL, type = NULL) {
|
||||
state = NULL, type = NULL, fiscal_period = FALSE) {
|
||||
basis_explicit <- length(basis) == 1L
|
||||
basis <- match.arg(basis, c("harmonized", "raw"))
|
||||
# match.arg() itself throws a base `simpleError`, not an rlang-classed
|
||||
@@ -373,6 +386,7 @@ cog_spending <- function(govid = NULL, years, category = NULL,
|
||||
)
|
||||
}
|
||||
|
||||
.validate_fiscal_period_arg(fiscal_period)
|
||||
complete <- isTRUE(complete)
|
||||
if (complete && !is.null(recipe)) {
|
||||
.abort_complete_unsupported(
|
||||
@@ -572,6 +586,10 @@ cog_spending <- function(govid = NULL, years, category = NULL,
|
||||
|
||||
result$notes <- .notes_column(result, direct_suppressed_info$notes)
|
||||
|
||||
# Appended last, after every other column, so the opt-in columns never move
|
||||
# an existing one; FALSE (the default) leaves the result untouched.
|
||||
if (fiscal_period) result <- .attach_fiscal_period(result, con)
|
||||
|
||||
# Determine expenditure_concept_note: only non-empty for "total", explains
|
||||
# how the IG leg was assembled from legacy-era aggregates. When the Direct
|
||||
# leg is suppressed for at least one requested (year, category), append an
|
||||
@@ -636,6 +654,9 @@ cog_spending <- function(govid = NULL, years, category = NULL,
|
||||
# every fleet-scale response body, which is the cost this path exists to
|
||||
# remove. NULL for a govid-named cohort, so that output is untouched.
|
||||
prov$scope$cohort <- .cohort_provenance(con, cohort)
|
||||
if (fiscal_period) {
|
||||
prov$transformations$fiscal_period <- .fiscal_period_provenance(result)
|
||||
}
|
||||
attr(result, "provenance") <- prov
|
||||
attr(result, ".popyear_range") <- NULL
|
||||
# Attached here, after every downstream transform (per_capita/real-dollar
|
||||
|
||||
Reference in new issue
Block a user