% Generated by roxygen2: do not edit by hand % Please edit documentation in R/search.R \name{cog_gov_search} \alias{cog_gov_search} \title{Search for governments by name, state, and/or type} \usage{ cog_gov_search( name = NULL, state = NULL, type = NULL, limit = NULL, offset = NULL ) } \arguments{ \item{name}{Character vector of place name(s). Length 1 = utility mode; length >1 = basket mode.} \item{state}{2-letter USPS abbreviation (e.g. \code{"FL"}), FIPS integer (e.g. \code{12}), or \code{NULL}. In basket mode, length 1 recycles across all entries; otherwise must match \code{length(name)}.} \item{type}{Government type: integer in \code{0:3} or one of \code{"state"}, \code{"county"}, \code{"city"}, \code{"township"}, or \code{NA}/\code{NULL}. Per-row optional in basket mode (recycles from length 1). Excluded types \code{4}/\code{5} (or \code{"special_district"} / \code{"school_district"}) trigger an explanatory message and an empty result.} \item{limit}{Maximum number of rows to return, applied in SQL. \code{NULL} (default) returns every match -- which, with no other filter, is the entire crosswalk. Utility mode only: pagination has no meaning in basket mode, where the result is one resolved row per requested name in input order, and is refused there with class \code{uscogdata_basket_pagination_conflict}.} \item{offset}{Rows to skip before \code{limit} starts counting (0-based). Ignored if \code{limit} is \code{NULL}; defaults to \code{0L} when \code{limit} is set.} } \value{ A tibble of \code{canonical_fips_xwalk} rows. In utility mode, all matches sorted by \code{population_acs} desc, ties broken by \code{canonical_govid}. In basket mode, resolved rows in input order, with \code{attr(., "resolution")} set to the sidecar tibble. When \code{limit} is set, carries a \code{total_rows} attribute: the full unpaginated match count, computed by the same query (\verb{COUNT(*) OVER()}) rather than a second scan. } \description{ Resolves human-readable place names into rows of \code{canonical_fips_xwalk}, the cross-vintage canonical-government registry. Operates in two modes: } \details{ \itemize{ \item \strong{Utility mode} (single \code{name}, the original behavior): returns all rows whose \code{gov_name} contains \code{name} as a \strong{literal, case-insensitive substring}, sorted by \code{population_acs} descending. Useful for exploratory lookups. Regex metacharacters in \code{name} are escaped, so a government is findable by its own complete name even when that name contains parentheses or a period. \item \strong{Basket mode} (\code{length(name) > 1}): resolves each input row to a single canonical govid and returns a tibble in input order, suitable for piping straight into \code{\link[=cog_spending]{cog_spending()}} / \code{\link[=cog_revenue]{cog_revenue()}} / \code{\link[=cog_geographic_rollup]{cog_geographic_rollup()}}. Carries an audit sidecar accessible via \code{\link[=cog_basket_resolution]{cog_basket_resolution()}} / \code{\link[=cog_basket_unresolved]{cog_basket_unresolved()}}. } \strong{Basket-mode resolution algorithm} (per input row): \enumerate{ \item Filter \code{canonical_fips_xwalk} by \code{state} and (if non-NA) \code{type}. \item \strong{Exact pass:} case-insensitive equality against \code{gov_name}. Single hit -> resolved. Multiple -> step 4. \item \strong{Substring fallback:} case-insensitive literal substring against \code{gov_name} (metacharacters escaped). Single hit -> resolved (\code{match_method = "substring"}). Zero hits -> \code{status = "no_match"}. Multiple hits -> step 4. \item \strong{Disambiguation:} if matches share one \code{govs_type}, pick the largest-population row (\code{status = "largest_pop"}). If they span >=2 types, no row is added (\code{status = "ambiguous"}); the user should re-run with \code{type} specified. } Resolved rows form the returned tibble in input order. Unresolved inputs (\code{ambiguous} / \code{no_match}) appear only in the sidecar. } \examples{ \dontrun{ # Utility mode — exploratory substring lookup cog_gov_search("broward", state = "FL") # Basket mode — resolve a known cohort basket <- cog_gov_search( name = c("BROWARD COUNTY", "SAN DIEGO CITY", "AUSTIN CITY"), state = c("FL", "CA", "TX") ) basket # Inspect resolution audit cog_basket_resolution(basket) # Pipe into a spending query library(dplyr) basket |> cog_spending(years = 2019:2020, category = "Police") # Iteratively refine ambiguous matches partial <- cog_gov_search( name = c("Broward", "San Diego"), # San Diego is ambiguous state = c("FL", "CA") ) cog_basket_unresolved(partial) refined <- cog_gov_search( name = c("Broward", "San Diego"), state = c("FL", "CA"), type = c(NA, "city") # disambiguate ) } } \seealso{ \code{\link[=cog_basket_resolution]{cog_basket_resolution()}}, \code{\link[=cog_basket_unresolved]{cog_basket_unresolved()}}, \code{\link[=cog_spending]{cog_spending()}}, \code{\link[=cog_revenue]{cog_revenue()}}. }