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:
Claude committed 2026-11-04 14:19:09 +00:00
1 parent d74fb627c2
commit 2392808696
6 files changed
+575 -5

No files matched your search

+29 -4
View File
@@ -1,11 +1,36 @@
# uscogdata (development version)
## `cog_suggestions()` builds coverage suggestions without a second verb call
`cog_spending()` and `cog_revenue()` attach harmonization-recipe suggestions as
`attr(x, "provenance")$suggestions` when you name a category. `cog_suggestions()`
returns the same list for a set of governments, years, categories, a flow and a
concept, given the years your rows cover. You can call a verb once with no
`category`, filter its rows yourself, and build the suggestions for one category
only when you need them.
```r
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)
)
```
`cog_suggestions()` checks its arguments before it opens a session and raises
`cli` errors for a malformed call. It returns the list without printing the
message the verbs print.
## Suggestions come back in a stable order
`cog_spending()` and `cog_revenue()` now order suggestions by `recipe_id`. The query
that picks candidate recipes used `SELECT DISTINCT` with no `ORDER BY`, so DuckDB
returned them in whatever order its parallel aggregate finished, and two identical
calls could list the same suggestions in different orders. The query now sorts them.
`cog_spending()`, `cog_revenue()` and `cog_suggestions()` order suggestions by
`recipe_id`. The query that picks candidate recipes used `SELECT DISTINCT` with no
`ORDER BY`, so DuckDB returned them in whatever order its parallel aggregate
finished, and two identical calls could list the same suggestions in different
orders. The query now sorts them.
# uscogdata 0.4.0