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
+27 -1
View File
@@ -33,6 +33,31 @@ cog_open <- function(url = .resolve_url(),
.uscogdata_env$con
}
#' @noRd
#' Coerce an input to a character vector of canonical_govid values.
#' Accepts either a character vector (returned as-is after `as.character`)
#' or a data.frame / tibble with a `canonical_govid` column (such as the
#' output of [cog_gov_search()] or [cog_find_peers()]) — in that case the
#' column is extracted so results from discovery verbs can pipe directly
#' into the query verbs.
.coerce_govid_input <- function(x, arg = "govid") {
if (is.data.frame(x)) {
if (!"canonical_govid" %in% names(x)) {
cli::cli_abort(c(
"`{arg}` data frame must have a `canonical_govid` column.",
i = "Use the result of cog_gov_search() or cog_find_peers() directly, or pass a character vector of canonical_govids."
))
}
return(as.character(x$canonical_govid))
}
if (!is.character(x) && !is.numeric(x)) {
cli::cli_abort(
"`{arg}` must be a character vector or a data frame with a `canonical_govid` column."
)
}
as.character(x)
}
#' @noRd
#' Check which of the supplied govids exist in canonical_fips_xwalk.
#' Emits a cli message listing any missing ones alongside a pointer to the
@@ -55,7 +80,8 @@ cog_open <- function(url = .resolve_url(),
cli::cli_inform(c(
i = sprintf("%d govid%s not found in v0.1 corpus: %s%s",
n, if (n == 1L) "" else "s", shown, more),
i = "v0.1 covers gov_types 0-3 (state/county/city/township). Types 4/5 excluded; see vignette('coverage-scope')."
i = "Common causes: typo, pre-2017 PID that isn't bridged, or a scope-excluded type (4=special district, 5=school district).",
i = "Resolve canonical names with cog_gov_search() first."
))
}
list(found = found, missing = missing)