fix: govid input ergonomics + clearer missing-govid message

Two UX fixes surfaced by first real-user use:

1. cog_spending / cog_revenue / cog_geographic_rollup now accept either
   a character vector OR a data.frame with a canonical_govid column
   (e.g. output of cog_gov_search() or cog_find_peers()). Shared
   .coerce_govid_input() helper in session.R. This lets the natural
   pipe work:

     cog_gov_search('MIAMI', state='FL', type='city') |>
       cog_spending(years=2022, category='Police')

   cog_peer_compare already accepted a data.frame for the peer arg;
   behavior there is unchanged.

2. .check_govids_in_scope() message reworded. The old text led with
   'v0.1 covers gov_types 0-3' which falsely implied the missing govids
   were scope-excluded types when the more common real cause is a typo
   or a guessed value. New message leads with typo + pre-2017 PID,
   mentions scope exclusion as one possibility, and points at
   cog_gov_search() as the recovery path.

Tests: 165 pass / 0 fail. check 0E/0W/0N.
This commit is contained in:
2026-04-24 17:49:08 -04:00
parent 377eed1240
commit a778d790d8
6 changed files with 71 additions and 9 deletions
+8 -5
View File
@@ -26,8 +26,14 @@
cog_geographic_rollup <- function(govids, category, years,
per_capita = FALSE, adjust_to_year = NULL) {
call <- match.call()
.validate_rollup_govids(govids)
.validate_rollup_layers(govids)
# Accept character vector OR a data.frame with canonical_govid per layer,
# so cog_gov_search() output can be piped into one of the layer slots.
govids <- lapply(govids, .coerce_govid_input, arg = "govids[[layer]]")
if (any(lengths(govids) == 0L)) {
cli::cli_abort("Each layer in `govids` must be non-empty after coercion.")
}
layer_names <- names(govids)
all_govids <- unlist(govids, use.names = FALSE)
layer_map <- tibble::tibble(
@@ -51,7 +57,7 @@ cog_geographic_rollup <- function(govids, category, years,
}
#' @noRd
.validate_rollup_govids <- function(govids) {
.validate_rollup_layers <- function(govids) {
if (!is.list(govids) || is.data.frame(govids)) {
cli::cli_abort("`govids` must be a named list.")
}
@@ -68,9 +74,6 @@ cog_geographic_rollup <- function(govids, category, years,
"`govids` names must be one of 'state', 'county', 'city'. Got: {bad}."
)
}
if (any(lengths(govids) == 0L)) {
cli::cli_abort("Each layer in `govids` must be non-empty.")
}
invisible(TRUE)
}