feat(api): export cog_suggestions() (#75)
cog_suggestions(govid, years, category, flow, concept, years_present) returns the list that cog_spending() and cog_revenue() attach as attr(x, "provenance")$suggestions, given the years the caller's rows cover. A caller can run a verb once with no category, filter the rows itself, and build the suggestions for a category only when it needs them, without reaching into .build_suggestions() and six other internal names. It validates every argument before opening a session, with cli errors whose classes match the verbs' where one exists (uscogdata_invalid_expenditure_concept, uscogdata_invalid_revenue_concept, uscogdata_all_categories_not_combinable) and a new uscogdata_invalid_flow. It resolves the verbs' default basis the way .verb_spendrev() does, so it returns an empty list on a schema_version < 5 corpus, and it does not print the suggestion message. Tests check that it returns exactly the verbs' list for empty_year and suppressed_component fires, a multi-government cohort, a data-frame govid, both revenue concepts, "All Categories" under each concept, and expenditure_concept = "total" given Direct-leg years. Closes #75 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qbe6u2h83wgZdTgJgE4Xh5
This commit is contained in:
6 files changed
+575
-5
No files matched your search
@@ -0,0 +1,97 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/suggestions.R
|
||||
\name{cog_suggestions}
|
||||
\alias{cog_suggestions}
|
||||
\title{Coverage suggestions for a spending or revenue query}
|
||||
\usage{
|
||||
cog_suggestions(
|
||||
govid,
|
||||
years,
|
||||
category,
|
||||
flow = c("spending", "revenue"),
|
||||
concept = NULL,
|
||||
years_present
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
\item{govid}{Character vector of `canonical_govid` values, or a data frame
|
||||
with a `canonical_govid` column, such as a [cog_find_peers()] or
|
||||
[cog_gov_search()] result. The suggestions cover these governments as one
|
||||
cohort, as they do in a verb call over the same `govid`.}
|
||||
|
||||
\item{years}{Integer vector of requested years.}
|
||||
|
||||
\item{category}{Character vector of category names from
|
||||
`summary_categories.category`, or the reserved `"All Categories"` alone.
|
||||
The verbs build suggestions only when a category is named, so `NULL` is
|
||||
refused here.}
|
||||
|
||||
\item{flow}{`"spending"` for the suggestions [cog_spending()] would attach,
|
||||
or `"revenue"` for [cog_revenue()]'s.}
|
||||
|
||||
\item{concept}{The verb's concept argument: `expenditure_concept` for
|
||||
spending (`"primary"`, `"direct"` or `"total"`) or `revenue_concept` for
|
||||
revenue (`"general"` or `"total"`). `NULL`, the default, uses the verb's
|
||||
own default, `"primary"` or `"general"`. The concept changes the result
|
||||
only when `category = "All Categories"`, where its subtype scope decides
|
||||
which recipes are candidates.}
|
||||
|
||||
\item{years_present}{Integer vector of the years in which your rows for
|
||||
`category` have at least one row. Pass `integer(0)` when there are no
|
||||
rows. Years outside `years` are ignored. With `flow = "spending"` and
|
||||
`concept = "total"`, count only rows whose `spend_subtype` is not
|
||||
`"intergovernmental"`. [cog_spending()] measures gaps on the Direct leg
|
||||
alone, because an intergovernmental row can survive in a year whose
|
||||
Direct rows Census published only as an aggregate.}
|
||||
}
|
||||
\value{
|
||||
A list with one element per suggestion, ordered by `recipe_id`,
|
||||
and identical to `attr(x, "provenance")$suggestions` for the same
|
||||
inputs. Each element is a list with `recipe_id`, `label`,
|
||||
`available_years` (first and last year the recipe covers), `hint`,
|
||||
`trigger` (`"empty_year"` or `"suppressed_component"`),
|
||||
`suppressed_amount` (full US dollars), `suppressed_years`,
|
||||
`suppressed_codes`, and `ig_recipe_id` (the intergovernmental
|
||||
counterpart recipe, or `NULL`). The list is empty when nothing qualifies.
|
||||
}
|
||||
\description{
|
||||
Builds the harmonization-recipe suggestions that [cog_spending()] and
|
||||
[cog_revenue()] attach as `attr(x, "provenance")$suggestions`, without
|
||||
running the verb again. Call a verb once with no `category`, filter its
|
||||
rows yourself, and call this when you need the suggestions for one
|
||||
category.
|
||||
}
|
||||
\details{
|
||||
A suggestion names a recipe (see [cog_recipes()]) that would fill a gap in
|
||||
the result. A recipe qualifies in two ways. Its `trigger` is
|
||||
`"empty_year"` when the result has no rows in a requested year and the
|
||||
recipe's own join finds rows for these governments in that year. Its
|
||||
`trigger` is `"suppressed_component"` when one of the recipe's component
|
||||
codes carries dollars that the verb's own view excludes, because Census
|
||||
published them only as an aggregate or because the code is absent from
|
||||
`summary_categories`.
|
||||
|
||||
Suggestions are built only for the verbs' default `basis = "harmonized"`.
|
||||
On a corpus with `schema_version < 5` that basis is unavailable, the verbs
|
||||
fall back to `"raw"`, and this function returns an empty list, as the
|
||||
verbs do. It also returns an empty list when nothing qualifies.
|
||||
|
||||
The verbs also print the suggestions as a message. This function returns
|
||||
them without printing.
|
||||
}
|
||||
\examples{
|
||||
\dontrun{
|
||||
govids <- c("061037123085", "121011212191")
|
||||
spend <- cog_spending(govids, years = 2011:2012)
|
||||
|
||||
corrections <- spend[spend$category == "Corrections", ]
|
||||
cog_suggestions(
|
||||
govids, years = 2011:2012, category = "Corrections",
|
||||
flow = "spending", years_present = unique(corrections$year)
|
||||
)
|
||||
}
|
||||
}
|
||||
\seealso{
|
||||
[cog_spending()], [cog_revenue()], [cog_recipes()],
|
||||
[cog_explain()]
|
||||
}
|
||||
Reference in new issue
Block a user