Compare commits
45
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6392a74013
|
||
|
|
de2ba0cfb9 | ||
|
|
e912a926c2 | ||
|
|
44e4953f94
|
||
|
|
a5500f0b6a
|
||
|
|
da2839f885
|
||
|
|
331399ab86
|
||
|
|
5582c6cb57
|
||
|
|
013af5b0d2
|
||
|
|
4a92f36d44
|
||
|
|
a67735f121
|
||
|
|
b9f7f7d8d3
|
||
|
|
4300b636b1
|
||
|
|
99e1e86e37
|
||
|
|
c042ee0b90
|
||
|
|
29dc8199e0
|
||
|
|
619b167ab1
|
||
|
|
dfda39051e
|
||
|
|
785f3af16d
|
||
|
|
c587c8ba87 | ||
|
|
6a06302036
|
||
|
|
e3ab26c3e6 | ||
|
|
498950afa6
|
||
|
|
a5f86d87b3
|
||
|
|
61b9c95731
|
||
|
|
44e9b40b86
|
||
|
|
f1e9aa383a
|
||
|
|
12a9be110f
|
||
|
|
503fa6562f
|
||
|
|
11ae99c382
|
||
|
|
5e22e940e7
|
||
|
|
2fc9e7585b | ||
|
|
8bf9c4ccc1
|
||
|
|
77074621d8
|
||
|
|
4b749205a5
|
||
|
|
f77adb6c83
|
||
|
|
230f3401c4
|
||
|
|
7522b48a08
|
||
|
|
693f8d81a6
|
||
|
|
db35fa9058
|
||
|
|
cabe2e2799
|
||
|
|
6cd219a291 | ||
|
|
e067a5930f
|
||
|
|
342debaefa
|
||
|
|
b59b79b2d5 |
+3
-3
@@ -3,17 +3,17 @@
|
|||||||
^\.Rproj\.user$
|
^\.Rproj\.user$
|
||||||
^_pkgdown\.yml$
|
^_pkgdown\.yml$
|
||||||
^docs$
|
^docs$
|
||||||
|
^Meta$
|
||||||
|
^doc$
|
||||||
^pkgdown$
|
^pkgdown$
|
||||||
^\.github$
|
^\.github$
|
||||||
^LICENSE\.md$
|
^LICENSE\.md$
|
||||||
^\.git$
|
^\.git$
|
||||||
^\.gitignore$
|
^\.gitignore$
|
||||||
\.gitkeep$
|
\.gitkeep$
|
||||||
^vignettes$
|
|
||||||
^specs$
|
^specs$
|
||||||
^plans$
|
^plans$
|
||||||
^doc$
|
|
||||||
^Meta$
|
|
||||||
^\.gitea$
|
^\.gitea$
|
||||||
^CLAUDE\.md$
|
^CLAUDE\.md$
|
||||||
^\.superpowers$
|
^\.superpowers$
|
||||||
|
^CONTRIBUTING\.md$
|
||||||
|
|||||||
@@ -11,6 +11,21 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Install system libraries and Node.js (required by actions/checkout)
|
- name: Install system libraries and Node.js (required by actions/checkout)
|
||||||
run: |
|
run: |
|
||||||
|
# Switch apt to HTTPS mirrors. Measured from this runner on
|
||||||
|
# 2026-08-04: the SAME index file takes 20.1s over http:// and 3.1s
|
||||||
|
# over https://. apt fetches many indexes serially, so http:// does
|
||||||
|
# not read as "slow" -- it reads as a hang (zero bytes in
|
||||||
|
# /var/cache/apt/archives after 3+ minutes, apt's http workers parked
|
||||||
|
# in S state). rocker/r-ver:4.4 already ships ca-certificates and
|
||||||
|
# apt 2.8.3 has the https method built in, so nothing needs to be
|
||||||
|
# installed over http first to bootstrap this.
|
||||||
|
# `|| true` because the step runs under `sh -e`: on an image whose
|
||||||
|
# sources live in the other location, the missing-file sed must not
|
||||||
|
# kill the job.
|
||||||
|
sed -i -E 's#http://(archive|security)\.ubuntu\.com#https://\1.ubuntu.com#g' \
|
||||||
|
/etc/apt/sources.list.d/ubuntu.sources 2>/dev/null || true
|
||||||
|
sed -i -E 's#http://(archive|security)\.ubuntu\.com#https://\1.ubuntu.com#g' \
|
||||||
|
/etc/apt/sources.list 2>/dev/null || true
|
||||||
apt-get update -qq
|
apt-get update -qq
|
||||||
apt-get install -y --no-install-recommends \
|
apt-get install -y --no-install-recommends \
|
||||||
nodejs git \
|
nodejs git \
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
# Multi-platform R CMD check, running on the GitHub mirror.
|
||||||
|
#
|
||||||
|
# This exists because the canonical Gitea runner is Linux-only, and this
|
||||||
|
# package hard-depends on duckdb and httr2 -- both compiled, both with real
|
||||||
|
# platform variance -- while having never been checked on Windows or macOS.
|
||||||
|
# A large share of the audience is on Windows.
|
||||||
|
#
|
||||||
|
# Gitea reads .gitea/workflows and GitHub reads .github/workflows, so this
|
||||||
|
# file is inert on the canonical repo and coexists with the Gitea CI that
|
||||||
|
# remains authoritative for deploys.
|
||||||
|
#
|
||||||
|
# The suite needs NO credentials: tests/testthat/setup.R points USCOGDATA_URL
|
||||||
|
# at the bundled fixture corpus. That is exactly why inst/extdata/fixture_corpus
|
||||||
|
# must never be added to .Rbuildignore.
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
name: R-CMD-check
|
||||||
|
|
||||||
|
permissions: read-all
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
R-CMD-check:
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- {os: macos-latest, r: 'release'}
|
||||||
|
- {os: windows-latest, r: 'release'}
|
||||||
|
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
|
||||||
|
- {os: ubuntu-latest, r: 'release'}
|
||||||
|
|
||||||
|
env:
|
||||||
|
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
R_KEEP_PKG_SOURCE: yes
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- uses: r-lib/actions/setup-pandoc@v2
|
||||||
|
|
||||||
|
- uses: r-lib/actions/setup-r@v2
|
||||||
|
with:
|
||||||
|
r-version: ${{ matrix.config.r }}
|
||||||
|
http-user-agent: ${{ matrix.config.http-user-agent }}
|
||||||
|
use-public-rspm: true
|
||||||
|
|
||||||
|
- uses: r-lib/actions/setup-r-dependencies@v2
|
||||||
|
with:
|
||||||
|
extra-packages: any::rcmdcheck
|
||||||
|
needs: check
|
||||||
|
|
||||||
|
- uses: r-lib/actions/check-r-package@v2
|
||||||
|
with:
|
||||||
|
upload-snapshots: true
|
||||||
|
build_args: 'c("--no-manual")'
|
||||||
@@ -124,3 +124,10 @@ devtools::test()
|
|||||||
a numbered `.sql` file; build a query in R.
|
a numbered `.sql` file; build a query in R.
|
||||||
- No arrow dependency — DuckDB reads parquet natively
|
- No arrow dependency — DuckDB reads parquet natively
|
||||||
- `withr` is a Suggests-only dep; only used in tests
|
- `withr` is a Suggests-only dep; only used in tests
|
||||||
|
|
||||||
|
## Domain context — read this first
|
||||||
|
|
||||||
|
**Before doing any work in this repo, read `~/.claude/memory/values/civilytics.md`.**
|
||||||
|
It carries the purpose, direction, and constraints for this domain. It is not optional
|
||||||
|
context — read it before planning or writing code, not after. (An `@` import will not
|
||||||
|
work here; project-level imports don't preload. The read is the mechanism.)
|
||||||
|
|||||||
+105
@@ -0,0 +1,105 @@
|
|||||||
|
# Contributing to uscogdata
|
||||||
|
|
||||||
|
Thanks for reading this — a package like this gets better mostly through people
|
||||||
|
noticing that a number looks wrong.
|
||||||
|
|
||||||
|
## Where the code lives
|
||||||
|
|
||||||
|
Development happens on **Gitea**, at
|
||||||
|
`gitea.civilytics.org/Civilytics/uscogdata`. The repository at
|
||||||
|
`github.com/civilytics/uscogdata` is a **mirror** that accepts issues and pull
|
||||||
|
requests.
|
||||||
|
|
||||||
|
## What happens to a GitHub pull request
|
||||||
|
|
||||||
|
Open it normally. Behind the scenes it is fetched and landed on the canonical
|
||||||
|
Gitea repository, then syncs back:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git fetch github refs/pull/42/head:pr-42
|
||||||
|
git switch main && git merge --no-ff pr-42
|
||||||
|
git push origin main # Gitea -> mirror -> GitHub
|
||||||
|
```
|
||||||
|
|
||||||
|
Because the merge preserves your commits at their original SHAs, **GitHub marks
|
||||||
|
your PR merged on its own** as soon as the mirror syncs. So:
|
||||||
|
|
||||||
|
> If your pull request closes as "Merged" without anyone visibly clicking
|
||||||
|
> Merge, that is the normal, successful outcome — not a rejection.
|
||||||
|
|
||||||
|
Substantial contributions get a `ctb` entry in `DESCRIPTION`, which surfaces in
|
||||||
|
`citation("uscogdata")`.
|
||||||
|
|
||||||
|
There is no CLA and no DCO sign-off requirement.
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
```r
|
||||||
|
devtools::test() # bundled fixture; no network, no credentials
|
||||||
|
```
|
||||||
|
|
||||||
|
`tests/testthat/setup.R` points `USCOGDATA_URL` at
|
||||||
|
`inst/extdata/fixture_corpus/` automatically — a four-year slice (2011, 2012,
|
||||||
|
2019, 2020) covering all 50 states. That is the whole data setup.
|
||||||
|
|
||||||
|
## Testing against the live corpus
|
||||||
|
|
||||||
|
```sh
|
||||||
|
USCOGDATA_LIVE_TEST=true Rscript -e 'devtools::test(filter = "live-corpus")'
|
||||||
|
```
|
||||||
|
|
||||||
|
This is worth understanding rather than skipping. Until 0.3.0 the package
|
||||||
|
**could not read a remote corpus at all** — the partitioned view used a glob,
|
||||||
|
and DuckDB cannot expand a glob over generic HTTP. It went unnoticed for months
|
||||||
|
because every test path used a local corpus (the bundled fixture), and so did
|
||||||
|
the production API (a host mount). Nothing exercised the package the way a new
|
||||||
|
user does.
|
||||||
|
|
||||||
|
`test-live-corpus.R` is the only test that runs with no `USCOGDATA_URL`, no
|
||||||
|
option, and no fixture. If you change anything touching view registration,
|
||||||
|
manifest handling, or configuration, run it.
|
||||||
|
|
||||||
|
## Do not exclude the fixture from the build
|
||||||
|
|
||||||
|
There is a temptation to add `^inst/extdata/fixture_corpus$` to
|
||||||
|
`.Rbuildignore` because 15 MB feels large for a package. Don't:
|
||||||
|
|
||||||
|
- `vignette("total-spending")` reads from it and would fail to build.
|
||||||
|
- `R CMD check` on r-universe and GitHub Actions would have no corpus, so the
|
||||||
|
suite could not run without credentials.
|
||||||
|
|
||||||
|
This package is not going to CRAN, so its 5 MB guidance does not apply. A
|
||||||
|
package-size NOTE in `R CMD check` is expected and acceptable.
|
||||||
|
|
||||||
|
## Downstream consumers
|
||||||
|
|
||||||
|
`cog-api` depends on this package and its CI clones uscogdata at
|
||||||
|
`USCOGDATA_REF`, **defaulting to `main`**. There is no pin. Anything merged
|
||||||
|
here reaches the API's next build, so before merging a change to the reader,
|
||||||
|
run the API suite against your branch:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
Rscript -e "remotes::install_local('/path/to/uscogdata', upgrade = 'never')"
|
||||||
|
cd /path/to/cog-api/api/tests/testthat
|
||||||
|
Rscript -e 'testthat::test_dir(".", stop_on_failure = TRUE)'
|
||||||
|
```
|
||||||
|
|
||||||
|
The API calls only exported verbs, so internal refactors are usually safe —
|
||||||
|
but "usually" is not a release gate.
|
||||||
|
|
||||||
|
## Release checklist
|
||||||
|
|
||||||
|
1. `devtools::test()` — green against the bundled fixture, offline.
|
||||||
|
2. `USCOGDATA_LIVE_TEST=true devtools::test()` — green against the live corpus.
|
||||||
|
3. cog-api suite green against this branch (above).
|
||||||
|
4. `devtools::check(args = "--as-cran")` — 0 errors, 0 warnings.
|
||||||
|
5. `pkgdown::build_site()` completes.
|
||||||
|
6. Vignettes resolve from an installed copy:
|
||||||
|
`vignette("total-spending", package = "uscogdata")`.
|
||||||
|
7. **Cold-start check**: on a machine that has never had this package,
|
||||||
|
install it and run the README quickstart verbatim with no environment
|
||||||
|
variables set. This is the only check that catches a
|
||||||
|
corpus-unreachable defect, and its absence is why 0.3.0 needed fixing.
|
||||||
|
8. Bump `Version` and add a `NEWS.md` section.
|
||||||
|
9. Tag, then update the r-universe registry pin at
|
||||||
|
`github.com/civilytics/civilytics.r-universe.dev`.
|
||||||
+11
-4
@@ -1,14 +1,21 @@
|
|||||||
Package: uscogdata
|
Package: uscogdata
|
||||||
Type: Package
|
Type: Package
|
||||||
Title: Curated Reader for the Civilytics US Census of Governments Finance Corpus
|
Title: Curated Reader for the Civilytics US Census of Governments Finance Corpus
|
||||||
Version: 0.1.0
|
Version: 0.3.0
|
||||||
Authors@R:
|
Authors@R: c(
|
||||||
person("Civilytics", , , "jknowles@gmail.com", role = c("aut", "cre"))
|
person(c("Jared", "E."), "Knowles",
|
||||||
|
email = "jared@civilytics.com",
|
||||||
|
role = c("aut", "cre"),
|
||||||
|
comment = c(ORCID = "0000-0003-0005-9478")),
|
||||||
|
person("Civilytics Consulting LLC", role = c("cph", "fnd")))
|
||||||
Description: Curated R verbs over the Civilytics US Census of Governments
|
Description: Curated R verbs over the Civilytics US Census of Governments
|
||||||
finance corpus. Provides unit-level financial profiles, geographic
|
finance corpus. Provides unit-level financial profiles, geographic
|
||||||
rollups, and peer comparisons with auditable provenance and built-in
|
rollups, and peer comparisons with auditable provenance and built-in
|
||||||
cross-vintage correctness.
|
cross-vintage correctness.
|
||||||
License: MIT + file LICENSE
|
License: MIT + file LICENSE
|
||||||
|
URL: https://github.com/civilytics/uscogdata,
|
||||||
|
https://civilytics.r-universe.dev/uscogdata
|
||||||
|
BugReports: https://github.com/civilytics/uscogdata/issues
|
||||||
Encoding: UTF-8
|
Encoding: UTF-8
|
||||||
LazyData: false
|
LazyData: false
|
||||||
Depends: R (>= 4.1)
|
Depends: R (>= 4.1)
|
||||||
@@ -32,4 +39,4 @@ Config/testthat/edition: 3
|
|||||||
VignetteBuilder: knitr
|
VignetteBuilder: knitr
|
||||||
RoxygenNote: 7.3.3
|
RoxygenNote: 7.3.3
|
||||||
MinCorpusSchema: 4
|
MinCorpusSchema: 4
|
||||||
MaxCorpusSchema: 5
|
MaxCorpusSchema: 7
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
YEAR: 2026
|
YEAR: 2026
|
||||||
COPYRIGHT HOLDER: Civilytics
|
COPYRIGHT HOLDER: Civilytics Consulting LLC
|
||||||
|
|||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
# MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2026 Civilytics Consulting LLC
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -1,225 +1,101 @@
|
|||||||
# uscogdata 0.1.0 (development)
|
# uscogdata 0.3.0
|
||||||
|
|
||||||
## New: `cog_balances()` for cash-and-security holdings
|
First public release.
|
||||||
|
|
||||||
* New `cog_balances()` exposes the 14 cash-and-security holding codes
|
`uscogdata` provides curated R verbs over the Civilytics US Census of
|
||||||
(`category_type = "balance"`): fund balances, retirement system holdings and
|
Governments finance corpus: unit-level financial profiles, geographic rollups
|
||||||
insurance trust balances (#25). Holdings are a stock, not a flow, so the verb
|
and peer comparisons, with auditable provenance on every result.
|
||||||
has no `expenditure_concept` / `revenue_concept` / `complete` arguments, and
|
|
||||||
no `subtype` argument either -- for holdings, `category` is a strict
|
|
||||||
coarsening of `balance_subtype`, so `category = "Fund Balances"` is exactly
|
|
||||||
the `general` family (`W01`/`W31`/`W61`).
|
|
||||||
* `cog_balances()` results carry `provenance$balance_caveats`, recording that
|
|
||||||
Census holdings are gross rather than GAAP fund balance, and the measured
|
|
||||||
coverage window of each subtype family.
|
|
||||||
|
|
||||||
## Multi-government aggregates now disclose their reporting coverage
|
## What it covers
|
||||||
|
|
||||||
* The Census of Governments is a **complete census only in years ending in 2
|
Government types 0-3 (state, county, municipality, township), FY1967-FY2024 --
|
||||||
and 7**; every other year is a sample, and the sample varies enormously. On
|
56 fiscal years, 46,148,034 rows, 190.6 MB. There is no source data for FY1968
|
||||||
the bundled fixture, Wisconsin's 608-city universe rolls up **597**
|
or FY1969. Special districts (type 4) and school districts (type 5) are out of
|
||||||
governments in FY2012 and **112** in FY2019 — an 18%-to-98% swing the
|
scope pending validation.
|
||||||
return value said nothing about, so a statewide total resting on a fifth of
|
|
||||||
the universe looked exactly like one resting on all of it.
|
|
||||||
* `cog_geographic_rollup()`, `cog_peer_compare()` and `cog_find_peers()` gain
|
|
||||||
`coverage`:
|
|
||||||
|
|
||||||
| value | effect |
|
## The verbs
|
||||||
|---|---|
|
|
||||||
| `"all"` (default) | every unit that reported that year — unchanged behaviour |
|
|
||||||
| `"census"` | census years only; aborts if the range holds none rather than returning nothing |
|
|
||||||
| `"consistent"` | only units reporting in *every* requested year — a balanced panel |
|
|
||||||
|
|
||||||
* **Regardless of mode**, every result now carries `provenance$coverage` with
|
`cog_spending()`, `cog_revenue()` and `cog_balances()` for flows and holdings;
|
||||||
per-year `n_units_reporting`, `n_units_expected` and `is_census_year`, plus
|
`cog_gov_search()` to resolve place names (including basket mode for many at
|
||||||
`provenance$coverage_mode`. `cog_explain()` prints a "Reporting coverage"
|
once); `cog_find_peers()` and `cog_peer_compare()` for cohorts;
|
||||||
section. So the default mode can no longer mislead silently.
|
`cog_geographic_rollup()` for aggregates; `cog_categories()`, `cog_recipes()`,
|
||||||
* `is_census_year` is a statement about the **survey calendar**, never a claim
|
`cog_manifest()` and `cog_explain()` for metadata and provenance; and
|
||||||
of completeness: FY1967 is a census year in which only 97 of Wisconsin's 608
|
`cog_mirror()` for a local copy of the corpus.
|
||||||
cities report. `n_units_reporting` is the number that tells the truth.
|
|
||||||
* On `cog_peer_compare()` the target is exempt from `"consistent"` balancing —
|
|
||||||
it is the subject of the comparison, not a member of the cohort — and the
|
|
||||||
`summary_*` quantiles are computed after the filter, so they describe the
|
|
||||||
cohort actually returned. `n_units_reporting` counts peers only, against the
|
|
||||||
cohort size.
|
|
||||||
* On `cog_find_peers()`, `coverage` governs the cohort **vintage** when `year`
|
|
||||||
is `NULL`: `"census"` snaps to the most recent census year with an observed
|
|
||||||
population, so a cohort is not built from a sample year in which most of the
|
|
||||||
candidate universe is absent.
|
|
||||||
|
|
||||||
## `complete = TRUE`: absent cells, labelled with why they are absent
|
## Reading the corpus now works out of the box
|
||||||
|
|
||||||
* `cog_spending()` and `cog_revenue()` gain `complete`, defaulting to `FALSE`
|
* The package reads the published corpus over HTTPS **with no configuration**.
|
||||||
(today's behaviour). With `complete = TRUE` the requested grid is filled
|
Previously the default was a placeholder sentinel and no document in the
|
||||||
from the corpus's `code_set` table and every row carries a new
|
package supplied a working URL, so a new user had no path to a session.
|
||||||
`value_source` column:
|
* Remote reads work at all. The partitioned view used a glob, and DuckDB
|
||||||
|
cannot expand a glob over generic HTTP -- there is no directory listing to
|
||||||
|
expand against. Partition paths are now enumerated from the corpus manifest,
|
||||||
|
which is host-agnostic: an HTTPS mirror, a Nextcloud share and a local
|
||||||
|
`cog_mirror()` copy all take the same path.
|
||||||
|
* Nothing is written to disk in remote mode; DuckDB fetches only the row
|
||||||
|
groups a query needs.
|
||||||
|
|
||||||
| `value_source` | meaning | `amt_nominal` |
|
## Four things to know before your first query
|
||||||
|---|---|---|
|
|
||||||
| `reported` | the corpus carries this cell | as published |
|
|
||||||
| `census_zero` | dense-source year (≤ FY2011), cell absent — Census published `$0` | `0` |
|
|
||||||
| `not_reported` | sparse-source year (≥ FY2012), cell absent — unknown | `NA` |
|
|
||||||
|
|
||||||
The `NA` is deliberate and is the whole point: filling a modern absence
|
* **Amounts are in full US dollars.** The raw Census files report thousands;
|
||||||
with `0` would invent data, which is precisely the error the corpus's
|
the verbs multiply by 1000 on the way out. Do not multiply again.
|
||||||
representation contract exists to prevent.
|
* **Multi-government aggregates disclose their coverage.** The Census is a
|
||||||
* This restores information the reader lost when the corpus was sparsified
|
complete enumeration only in years ending in 2 and 7; every other year is a
|
||||||
(`SB194`, cog_pipeline#64) — a wide-era query whose cells were all `$0`
|
sample. Every such result carries `provenance$coverage` with per-year
|
||||||
had begun returning nothing at all — and improves on what came before it,
|
`n_units_reporting`.
|
||||||
since the pre-sparsification corpus could not distinguish a published zero
|
* **Absence means two different things.** Before FY2012 an absent cell means
|
||||||
from an unreported cell either.
|
Census published $0; from FY2012 it means not reported. `complete = TRUE`
|
||||||
* The grid is scoped to each government's **own type**, so a county is never
|
labels which.
|
||||||
filled with cells only a state can report.
|
* **Series breaks reach you unasked.** Catalogued breaks intersecting your
|
||||||
* Needs a corpus published from 2026-07-29 onward (when `representation` and
|
query appear in provenance and in `cog_explain()`.
|
||||||
`code_set` began shipping); aborts with class
|
|
||||||
`uscogdata_representation_unavailable` otherwise. Gated on the manifest
|
|
||||||
listing those tables rather than on `schema_version`, which was never
|
|
||||||
bumped for the change. Not available with `recipe` or
|
|
||||||
`expenditure_concept = "total"` — neither draws its cells from `code_set`.
|
|
||||||
* `provenance$completion` reports `applied`, `rows_filled`, and the per-year
|
|
||||||
`absence_means` rule; `cog_explain()` prints a "Completion" section.
|
|
||||||
|
|
||||||
## Corpus-wide series breaks now reach users (`corpus_break_refs`)
|
## Known limits
|
||||||
|
|
||||||
* Four catalogued series breaks carry `fin_code = "ALL"` — caveats about the
|
* Special districts (type 4) and school districts (type 5) are out of scope.
|
||||||
corpus as a whole rather than about one item code. `series_break_refs` is
|
* Per-capita rollups exclude governments with no F-33 population, which is by
|
||||||
built by matching `fin_code` against the item codes in the result, and no
|
design but does silently narrow a rollup.
|
||||||
row's `item_code` is ever the literal `"ALL"`, so **none of them could ever
|
* `n_units_reporting` is category-conditional and is not a response rate.
|
||||||
be surfaced**: `SB085` (dollar precision across the 1976/1977 boundary),
|
* Employee-retirement (`X`) codes stop at FY2016, when those systems moved to
|
||||||
`SB087` (imputation exclusion from FY2002), `SB194` (the dense → sparse
|
the Annual Survey of Public Pensions.
|
||||||
representation change at FY2012) and `SB086` (the government id scheme
|
|
||||||
change at FY2017).
|
|
||||||
* Provenance gains `corpus_break_refs`, selected on the break-year window
|
|
||||||
alone and disjoint from `series_break_refs` by construction, so a consumer
|
|
||||||
can tell a whole-result caveat from a break in one series. `cog_explain()`
|
|
||||||
prints them under their own "Corpus-wide caveats" heading. cog-api passes
|
|
||||||
provenance through verbatim, so the field appears there without an API
|
|
||||||
change.
|
|
||||||
* `SB194` is the one that made this urgent: a query spanning FY2011 → FY2012
|
|
||||||
crosses the boundary where an absent cell stops meaning "Census published
|
|
||||||
`$0`" and starts meaning "not reported", and until now nothing said so.
|
|
||||||
|
|
||||||
## Bundled fixture regenerated against the sparsified corpus
|
# uscogdata 0.2.0
|
||||||
|
|
||||||
* `inst/extdata/fixture_corpus/` now tracks the corpus published on
|
|
||||||
2026-07-29 (`pipeline_commit 83f9715`, schema v6). The wide era no longer
|
|
||||||
stores explicit zeros: FY2011 fell from 2,864,212 rows to 496,004, of
|
|
||||||
which none are `$0`. **Absence now means two different things** — in a
|
|
||||||
`dense_source` year (≤ FY2011) an absent cell means Census published `$0`;
|
|
||||||
in a `sparse_source` year (≥ FY2012) it means not reported. The corpus
|
|
||||||
carries that rule in two new tables the fixture now ships,
|
|
||||||
`representation.parquet` and `code_set.parquet`, alongside
|
|
||||||
`census_collection_coverage.parquet` and `lineage_events.parquet`
|
|
||||||
(all ten publish-tree metadata tables, up from six). Catalogued upstream
|
|
||||||
as series break `SB194`.
|
|
||||||
* `cog_categories()` gains an `assistance` spending subtype: the J-prefix
|
|
||||||
aid/benefit codes (`J19`, `J67`, `J68`, `J85`) are categorised now that
|
|
||||||
the upstream crosswalk covers every flow code carrying dollars.
|
|
||||||
* Two consequences worth knowing about, both visible in provenance rather
|
|
||||||
than in returned dollars. The harmonization block's `na_rows_excluded`
|
|
||||||
counts only rows that exist, so wide-era codes that were zero-padded no
|
|
||||||
longer appear there. Coverage-gap `suggestions` are presence-based for the
|
|
||||||
same reason, so a recipe whose component codes were all `$0` for a given
|
|
||||||
government-year is no longer suggested for it.
|
|
||||||
* `tests/testthat/test-fixture-vintage.R` pins these structural facts, so a
|
|
||||||
fixture left behind by a future publish fails loudly instead of letting the
|
|
||||||
suite pass against a corpus that no longer exists.
|
|
||||||
|
|
||||||
## Breaking: corpus schema_version 4 (Phase P canonical ids)
|
|
||||||
|
|
||||||
* The package now requires corpus `schema_version = 4` (`MinCorpusSchema` /
|
|
||||||
`MaxCorpusSchema` in `DESCRIPTION` are both `4`); older corpora built
|
|
||||||
against schema 3 are rejected by `cog_open()` with a clear version-mismatch
|
|
||||||
error. `canonical_govid` is now uniformly 12 characters across every
|
|
||||||
vintage the corpus covers (previously a mix of 9-char legacy ids and
|
|
||||||
12-char FIPS ids depending on source year) — **every hardcoded
|
|
||||||
`canonical_govid` literal from a pre-Phase-P corpus is now invalid** and
|
|
||||||
must be re-resolved via `cog_gov_search()` or the new `canonical_alias`
|
|
||||||
lookup table. `canonical_fips_xwalk` gains four columns
|
|
||||||
(`legacy_govs_id`, `census_geoid`, `id_source`; `confidence` is renamed to
|
|
||||||
`pop_confidence`) and a companion `canonical_alias` table ships in the
|
|
||||||
corpus for mapping legacy/alternate ids onto the current canonical
|
|
||||||
namespace. The bundled fixture corpus (`inst/extdata/fixture_corpus/`) has
|
|
||||||
been regenerated against the Phase P publish tree, now ships the full
|
|
||||||
`canonical_fips_xwalk` and `canonical_alias` master tables alongside the
|
|
||||||
2019-2020 long partitions, and is reproducible via
|
|
||||||
`data-raw/regenerate_fixture_corpus.R`.
|
|
||||||
|
|
||||||
## Clearer errors when `USCOGDATA_URL` is unconfigured or returns non-JSON
|
|
||||||
|
|
||||||
* `cog_open()` now aborts with the `uscogdata_url_not_configured` error
|
|
||||||
class when the resolved corpus URL still contains the placeholder
|
|
||||||
`REPLACE_WITH_SHARE_TOKEN` sentinel (or is empty). The message lists both
|
|
||||||
remediation paths (`Sys.setenv(USCOGDATA_URL = ...)` and
|
|
||||||
`options(uscogdata.url = ...)`) and points at the bundled fixture for
|
|
||||||
offline testing. Previously the package proceeded to fetch the placeholder
|
|
||||||
URL, cached the resulting HTML welcome page, and failed downstream with a
|
|
||||||
cryptic `jsonlite` lexical-error.
|
|
||||||
* `.fetch_or_cache_manifest()` now parses the HTTP response body before
|
|
||||||
persisting it. Non-JSON responses (login pages, 404 HTML) raise
|
|
||||||
`uscogdata_invalid_manifest` with the URL, Content-Type, and underlying
|
|
||||||
parse error — and never write to the on-disk cache.
|
|
||||||
* Manifest cache writes are now atomic (write to `manifest.json.tmp.<pid>`
|
|
||||||
in `cache_dir`, then `file.rename` over the target), so an interrupted
|
|
||||||
fetch cannot replace a previously-good cache.
|
|
||||||
* Existing caches with non-JSON content (poisoned by the prior code path)
|
|
||||||
are silently refetched instead of returning a parse error to the caller.
|
|
||||||
* Local `USCOGDATA_URL` paths whose `manifest.json` is not valid JSON now
|
|
||||||
surface the same `uscogdata_invalid_manifest` class with file context.
|
|
||||||
|
|
||||||
## Per-capita denominators now use per-year Census F-33 population
|
|
||||||
|
|
||||||
* `cog_spending()` and `cog_revenue()` previously divided all years' amounts
|
|
||||||
by a single ACS 2018-2022 estimate (`canonical_fips_xwalk.population_acs`),
|
|
||||||
producing biased per-capita values for time-series analysis. They now
|
|
||||||
divide by the F-33 `population` recorded on each gov-year via the new
|
|
||||||
`gov_population_yearly` view. Result tibbles gain a `pop_source` column
|
|
||||||
with values `"census_f33"` or `"unavailable"`. `notes` is updated to
|
|
||||||
concatenate multiple notes with `"; "`.
|
|
||||||
|
|
||||||
## Peer cohorts can be set to a chosen year
|
|
||||||
|
|
||||||
* `cog_find_peers()` adds a `year` argument (default: most recent year for
|
|
||||||
which the target has an observed population in `gov_population_yearly`).
|
|
||||||
The returned column previously named `population_acs` is now `population`
|
|
||||||
and reflects the cohort year's vintage. The cohort year is attached to the
|
|
||||||
returned tibble as `attr(x, "cohort_year")`.
|
|
||||||
* `cog_peer_compare()` now stamps a `cohort_year` column on its result (read
|
|
||||||
from the peers tibble's attribute) and records `cohort_year` plus
|
|
||||||
`cohort_govids` in provenance. When the caller supplies a bare character
|
|
||||||
vector instead of a `cog_find_peers()` result, `cohort_year` is `NA`.
|
|
||||||
|
|
||||||
## Rollups exclude govs missing population
|
|
||||||
|
|
||||||
* `cog_geographic_rollup(per_capita = TRUE)` drops rows whose government has
|
|
||||||
`pop_source == "unavailable"` and records the dropped govids in
|
|
||||||
`provenance$rollup$excluded_govids`. This excludes special districts
|
|
||||||
(type 4) and school districts (type 5) from per-capita rollups by design.
|
|
||||||
|
|
||||||
## New: vignette and provenance metadata
|
|
||||||
|
|
||||||
* New vignette `population-denominators` covers the four population sources,
|
|
||||||
the type-4/5 coverage gap, the popyear quirk, and how to build moving-window
|
|
||||||
peer cohorts manually.
|
|
||||||
* Provenance gains `transformations$per_capita$popyear_range` and
|
|
||||||
`pop_source_counts`. `cog_explain()` renders both.
|
|
||||||
|
|
||||||
## New features
|
## New features
|
||||||
|
|
||||||
* `cog_gov_search()` gains a **basket mode**: passing vector `name`
|
* `cog_spending()` and `cog_revenue()` accept the reserved category
|
||||||
/ `state` / `type` arguments resolves multiple place names in one
|
`"All Categories"`, returning one summed row per
|
||||||
call and returns a tibble of canonical rows in input order, ready
|
`(year, canonical_govid, subtype)` across every category inside the
|
||||||
to pipe into `cog_spending()` / `cog_revenue()`. Per-row resolution
|
requested concept's subtype scope. Filtering the result to
|
||||||
follows an exact-then-substring matching algorithm with deterministic
|
`spend_subtype == "operations"` gives an operating-expenditure total.
|
||||||
disambiguation; ambiguous and missing entries are surfaced via a
|
`cog_geographic_rollup()` inherits it,
|
||||||
sidecar audit tibble plus a single console summary message.
|
which is the efficient way to build a geographic total — previously a
|
||||||
* New exports `cog_basket_resolution()` and `cog_basket_unresolved()`
|
caller had to issue one rollup per category and sum the results
|
||||||
expose the basket sidecar for iterative query refinement.
|
(cog-api#37).
|
||||||
|
|
||||||
## Breaking changes
|
`"All Categories"` is not the same thing as `expenditure_concept = "total"`.
|
||||||
|
The concept chooses which subtypes are in scope; `"All Categories"` chooses
|
||||||
|
whether the rows inside that scope are broken out or summed.
|
||||||
|
|
||||||
* The first formal of `cog_gov_search()` was renamed from `pattern`
|
* `cog_categories()` advertises `"All Categories"` for the expenditure and
|
||||||
to `name`. All existing call sites in `cog_explorer/` and the
|
revenue vocabularies, so the reserved value is discoverable.
|
||||||
package itself use positional first-arg, so this rename is
|
|
||||||
non-breaking in practice. Callers that pass `pattern = ...` by name
|
* Coverage signposting (see "Signposting now catches partially-suppressed
|
||||||
must update to `name = ...`.
|
categories" below) now also works in `category = "All Categories"` mode.
|
||||||
|
The recipe-suggestion candidate query used to be scoped by `category`,
|
||||||
|
which is never a match for the reserved `"All Categories"` value, so
|
||||||
|
`provenance$suggestions` always came back empty there — the one mode whose
|
||||||
|
whole point is "you cannot sum the wrong scope" was silently unable to
|
||||||
|
signal a wrong scope. The candidate query is now scoped by the concept's
|
||||||
|
subtype allowlist instead, symmetric with how `.build_verb_sql()` itself
|
||||||
|
scopes the summed total: Los Angeles County FY2011, `category = "All
|
||||||
|
Categories"` still excludes $271,589,000 of aggregate-published Public
|
||||||
|
Welfare (`E68`), but now names `recipe = "welfare_cash_e68_wide"` to
|
||||||
|
recover it instead of reporting zero suggestions.
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
* `cog_geographic_rollup()` and `cog_peer_compare()` now document that
|
||||||
|
`provenance$coverage`'s `n_units_reporting` is **category-conditional** and
|
||||||
|
is not a response rate: a government that was surveyed and genuinely spends
|
||||||
|
nothing in the requested category is indistinguishable from one never
|
||||||
|
surveyed (uscogdata#36).
|
||||||
|
|||||||
+13
-1
@@ -28,7 +28,12 @@
|
|||||||
#' every combination would be either redundant or empty.
|
#' every combination would be either redundant or empty.
|
||||||
#' `category = "Fund Balances"` is exactly the `general` family
|
#' `category = "Fund Balances"` is exactly the `general` family
|
||||||
#' (`W01`/`W31`/`W61`). `balance_subtype` is returned, so a finer split is
|
#' (`W01`/`W31`/`W61`). `balance_subtype` is returned, so a finer split is
|
||||||
#' one `dplyr::filter()` away.
|
#' one `dplyr::filter()` away. The reserved pseudo-category
|
||||||
|
#' `"All Categories"` (see [cog_spending()]) is **not** supported here and
|
||||||
|
#' errors with class `uscogdata_all_categories_unsupported`: it sums a
|
||||||
|
#' concept's subtype scope, and holdings are a stock with no concept
|
||||||
|
#' vocabulary to sum across. Omit `category` to get every category broken
|
||||||
|
#' out instead.
|
||||||
#' @param per_capita Divide holdings by population. Note this is a **stock per
|
#' @param per_capita Divide holdings by population. Note this is a **stock per
|
||||||
#' resident** (reserves per person), which is *not* comparable to
|
#' resident** (reserves per person), which is *not* comparable to
|
||||||
#' [cog_spending()]'s per-capita figures -- those are a flow per person.
|
#' [cog_spending()]'s per-capita figures -- those are a flow per person.
|
||||||
@@ -74,6 +79,13 @@ cog_balances <- function(govid, years, category = NULL,
|
|||||||
# helper reuse as .build_verb_sql()/.attach_per_capita() below; it does NOT
|
# helper reuse as .build_verb_sql()/.attach_per_capita() below; it does NOT
|
||||||
# route the verb through .verb_spendrev(), which stays deliberately unused
|
# route the verb through .verb_spendrev(), which stays deliberately unused
|
||||||
# here because its flow vocabulary is meaningless for a stock.
|
# here because its flow vocabulary is meaningless for a stock.
|
||||||
|
#
|
||||||
|
# allow_all_categories is left at its FALSE default (contrast
|
||||||
|
# .verb_spendrev(), which passes TRUE): the all-categories mode's "sum"
|
||||||
|
# only means something in terms of a concept's subtype scope, and holdings
|
||||||
|
# have no concept vocabulary. The reuse above is exactly why this can be a
|
||||||
|
# one-line default rather than a second bespoke check -- see the
|
||||||
|
# validator's own doc comment for the incident that made that matter.
|
||||||
.validate_verb_inputs(govid, years, category, per_capita, adjust_to_year,
|
.validate_verb_inputs(govid, years, category, per_capita, adjust_to_year,
|
||||||
recipe)
|
recipe)
|
||||||
years <- as.integer(years)
|
years <- as.integer(years)
|
||||||
|
|||||||
+28
-2
@@ -22,7 +22,10 @@
|
|||||||
#' `category` column (e.g. `"Police"` or `"Tax"`).
|
#' `category` column (e.g. `"Police"` or `"Tax"`).
|
||||||
#' @return Tibble with columns `category`, `category_type`, `subtype`,
|
#' @return Tibble with columns `category`, `category_type`, `subtype`,
|
||||||
#' `n_codes`, `item_codes` (comma-separated, alphabetical). Sorted by
|
#' `n_codes`, `item_codes` (comma-separated, alphabetical). Sorted by
|
||||||
#' `category_type`, `category`, `subtype`.
|
#' `category_type`, `category`, `subtype`. Includes one row per flow for the
|
||||||
|
#' reserved pseudo-category `"All Categories"`, which carries `NA` for
|
||||||
|
#' `subtype`, `n_codes` and `item_codes` because it is a query mode rather
|
||||||
|
#' than a crosswalk entry — see [cog_spending()]'s `category` argument.
|
||||||
#' @export
|
#' @export
|
||||||
cog_categories <- function(type = NULL, pattern = NULL) {
|
cog_categories <- function(type = NULL, pattern = NULL) {
|
||||||
if (!is.null(type)) {
|
if (!is.null(type)) {
|
||||||
@@ -63,5 +66,28 @@ cog_categories <- function(type = NULL, pattern = NULL) {
|
|||||||
"GROUP BY category, category_type, subtype
|
"GROUP BY category, category_type, subtype
|
||||||
ORDER BY category_type, category, subtype"
|
ORDER BY category_type, category, subtype"
|
||||||
)
|
)
|
||||||
tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
out <- tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
||||||
|
|
||||||
|
# The reserved pseudo-category is a query mode, not a crosswalk row, so it
|
||||||
|
# has no item codes to report -- hence NA rather than 0 for n_codes. It is
|
||||||
|
# emitted for the two FLOW vocabularies only: cog_balances() returns a stock
|
||||||
|
# and has no concept argument to sum within.
|
||||||
|
pseudo <- tibble::tibble(
|
||||||
|
category = .ALL_CATEGORIES,
|
||||||
|
category_type = c("expenditure", "revenue"),
|
||||||
|
subtype = NA_character_,
|
||||||
|
n_codes = NA_integer_,
|
||||||
|
item_codes = NA_character_
|
||||||
|
)
|
||||||
|
if (!is.null(type)) {
|
||||||
|
db_type <- if (type == "spending") "expenditure" else type
|
||||||
|
pseudo <- pseudo[pseudo$category_type == db_type, , drop = FALSE]
|
||||||
|
}
|
||||||
|
if (!is.null(pattern) && nrow(pseudo) > 0L) {
|
||||||
|
keep <- grepl(pattern, pseudo$category, ignore.case = TRUE)
|
||||||
|
pseudo <- pseudo[keep, , drop = FALSE]
|
||||||
|
}
|
||||||
|
if (nrow(pseudo) == 0L) return(out)
|
||||||
|
out <- rbind(out, pseudo)
|
||||||
|
out[order(out$category_type, out$category, out$subtype), , drop = FALSE]
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-1
@@ -5,7 +5,18 @@
|
|||||||
.uscogdata_env <- new.env(parent = emptyenv())
|
.uscogdata_env <- new.env(parent = emptyenv())
|
||||||
|
|
||||||
.uscogdata_defaults <- list(
|
.uscogdata_defaults <- list(
|
||||||
url = "https://cloud.civilytics.org/s/REPLACE_WITH_SHARE_TOKEN/download/",
|
# Public HuggingFace mirror of the published corpus: CC-BY-4.0, no
|
||||||
|
# credential, CDN-backed. This is the default so `library(uscogdata)`
|
||||||
|
# followed by a verb works with zero configuration -- previously the
|
||||||
|
# default was a REPLACE_WITH_SHARE_TOKEN sentinel and no document in the
|
||||||
|
# package supplied a working URL, so a new user had no path to a session.
|
||||||
|
#
|
||||||
|
# The trailing slash is required: every consumer concatenates onto this
|
||||||
|
# (see .resolve_url(), which enforces it anyway).
|
||||||
|
#
|
||||||
|
# Override with USCOGDATA_URL or options(uscogdata.url=) to read a
|
||||||
|
# Nextcloud share or a local copy made by cog_mirror().
|
||||||
|
url = "https://huggingface.co/datasets/civilytics/us-cog-finance/resolve/main/",
|
||||||
cache_dir = NULL,
|
cache_dir = NULL,
|
||||||
manifest_ttl_secs = 3600L
|
manifest_ttl_secs = 3600L
|
||||||
)
|
)
|
||||||
|
|||||||
+32
-1
@@ -11,6 +11,30 @@
|
|||||||
#' returns `result` invisibly for chaining. `"list"` returns the raw
|
#' returns `result` invisibly for chaining. `"list"` returns the raw
|
||||||
#' provenance list (identical to `attr(result, "provenance")`).
|
#' provenance list (identical to `attr(result, "provenance")`).
|
||||||
#' @return Either `result` (invisibly) or the provenance list.
|
#' @return Either `result` (invisibly) or the provenance list.
|
||||||
|
#' @section Two kinds of series break:
|
||||||
|
#' Catalogued breaks reach you without being asked for, in two disjoint
|
||||||
|
#' fields, because a caveat about one series and a caveat about the whole
|
||||||
|
#' corpus are different claims:
|
||||||
|
#'
|
||||||
|
#' * **`series_break_refs`** — breaks matched against the item codes actually
|
||||||
|
#' present in this result. A break in one code you queried.
|
||||||
|
#' * **`corpus_break_refs`** — breaks catalogued with `fin_code = "ALL"`,
|
||||||
|
#' which are statements about the corpus rather than about any one code:
|
||||||
|
#' dollar precision across the 1976/1977 boundary (`SB085`), imputation
|
||||||
|
#' exclusion from FY2002 (`SB087`), the FY2012 dense-to-sparse
|
||||||
|
#' representation change (`SB194`), and the FY2017 government-identifier
|
||||||
|
#' change (`SB086`). These are selected on the break-year window alone.
|
||||||
|
#'
|
||||||
|
#' `SB194` is the one most likely to matter: a query spanning FY2011 to FY2012
|
||||||
|
#' crosses the boundary where an absent cell stops meaning "Census published
|
||||||
|
#' $0" and starts meaning "not reported".
|
||||||
|
#' @section Other provenance blocks:
|
||||||
|
#' `transformations$units_conversion` records the `$1,000s`-to-dollars
|
||||||
|
#' multiply that every amount column has already had applied.
|
||||||
|
#' `transformations$per_capita` records the population denominator and its
|
||||||
|
#' year range. `coverage` and `coverage_mode` appear on multi-government
|
||||||
|
#' results (see [cog_geographic_rollup()]). `completion` appears when
|
||||||
|
#' `complete = TRUE`. `balance_caveats` appears on [cog_balances()] results.
|
||||||
#' @export
|
#' @export
|
||||||
cog_explain <- function(result, format = c("print", "list")) {
|
cog_explain <- function(result, format = c("print", "list")) {
|
||||||
format <- match.arg(format)
|
format <- match.arg(format)
|
||||||
@@ -125,8 +149,15 @@ cog_explain <- function(result, format = c("print", "list")) {
|
|||||||
if (length(prov$suggestions) > 0L) {
|
if (length(prov$suggestions) > 0L) {
|
||||||
cli::cli_h2("Suggestions")
|
cli::cli_h2("Suggestions")
|
||||||
sugg_lines <- vapply(prov$suggestions, function(s) {
|
sugg_lines <- vapply(prov$suggestions, function(s) {
|
||||||
sprintf("%s -- %s (years %s-%s): %s", s$recipe_id, s$label,
|
line <- sprintf("%s -- %s (years %s-%s): %s", s$recipe_id, s$label,
|
||||||
s$available_years[1], s$available_years[2], s$hint)
|
s$available_years[1], s$available_years[2], s$hint)
|
||||||
|
if (isTRUE(s$suppressed_amount > 0)) {
|
||||||
|
line <- paste0(line, sprintf(" [$%s excluded from %s: %s]",
|
||||||
|
formatC(s$suppressed_amount, format = "f", digits = 0, big.mark = ","),
|
||||||
|
paste0("FY", s$suppressed_years, collapse = ", "),
|
||||||
|
paste(s$suppressed_codes, collapse = ", ")))
|
||||||
|
}
|
||||||
|
line
|
||||||
}, character(1))
|
}, character(1))
|
||||||
cli::cli_ul(sugg_lines)
|
cli::cli_ul(sugg_lines)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@
|
|||||||
"*" = "{.code Sys.setenv(USCOGDATA_URL = \"<url-or-local-path>/\")}",
|
"*" = "{.code Sys.setenv(USCOGDATA_URL = \"<url-or-local-path>/\")}",
|
||||||
"*" = "{.code options(uscogdata.url = \"<url-or-local-path>/\")}",
|
"*" = "{.code options(uscogdata.url = \"<url-or-local-path>/\")}",
|
||||||
i = "For an offline smoke test, use the bundled fixture: {.code system.file(\"extdata/fixture_corpus\", package = \"uscogdata\")}.",
|
i = "For an offline smoke test, use the bundled fixture: {.code system.file(\"extdata/fixture_corpus\", package = \"uscogdata\")}.",
|
||||||
i = "For the live Civilytics corpus, request the Nextcloud share URL from the package maintainer."
|
i = "The public corpus is the default: unset USCOGDATA_URL to use it, or point it at a local copy made by {.code cog_mirror()}."
|
||||||
), class = "uscogdata_url_not_configured")
|
), class = "uscogdata_url_not_configured")
|
||||||
}
|
}
|
||||||
invisible(url)
|
invisible(url)
|
||||||
|
|||||||
@@ -240,6 +240,23 @@ cog_find_peers <- function(target_govid,
|
|||||||
#' group_by(year) |>
|
#' group_by(year) |>
|
||||||
#' summarise(p50 = quantile(total, 0.5, na.rm = TRUE))
|
#' summarise(p50 = quantile(total, 0.5, na.rm = TRUE))
|
||||||
#' ```
|
#' ```
|
||||||
|
#' @section Reading `coverage`:
|
||||||
|
#' `provenance$coverage` reports `n_units_reporting` against
|
||||||
|
#' `n_units_expected` per year. **`n_units_reporting` is category-conditional:
|
||||||
|
#' it counts cohort members with rows for the category you asked for, not
|
||||||
|
#' cohort members collected that year.** A government that was surveyed and
|
||||||
|
#' genuinely spends nothing in that category is indistinguishable here from one
|
||||||
|
#' that was never surveyed.
|
||||||
|
#'
|
||||||
|
#' The ratio is therefore **not a response rate** and must not be used as one.
|
||||||
|
#' In FY2022 — a complete census year — Georgia reports 393 of 567 cities for
|
||||||
|
#' `category = "Police"`; the 174-city gap is overwhelmingly cities that
|
||||||
|
#' contract policing to the county sheriff, not non-response.
|
||||||
|
#'
|
||||||
|
#' The comparison that *is* valid is the same category across a census year
|
||||||
|
#' (ending in 2 or 7) and a sample year, where the real-zero component is
|
||||||
|
#' roughly constant and the difference reflects the survey cycle. `is_census_year`
|
||||||
|
#' marks which is which.
|
||||||
#' @export
|
#' @export
|
||||||
cog_peer_compare <- function(target_govid, peers, category, years,
|
cog_peer_compare <- function(target_govid, peers, category, years,
|
||||||
per_capita = TRUE, adjust_to_year = NULL,
|
per_capita = TRUE, adjust_to_year = NULL,
|
||||||
|
|||||||
+9
-1
@@ -67,7 +67,15 @@
|
|||||||
basis_note = basis_note,
|
basis_note = basis_note,
|
||||||
expenditure_concept = expenditure_concept,
|
expenditure_concept = expenditure_concept,
|
||||||
expenditure_concept_note = expenditure_concept_note,
|
expenditure_concept_note = expenditure_concept_note,
|
||||||
expenditure_concept_direct_suppressed = isTRUE(expenditure_concept_direct_suppressed),
|
# isTRUE() alone would collapse a deliberate NA (all-categories mode,
|
||||||
|
# where suppression detection cannot run -- see .verb_spendrev()) down to
|
||||||
|
# FALSE, turning "we don't know" back into the false claim this field
|
||||||
|
# exists to avoid. Preserve NA; otherwise normalize to a strict logical.
|
||||||
|
expenditure_concept_direct_suppressed = if (isTRUE(is.na(expenditure_concept_direct_suppressed))) {
|
||||||
|
NA
|
||||||
|
} else {
|
||||||
|
isTRUE(expenditure_concept_direct_suppressed)
|
||||||
|
},
|
||||||
revenue_concept = revenue_concept,
|
revenue_concept = revenue_concept,
|
||||||
harmonization = harmonization %||% list(
|
harmonization = harmonization %||% list(
|
||||||
applied = FALSE, na_rows_excluded = 0L, na_amount_excluded = 0,
|
applied = FALSE, na_rows_excluded = 0L, na_amount_excluded = 0,
|
||||||
|
|||||||
+15
-2
@@ -8,6 +8,17 @@
|
|||||||
#' multiplies by 1000 and records the conversion in `provenance`).
|
#' multiplies by 1000 and records the conversion in `provenance`).
|
||||||
#'
|
#'
|
||||||
#' @inheritParams cog_spending
|
#' @inheritParams cog_spending
|
||||||
|
#' @param category Character vector of category names (from
|
||||||
|
#' `summary_categories.category`), or `NULL` for all categories broken out
|
||||||
|
#' one row each. The reserved value `"All Categories"` instead returns a
|
||||||
|
#' single summed row per `(year, canonical_govid, subtype)`, covering every
|
||||||
|
#' category inside the requested concept's subtype scope. It cannot be
|
||||||
|
#' combined with other category names, and it is not the same thing as
|
||||||
|
#' `revenue_concept = "total"`: the concept chooses which subtypes are in
|
||||||
|
#' scope, `"All Categories"` chooses whether rows inside that scope are
|
||||||
|
#' broken out or summed. Because the result keeps one row per
|
||||||
|
#' `revenue_subtype`, filtering the returned frame to
|
||||||
|
#' `revenue_subtype == "own_source"` gives an own-source revenue total.
|
||||||
#' @param revenue_concept Which of Census's two published revenue concepts to
|
#' @param revenue_concept Which of Census's two published revenue concepts to
|
||||||
#' return. Concepts are defined as sets of the crosswalk's `revenue_subtype`
|
#' return. Concepts are defined as sets of the crosswalk's `revenue_subtype`
|
||||||
#' values -- never as item-code first letters, which cannot classify
|
#' values -- never as item-code first letters, which cannot classify
|
||||||
@@ -43,7 +54,7 @@ cog_revenue <- function(govid, years, category = NULL,
|
|||||||
per_capita = FALSE, adjust_to_year = NULL,
|
per_capita = FALSE, adjust_to_year = NULL,
|
||||||
basis = c("harmonized", "raw"), recipe = NULL,
|
basis = c("harmonized", "raw"), recipe = NULL,
|
||||||
revenue_concept = c("general", "total"),
|
revenue_concept = c("general", "total"),
|
||||||
complete = FALSE) {
|
complete = FALSE, limit = NULL, offset = NULL) {
|
||||||
# flow_prefixes no longer classifies rows (crosswalk revenue_subtype
|
# flow_prefixes no longer classifies rows (crosswalk revenue_subtype
|
||||||
# membership does -- General Revenue, i.e. everything except
|
# membership does -- General Revenue, i.e. everything except
|
||||||
# insurance_trust) -- it only scopes the recipe-suggestion machinery to
|
# insurance_trust) -- it only scopes the recipe-suggestion machinery to
|
||||||
@@ -62,6 +73,8 @@ cog_revenue <- function(govid, years, category = NULL,
|
|||||||
basis = basis,
|
basis = basis,
|
||||||
recipe = recipe,
|
recipe = recipe,
|
||||||
revenue_concept = revenue_concept,
|
revenue_concept = revenue_concept,
|
||||||
complete = complete
|
complete = complete,
|
||||||
|
limit = limit,
|
||||||
|
offset = offset
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-1
@@ -19,7 +19,11 @@
|
|||||||
#' `state`, `county`, `city`. Each element is a character vector of
|
#' `state`, `county`, `city`. Each element is a character vector of
|
||||||
#' `canonical_govid` values. At least one layer required.
|
#' `canonical_govid` values. At least one layer required.
|
||||||
#' @param category Single category name or character vector (passed through
|
#' @param category Single category name or character vector (passed through
|
||||||
#' to [cog_spending()]).
|
#' to [cog_spending()]), or the reserved `"All Categories"` for one summed
|
||||||
|
#' row per `(year, canonical_govid, subtype)` covering every category in the
|
||||||
|
#' concept's scope. `"All Categories"` is the efficient way to build a
|
||||||
|
#' geographic total: without it a caller must issue one rollup per category
|
||||||
|
#' and sum the results themselves.
|
||||||
#' @param years Integer vector of years.
|
#' @param years Integer vector of years.
|
||||||
#' @param per_capita If `TRUE`, per-capita uses each gov's own per-year
|
#' @param per_capita If `TRUE`, per-capita uses each gov's own per-year
|
||||||
#' population from `gov_population_yearly`. Govs with missing population
|
#' population from `gov_population_yearly`. Govs with missing population
|
||||||
@@ -56,6 +60,23 @@
|
|||||||
#' `codes_included`, `aggregate_fallback`, `scope_note`, `notes`. Carries a
|
#' `codes_included`, `aggregate_fallback`, `scope_note`, `notes`. Carries a
|
||||||
#' `provenance` attribute with `verb = "cog_geographic_rollup"`, `layers`,
|
#' `provenance` attribute with `verb = "cog_geographic_rollup"`, `layers`,
|
||||||
#' and `rollup$included_govids` / `rollup$excluded_govids`.
|
#' and `rollup$included_govids` / `rollup$excluded_govids`.
|
||||||
|
#' @section Reading `coverage`:
|
||||||
|
#' `provenance$coverage` reports `n_units_reporting` against
|
||||||
|
#' `n_units_expected` per year. **`n_units_reporting` is category-conditional:
|
||||||
|
#' it counts governments with rows for the category you asked for, not
|
||||||
|
#' governments collected that year.** A government that was surveyed and
|
||||||
|
#' genuinely spends nothing in that category is indistinguishable here from one
|
||||||
|
#' that was never surveyed.
|
||||||
|
#'
|
||||||
|
#' The ratio is therefore **not a response rate** and must not be used as one.
|
||||||
|
#' In FY2022 — a complete census year — Georgia reports 393 of 567 cities for
|
||||||
|
#' `category = "Police"`; the 174-city gap is overwhelmingly cities that
|
||||||
|
#' contract policing to the county sheriff, not non-response.
|
||||||
|
#'
|
||||||
|
#' The comparison that *is* valid is the same category across a census year
|
||||||
|
#' (ending in 2 or 7) and a sample year, where the real-zero component is
|
||||||
|
#' roughly constant and the difference reflects the survey cycle. `is_census_year`
|
||||||
|
#' marks which is which.
|
||||||
#' @export
|
#' @export
|
||||||
cog_geographic_rollup <- function(govids, category, years,
|
cog_geographic_rollup <- function(govids, category, years,
|
||||||
per_capita = FALSE, adjust_to_year = NULL,
|
per_capita = FALSE, adjust_to_year = NULL,
|
||||||
|
|||||||
+246
-21
@@ -19,6 +19,14 @@
|
|||||||
.spend_subtypes_primary <- c("operations", "capital", "assistance")
|
.spend_subtypes_primary <- c("operations", "capital", "assistance")
|
||||||
.spend_subtypes_direct <- c(.spend_subtypes_primary, "interest", "insurance_benefits")
|
.spend_subtypes_direct <- c(.spend_subtypes_primary, "interest", "insurance_benefits")
|
||||||
|
|
||||||
|
# The reserved pseudo-category. Deliberately NOT "Total": `category = "Total"`
|
||||||
|
# would sit one argument away from `expenditure_concept = "total"` and mean
|
||||||
|
# something different -- the concept selects WHICH SUBTYPES are in scope, this
|
||||||
|
# selects whether the rows inside that scope are broken out by category or
|
||||||
|
# summed. "All Categories" states the operation and cannot be misread as the
|
||||||
|
# concept.
|
||||||
|
.ALL_CATEGORIES <- "All Categories"
|
||||||
|
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.expenditure_concept_subtypes <- function(concept) {
|
.expenditure_concept_subtypes <- function(concept) {
|
||||||
switch(concept,
|
switch(concept,
|
||||||
@@ -63,7 +71,16 @@
|
|||||||
#' @param govid Character vector of `canonical_govid` values.
|
#' @param govid Character vector of `canonical_govid` values.
|
||||||
#' @param years Integer vector of years.
|
#' @param years Integer vector of years.
|
||||||
#' @param category Character vector of category names (from
|
#' @param category Character vector of category names (from
|
||||||
#' `summary_categories.category`), or `NULL` for all categories.
|
#' `summary_categories.category`), or `NULL` for all categories broken out
|
||||||
|
#' one row each. The reserved value `"All Categories"` instead returns a
|
||||||
|
#' single summed row per `(year, canonical_govid, subtype)`, covering every
|
||||||
|
#' category inside the requested concept's subtype scope. It cannot be
|
||||||
|
#' combined with other category names, and it is not the same thing as
|
||||||
|
#' `expenditure_concept = "total"`: the concept chooses which subtypes are in
|
||||||
|
#' scope, `"All Categories"` chooses whether rows inside that scope are
|
||||||
|
#' broken out or summed. Because the result keeps one row per
|
||||||
|
#' `spend_subtype`, filtering the returned frame to
|
||||||
|
#' `spend_subtype == "operations"` gives an operating-expenditure total.
|
||||||
#' @param per_capita If `TRUE`, adds `amt_per_capita_nominal` (and
|
#' @param per_capita If `TRUE`, adds `amt_per_capita_nominal` (and
|
||||||
#' `amt_per_capita_real` when `adjust_to_year` is set) using the per-year
|
#' `amt_per_capita_real` when `adjust_to_year` is set) using the per-year
|
||||||
#' Census F-33 population from `gov_population_yearly`. Result also gains
|
#' Census F-33 population from `gov_population_yearly`. Result also gains
|
||||||
@@ -133,7 +150,12 @@
|
|||||||
#' component (when one exists), and
|
#' component (when one exists), and
|
||||||
#' `provenance$expenditure_concept_direct_suppressed` is `TRUE` -- the
|
#' `provenance$expenditure_concept_direct_suppressed` is `TRUE` -- the
|
||||||
#' figure in those rows is the intergovernmental leg alone, not Direct +
|
#' figure in those rows is the intergovernmental leg alone, not Direct +
|
||||||
#' IG.
|
#' IG. When `category = "All Categories"` is combined with
|
||||||
|
#' `expenditure_concept = "total"`, this detection cannot run (it keys on
|
||||||
|
#' per-category rows, which all-categories mode collapses to one literal
|
||||||
|
#' value), so `expenditure_concept_direct_suppressed` is `NA` rather than a
|
||||||
|
#' possibly-false `FALSE`; query an explicit `category` to get a real
|
||||||
|
#' answer.
|
||||||
#' @param complete If `TRUE`, fill the requested grid so that a cell the
|
#' @param complete If `TRUE`, fill the requested grid so that a cell the
|
||||||
#' corpus does not carry still appears, labelled with **why** it is
|
#' corpus does not carry still appears, labelled with **why** it is
|
||||||
#' missing, and add a `value_source` column to every row:
|
#' missing, and add a `value_source` column to every row:
|
||||||
@@ -156,6 +178,13 @@
|
|||||||
#' `recipe` or with `expenditure_concept = "total"` (class
|
#' `recipe` or with `expenditure_concept = "total"` (class
|
||||||
#' `uscogdata_complete_unsupported`) — neither draws its cells from
|
#' `uscogdata_complete_unsupported`) — neither draws its cells from
|
||||||
#' `code_set`.
|
#' `code_set`.
|
||||||
|
#' @param limit Maximum number of result rows to return, pushed into the SQL
|
||||||
|
#' query itself (`LIMIT`/`OFFSET`) rather than applied after the full
|
||||||
|
#' result is materialized. `NULL` (the default) returns every matching row,
|
||||||
|
#' exactly as before this parameter existed. Mutually exclusive with
|
||||||
|
#' `recipe` and with `complete = TRUE` -- see `offset` and `total_rows`.
|
||||||
|
#' @param offset Rows to skip before `limit` starts counting (0-based).
|
||||||
|
#' Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.
|
||||||
#' @return Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
#' @return Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||||
#' `spend_subtype`, `category`, `amt_nominal`, optional `amt_real`,
|
#' `spend_subtype`, `category`, `amt_nominal`, optional `amt_real`,
|
||||||
#' optional `amt_per_capita_nominal`, optional `amt_per_capita_real`,
|
#' optional `amt_per_capita_nominal`, optional `amt_per_capita_real`,
|
||||||
@@ -163,13 +192,17 @@
|
|||||||
#' and `value_source` when `complete = TRUE`.
|
#' and `value_source` when `complete = TRUE`.
|
||||||
#' Carries a `provenance` attribute matching `inst/schemas/provenance-v1.json`,
|
#' Carries a `provenance` attribute matching `inst/schemas/provenance-v1.json`,
|
||||||
#' whose `completion` block reports `applied`, `rows_filled`, and the
|
#' whose `completion` block reports `applied`, `rows_filled`, and the
|
||||||
#' per-year `absence_means` rule that was applied.
|
#' per-year `absence_means` rule that was applied. When `limit` is set,
|
||||||
|
#' also carries a `total_rows` attribute: the full unpaginated row count,
|
||||||
|
#' computed by the same query (`COUNT(*) OVER()`) rather than a second
|
||||||
|
#' round trip -- so a caller walking pages never has to ask "how many are
|
||||||
|
#' there" separately.
|
||||||
#' @export
|
#' @export
|
||||||
cog_spending <- function(govid, years, category = NULL,
|
cog_spending <- function(govid, years, category = NULL,
|
||||||
per_capita = FALSE, adjust_to_year = NULL,
|
per_capita = FALSE, adjust_to_year = NULL,
|
||||||
basis = c("harmonized", "raw"), recipe = NULL,
|
basis = c("harmonized", "raw"), recipe = NULL,
|
||||||
expenditure_concept = c("primary", "direct", "total"),
|
expenditure_concept = c("primary", "direct", "total"),
|
||||||
complete = FALSE) {
|
complete = FALSE, limit = NULL, offset = NULL) {
|
||||||
# flow_prefixes no longer classifies rows (crosswalk subtype membership
|
# flow_prefixes no longer classifies rows (crosswalk subtype membership
|
||||||
# does, per expenditure_concept) -- it only scopes the recipe-suggestion
|
# does, per expenditure_concept) -- it only scopes the recipe-suggestion
|
||||||
# machinery to this verb's recipe families (see R/suggestions.R; the
|
# machinery to this verb's recipe families (see R/suggestions.R; the
|
||||||
@@ -188,7 +221,9 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
basis = basis,
|
basis = basis,
|
||||||
recipe = recipe,
|
recipe = recipe,
|
||||||
expenditure_concept = expenditure_concept,
|
expenditure_concept = expenditure_concept,
|
||||||
complete = complete
|
complete = complete,
|
||||||
|
limit = limit,
|
||||||
|
offset = offset
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +251,7 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
basis = c("harmonized", "raw"), recipe = NULL,
|
basis = c("harmonized", "raw"), recipe = NULL,
|
||||||
expenditure_concept = c("primary", "direct", "total"),
|
expenditure_concept = c("primary", "direct", "total"),
|
||||||
revenue_concept = c("general", "total"),
|
revenue_concept = c("general", "total"),
|
||||||
complete = FALSE) {
|
complete = FALSE, limit = NULL, offset = NULL) {
|
||||||
basis_explicit <- length(basis) == 1L
|
basis_explicit <- length(basis) == 1L
|
||||||
basis <- match.arg(basis, c("harmonized", "raw"))
|
basis <- match.arg(basis, c("harmonized", "raw"))
|
||||||
# match.arg() itself throws a base `simpleError`, not an rlang-classed
|
# match.arg() itself throws a base `simpleError`, not an rlang-classed
|
||||||
@@ -257,8 +292,24 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
}
|
}
|
||||||
|
|
||||||
govid <- .coerce_govid_input(govid, arg = "govid")
|
govid <- .coerce_govid_input(govid, arg = "govid")
|
||||||
|
# allow_all_categories = TRUE: cog_spending()/cog_revenue() are the two
|
||||||
|
# verbs the reserved pseudo-category is defined for. cog_balances() shares
|
||||||
|
# this validator but leaves the argument at its FALSE default, so it
|
||||||
|
# rejects "All Categories" instead of silently returning zero rows
|
||||||
|
# (finding 3, all-categories review).
|
||||||
.validate_verb_inputs(govid, years, category, per_capita, adjust_to_year,
|
.validate_verb_inputs(govid, years, category, per_capita, adjust_to_year,
|
||||||
recipe)
|
recipe, allow_all_categories = TRUE)
|
||||||
|
|
||||||
|
# Recognize the reserved pseudo-category. Detected after type validation so a
|
||||||
|
# non-character `category` still fails with the ordinary type error.
|
||||||
|
all_categories <- !is.null(category) && .ALL_CATEGORIES %in% category
|
||||||
|
if (all_categories && length(category) > 1L) {
|
||||||
|
cli::cli_abort(c(
|
||||||
|
"{.val {(.ALL_CATEGORIES)}} cannot be combined with other categories.",
|
||||||
|
"i" = "It already sums every category in the requested concept's scope.",
|
||||||
|
"*" = "Ask for it alone, or list the specific categories you want."
|
||||||
|
), class = "uscogdata_all_categories_not_combinable")
|
||||||
|
}
|
||||||
|
|
||||||
if (!is.null(recipe) && identical(expenditure_concept, "total")) {
|
if (!is.null(recipe) && identical(expenditure_concept, "total")) {
|
||||||
cli::cli_abort(c(
|
cli::cli_abort(c(
|
||||||
@@ -299,6 +350,44 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
"Use `expenditure_concept = \"direct\"` with `complete = TRUE`, or drop `complete`."
|
"Use `expenditure_concept = \"direct\"` with `complete = TRUE`, or drop `complete`."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
if (complete && all_categories) {
|
||||||
|
.abort_complete_unsupported(
|
||||||
|
"`category = \"All Categories\"` collapses the category dimension that `code_set` grids over (see `.completion_grid_sql()`), so there is no per-category grid left to fill -- filling a summed row has no defined semantics.",
|
||||||
|
"Drop `complete`, or use `complete = TRUE` with an explicit `category` (or `category = NULL` for every category)."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
# limit/offset push the page into the SQL itself (see .build_verb_sql()),
|
||||||
|
# so the two things that would make "a page of what" ambiguous are refused
|
||||||
|
# up front rather than silently ignored: complete = TRUE fills a grid over
|
||||||
|
# the FULL requested (year, category) space, and a recipe's result comes
|
||||||
|
# from .run_recipe()'s own query, which this function does not touch.
|
||||||
|
if (!is.null(limit)) {
|
||||||
|
limit <- as.integer(limit)
|
||||||
|
if (length(limit) != 1L || is.na(limit) || limit < 0L) {
|
||||||
|
cli::cli_abort("`limit` must be a single non-negative integer.",
|
||||||
|
class = "uscogdata_invalid_pagination")
|
||||||
|
}
|
||||||
|
offset <- if (is.null(offset)) 0L else as.integer(offset)
|
||||||
|
if (length(offset) != 1L || is.na(offset) || offset < 0L) {
|
||||||
|
cli::cli_abort("`offset` must be a single non-negative integer.",
|
||||||
|
class = "uscogdata_invalid_pagination")
|
||||||
|
}
|
||||||
|
if (complete) {
|
||||||
|
cli::cli_abort(c(
|
||||||
|
"`limit`/`offset` cannot be combined with `complete = TRUE`.",
|
||||||
|
"i" = "`complete` fills a grid over the FULL requested (year, category) space; paginating a slice of already-grouped rows has no defined meaning for the cells it would fill.",
|
||||||
|
"*" = "Drop `limit`/`offset`, or drop `complete`."
|
||||||
|
), class = "uscogdata_complete_pagination_conflict")
|
||||||
|
}
|
||||||
|
if (!is.null(recipe)) {
|
||||||
|
cli::cli_abort(c(
|
||||||
|
"`limit`/`offset` cannot be combined with `recipe`.",
|
||||||
|
"i" = "A recipe's result comes from a separate query (`.run_recipe()`) that pagination is not wired into yet.",
|
||||||
|
"*" = "Drop `limit`/`offset`, or drop `recipe`."
|
||||||
|
), class = "uscogdata_recipe_pagination_conflict")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
years <- as.integer(years)
|
years <- as.integer(years)
|
||||||
if (!is.null(adjust_to_year)) adjust_to_year <- as.integer(adjust_to_year)
|
if (!is.null(adjust_to_year)) adjust_to_year <- as.integer(adjust_to_year)
|
||||||
@@ -312,6 +401,7 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
|
|
||||||
recipe_block <- NULL
|
recipe_block <- NULL
|
||||||
category_for_prov <- category
|
category_for_prov <- category
|
||||||
|
total_rows <- NULL # set below only when limit is non-NULL (non-recipe path)
|
||||||
if (!is.null(recipe)) {
|
if (!is.null(recipe)) {
|
||||||
.require_schema_v5(con, manifest, "recipe =")
|
.require_schema_v5(con, manifest, "recipe =")
|
||||||
.validate_recipe_id(con, recipe)
|
.validate_recipe_id(con, recipe)
|
||||||
@@ -333,9 +423,32 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
} else {
|
} else {
|
||||||
NULL
|
NULL
|
||||||
}
|
}
|
||||||
sql <- .build_verb_sql(view, subtype_col, govid, years, category, ig_view,
|
sql <- .build_verb_sql(view, subtype_col, govid, years,
|
||||||
subtype_scope)
|
if (all_categories) NULL else category,
|
||||||
|
ig_view, subtype_scope,
|
||||||
|
all_categories = all_categories,
|
||||||
|
limit = limit, offset = offset)
|
||||||
result <- tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
result <- tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
||||||
|
if (!is.null(limit)) {
|
||||||
|
# COUNT(*) OVER() rides along as an ordinary column so the total comes
|
||||||
|
# from the same scan when this page has any rows -- see
|
||||||
|
# .build_verb_sql(). An empty page (offset past the end) carries no
|
||||||
|
# such row to read it from, so that one case falls back to a second,
|
||||||
|
# unpaginated COUNT(*) query rather than reporting a wrong zero.
|
||||||
|
if (nrow(result) > 0L) {
|
||||||
|
total_rows <- result$pagination_total_rows[[1]]
|
||||||
|
result$pagination_total_rows <- NULL
|
||||||
|
} else {
|
||||||
|
count_sql <- sprintf(
|
||||||
|
"SELECT COUNT(*) AS n FROM (%s) AS _uncounted",
|
||||||
|
.build_verb_sql(view, subtype_col, govid, years,
|
||||||
|
if (all_categories) NULL else category,
|
||||||
|
ig_view, subtype_scope,
|
||||||
|
all_categories = all_categories)
|
||||||
|
)
|
||||||
|
total_rows <- as.integer(DBI::dbGetQuery(con, count_sql)$n[[1]])
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Fill BEFORE per_capita / inflation so the added cells get the same
|
# Fill BEFORE per_capita / inflation so the added cells get the same
|
||||||
@@ -395,7 +508,11 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
}
|
}
|
||||||
suggestions <- .build_suggestions(con, govid, years, category,
|
suggestions <- .build_suggestions(con, govid, years, category,
|
||||||
direct_leg_result,
|
direct_leg_result,
|
||||||
resolved$basis, flow_prefixes)
|
resolved$basis, flow_prefixes,
|
||||||
|
.select_long_view(view_base, resolved$basis),
|
||||||
|
all_categories = all_categories,
|
||||||
|
subtype_col = subtype_col,
|
||||||
|
subtype_scope = subtype_scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
# C1(b): when expenditure_concept = "total", flag any row where the IG
|
# C1(b): when expenditure_concept = "total", flag any row where the IG
|
||||||
@@ -407,13 +524,32 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
# direct spending in that category, which is correct, ordinary data). When
|
# direct spending in that category, which is correct, ordinary data). When
|
||||||
# a covering recipe is found, both the row-level notes and the provenance
|
# a covering recipe is found, both the row-level notes and the provenance
|
||||||
# say so rather than pass silently as a plausible Total.
|
# say so rather than pass silently as a plausible Total.
|
||||||
direct_suppressed_info <- if (identical(expenditure_concept, "total")) {
|
#
|
||||||
|
# In all-categories mode this cannot run at all: .detect_direct_suppressed()
|
||||||
|
# keys on (year, canonical_govid, category), and every row shares the same
|
||||||
|
# literal "All Categories" value, so the key collides across every real
|
||||||
|
# category for that (year, govid) -- an IG-only row for a suppressed
|
||||||
|
# category becomes indistinguishable from one sharing a key with an
|
||||||
|
# unrelated category's ordinary Direct row. `has_direct` would then read
|
||||||
|
# TRUE whenever the government has ANY direct spending at all, and the
|
||||||
|
# detector could never fire. Rather than run it and report a false FALSE,
|
||||||
|
# skip it and record NA -- the provenance must stop making a claim it
|
||||||
|
# cannot support (finding 1, all-categories review).
|
||||||
|
suppression_unavailable <- all_categories &&
|
||||||
|
identical(expenditure_concept, "total")
|
||||||
|
direct_suppressed_info <- if (suppression_unavailable) {
|
||||||
|
list(flag = rep(NA, nrow(result)), notes = rep(NA_character_, nrow(result)))
|
||||||
|
} else if (identical(expenditure_concept, "total")) {
|
||||||
.detect_direct_suppressed(con, result, subtype_col)
|
.detect_direct_suppressed(con, result, subtype_col)
|
||||||
} else {
|
} else {
|
||||||
list(flag = rep(FALSE, nrow(result)), notes = rep(NA_character_, nrow(result)))
|
list(flag = rep(FALSE, nrow(result)), notes = rep(NA_character_, nrow(result)))
|
||||||
}
|
}
|
||||||
direct_suppressed <- direct_suppressed_info$flag
|
direct_suppressed <- direct_suppressed_info$flag
|
||||||
direct_suppressed_flag <- isTRUE(any(direct_suppressed))
|
direct_suppressed_flag <- if (suppression_unavailable) {
|
||||||
|
NA
|
||||||
|
} else {
|
||||||
|
isTRUE(any(direct_suppressed))
|
||||||
|
}
|
||||||
|
|
||||||
result$notes <- .notes_column(result, direct_suppressed_info$notes)
|
result$notes <- .notes_column(result, direct_suppressed_info$notes)
|
||||||
|
|
||||||
@@ -422,9 +558,20 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
# leg is suppressed for at least one requested (year, category), append an
|
# leg is suppressed for at least one requested (year, category), append an
|
||||||
# explicit warning rather than let the base note's "Total = Direct + IG"
|
# explicit warning rather than let the base note's "Total = Direct + IG"
|
||||||
# framing stand unqualified for rows where that arithmetic didn't happen.
|
# framing stand unqualified for rows where that arithmetic didn't happen.
|
||||||
|
# When suppression detection itself is unavailable (all-categories mode),
|
||||||
|
# say so instead of silently reusing the unqualified base note.
|
||||||
expenditure_concept_note_for_prov <- if (identical(expenditure_concept, "total")) {
|
expenditure_concept_note_for_prov <- if (identical(expenditure_concept, "total")) {
|
||||||
base_note <- "Total = Direct + intergovernmental (M to local govts + L to state govts). Legacy-era IG is assembled from aggregate-flagged rows, which are year-disjoint from their modern leaf components; the L-- family total is excluded."
|
base_note <- "Total = Direct + intergovernmental (M to local govts + L to state govts). Legacy-era IG is assembled from aggregate-flagged rows, which are year-disjoint from their modern leaf components; the L-- family total is excluded."
|
||||||
if (direct_suppressed_flag) {
|
if (suppression_unavailable) {
|
||||||
|
paste0(
|
||||||
|
base_note,
|
||||||
|
" NOTE: direct-leg-suppression detection is unavailable when ",
|
||||||
|
"`category = \"All Categories\"` -- it keys on per-category rows, ",
|
||||||
|
"which this mode collapses. `expenditure_concept_direct_suppressed` ",
|
||||||
|
"is NA here rather than a possibly-false FALSE; query an explicit ",
|
||||||
|
"`category` (or `category = NULL`) to get a real answer."
|
||||||
|
)
|
||||||
|
} else if (isTRUE(direct_suppressed_flag)) {
|
||||||
paste0(
|
paste0(
|
||||||
base_note,
|
base_note,
|
||||||
" NOTE: for at least one requested (year, category) the Direct leg ",
|
" NOTE: for at least one requested (year, category) the Direct leg ",
|
||||||
@@ -466,15 +613,32 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
prov$scope$govids_missing <- scope$missing
|
prov$scope$govids_missing <- scope$missing
|
||||||
attr(result, "provenance") <- prov
|
attr(result, "provenance") <- prov
|
||||||
attr(result, ".popyear_range") <- NULL
|
attr(result, ".popyear_range") <- NULL
|
||||||
|
# Attached here, after every downstream transform (per_capita/real-dollar
|
||||||
|
# joins, notes, subtype filtering), the same way provenance is -- an
|
||||||
|
# attribute set before those runs is not guaranteed to survive them.
|
||||||
|
if (!is.null(limit)) attr(result, "total_rows") <- total_rows
|
||||||
|
|
||||||
if (length(suggestions) > 0L) .inform_suggestions(suggestions)
|
if (length(suggestions) > 0L) .inform_suggestions(suggestions)
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#' Shared input validation for the money/holdings verbs.
|
||||||
|
#'
|
||||||
|
#' `allow_all_categories` gates the reserved pseudo-category
|
||||||
|
#' `.ALL_CATEGORIES` ("All Categories"). It is meaningful only where a
|
||||||
|
#' concept's subtype scope defines what "all" sums over --
|
||||||
|
#' `cog_spending()`/`cog_revenue()`, via `.verb_spendrev()`, pass `TRUE`.
|
||||||
|
#' `cog_balances()` leaves it at the `FALSE` default: holdings are a stock
|
||||||
|
#' with no concept vocabulary to sum across (see R/balances.R), and before
|
||||||
|
#' this guard existed `cog_balances(category = "All Categories")` silently
|
||||||
|
#' matched zero crosswalk rows and returned an empty result with no error
|
||||||
|
#' (finding 3, all-categories review). This validator is shared specifically
|
||||||
|
#' so the three verbs cannot drift apart on this again.
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.validate_verb_inputs <- function(govid, years, category,
|
.validate_verb_inputs <- function(govid, years, category,
|
||||||
per_capita, adjust_to_year, recipe = NULL) {
|
per_capita, adjust_to_year, recipe = NULL,
|
||||||
|
allow_all_categories = FALSE) {
|
||||||
if (!is.character(govid) || length(govid) == 0L) {
|
if (!is.character(govid) || length(govid) == 0L) {
|
||||||
cli::cli_abort("`govid` must be a non-empty character vector.")
|
cli::cli_abort("`govid` must be a non-empty character vector.")
|
||||||
}
|
}
|
||||||
@@ -484,6 +648,14 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
if (!is.null(category) && !is.character(category)) {
|
if (!is.null(category) && !is.character(category)) {
|
||||||
cli::cli_abort("`category` must be character or NULL.")
|
cli::cli_abort("`category` must be character or NULL.")
|
||||||
}
|
}
|
||||||
|
if (!allow_all_categories && !is.null(category) &&
|
||||||
|
.ALL_CATEGORIES %in% category) {
|
||||||
|
cli::cli_abort(c(
|
||||||
|
"{.val {(.ALL_CATEGORIES)}} is not supported here.",
|
||||||
|
i = "It sums a spending or revenue concept's subtype scope; this verb has no concept vocabulary to sum across.",
|
||||||
|
i = "Use {.fn cog_spending} or {.fn cog_revenue} for an all-categories total."
|
||||||
|
), class = "uscogdata_all_categories_unsupported")
|
||||||
|
}
|
||||||
if (!is.logical(per_capita) || length(per_capita) != 1L) {
|
if (!is.logical(per_capita) || length(per_capita) != 1L) {
|
||||||
cli::cli_abort("`per_capita` must be a length-1 logical.")
|
cli::cli_abort("`per_capita` must be a length-1 logical.")
|
||||||
}
|
}
|
||||||
@@ -512,6 +684,18 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
if (identical(basis, "harmonized")) paste0(view_base, "_harmonized") else view_base
|
if (identical(basis, "harmonized")) paste0(view_base, "_harmonized") else view_base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#' The `*_long`/`*_long_harmonized` view behind an annotated view base --
|
||||||
|
#' `"spending_annotated"` -> `"spending_long_harmonized"`. `.build_suggestions()`
|
||||||
|
#' anti-joins the LONG view rather than the annotated one: they have identical
|
||||||
|
#' row membership (the annotated views are the long views plus LEFT JOINs, see
|
||||||
|
#' inst/sql/42-spending_annotated_harmonized.sql), but the long view is the
|
||||||
|
#' one that actually owns the `NOT is_aggregate` + crosswalk-membership rule
|
||||||
|
#' the suppression test is asking about.
|
||||||
|
#' @noRd
|
||||||
|
.select_long_view <- function(view_base, basis) {
|
||||||
|
.select_view(sub("_annotated$", "_long", view_base), basis)
|
||||||
|
}
|
||||||
|
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.select_ig_view <- function(basis) {
|
.select_ig_view <- function(basis) {
|
||||||
if (identical(basis, "harmonized")) "ig_annotated_harmonized" else "ig_annotated"
|
if (identical(basis, "harmonized")) "ig_annotated_harmonized" else "ig_annotated"
|
||||||
@@ -557,10 +741,16 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
|
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.build_verb_sql <- function(view, subtype_col, govid, years, category,
|
.build_verb_sql <- function(view, subtype_col, govid, years, category,
|
||||||
ig_view = NULL, subtype_scope = NULL) {
|
ig_view = NULL, subtype_scope = NULL,
|
||||||
|
all_categories = FALSE, limit = NULL, offset = NULL) {
|
||||||
govid_lit <- .sql_lit_chr(govid)
|
govid_lit <- .sql_lit_chr(govid)
|
||||||
years_lit <- paste(as.integer(years), collapse = ",")
|
years_lit <- paste(as.integer(years), collapse = ",")
|
||||||
category_pred <- if (is.null(category)) {
|
# In all-categories mode there is no category filter: the sum is defined by
|
||||||
|
# the concept's SUBTYPE allowlist (subtype_pred below), which is the real
|
||||||
|
# concept boundary. Filtering by category as well would be a no-op at best
|
||||||
|
# and, if the crosswalk ever gained an uncategorized code, a silent
|
||||||
|
# under-count of the very total this mode exists to guarantee.
|
||||||
|
category_pred <- if (all_categories || is.null(category)) {
|
||||||
""
|
""
|
||||||
} else {
|
} else {
|
||||||
sprintf("AND category IN (%s)", .sql_lit_chr(category))
|
sprintf("AND category IN (%s)", .sql_lit_chr(category))
|
||||||
@@ -599,13 +789,26 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
# though its dollars came entirely from an aggregate row, silently
|
# though its dollars came entirely from an aggregate row, silently
|
||||||
# suppressing the "Aggregate fallback applied" note on exactly the rows
|
# suppressing the "Aggregate fallback applied" note on exactly the rows
|
||||||
# this feature exists to surface.
|
# this feature exists to surface.
|
||||||
sprintf(
|
|
||||||
|
# Collapse the category dimension. subtype is deliberately KEPT: it is what
|
||||||
|
# lets a caller filter the result to `spend_subtype == "operations"` and
|
||||||
|
# get an operating-expenditure total, the measure a fiscal comparison
|
||||||
|
# actually wants. (There is no `subtype` argument -- this is a post-hoc
|
||||||
|
# filter on the returned column, not a query parameter.)
|
||||||
|
category_select <- if (all_categories) {
|
||||||
|
sprintf("%s AS category", .sql_lit_chr(.ALL_CATEGORIES))
|
||||||
|
} else {
|
||||||
|
"category"
|
||||||
|
}
|
||||||
|
category_group <- if (all_categories) "" else ", category"
|
||||||
|
|
||||||
|
base_sql <- sprintf(
|
||||||
"SELECT
|
"SELECT
|
||||||
year,
|
year,
|
||||||
canonical_govid,
|
canonical_govid,
|
||||||
COALESCE(xwalk_gov_name, gov_name) AS gov_name,
|
COALESCE(xwalk_gov_name, gov_name) AS gov_name,
|
||||||
%1$s,
|
%1$s,
|
||||||
category,
|
%7$s,
|
||||||
SUM(amt) * 1000.0 AS amt_nominal,
|
SUM(amt) * 1000.0 AS amt_nominal,
|
||||||
string_agg(DISTINCT item_code, ',' ORDER BY item_code) AS codes_included,
|
string_agg(DISTINCT item_code, ',' ORDER BY item_code) AS codes_included,
|
||||||
bool_or(is_aggregate) AS aggregate_fallback
|
bool_or(is_aggregate) AS aggregate_fallback
|
||||||
@@ -614,10 +817,32 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
AND year IN (%4$s)
|
AND year IN (%4$s)
|
||||||
%5$s
|
%5$s
|
||||||
%6$s
|
%6$s
|
||||||
GROUP BY year, canonical_govid, gov_name, xwalk_gov_name, %1$s, category
|
GROUP BY year, canonical_govid, gov_name, xwalk_gov_name, %1$s%8$s
|
||||||
ORDER BY year, canonical_govid, %1$s, category",
|
ORDER BY year, canonical_govid, %1$s%8$s",
|
||||||
subtype_col, source_expr, govid_lit, years_lit, category_pred, subtype_pred
|
subtype_col, source_expr, govid_lit, years_lit, category_pred, subtype_pred,
|
||||||
|
category_select, category_group
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# limit/offset push the page into the query itself instead of pulling every
|
||||||
|
# matching row across the network only to slice and discard most of it
|
||||||
|
# afterward (the pattern behind the 2026-08-06 production incident: a
|
||||||
|
# 193,105-row/194-page sweep re-ran the full query and re-listified every
|
||||||
|
# row on EVERY page). COUNT(*) OVER() rides along as an ordinary column so
|
||||||
|
# the caller gets the true total from this same scan -- see the call site
|
||||||
|
# in .verb_spendrev(), which reads it off row 1 and strips it back out.
|
||||||
|
# The outer SELECT * wrapping (rather than appending LIMIT/OFFSET directly
|
||||||
|
# to base_sql) is what makes COUNT(*) OVER() see the post-GROUP-BY row
|
||||||
|
# count, not the pre-aggregation one.
|
||||||
|
if (is.null(limit)) {
|
||||||
|
base_sql
|
||||||
|
} else {
|
||||||
|
sprintf(
|
||||||
|
"SELECT *, COUNT(*) OVER() AS pagination_total_rows
|
||||||
|
FROM (%s) AS _paged
|
||||||
|
LIMIT %d OFFSET %d",
|
||||||
|
base_sql, limit, offset
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#' @noRd
|
#' @noRd
|
||||||
|
|||||||
+127
-18
@@ -1,8 +1,16 @@
|
|||||||
# R/suggestions.R
|
# R/suggestions.R
|
||||||
# Recipe-component-driven signposting: when a basis = "harmonized" query for
|
# Recipe-component-driven signposting. When a basis = "harmonized" query for
|
||||||
# a category comes back with a coverage gap in some requested years (the
|
# a category comes back incomplete in some requested year -- and a
|
||||||
# result has no rows at all in that year) that a harmonization recipe would
|
# harmonization recipe would actually fill it for this government -- surface
|
||||||
# actually fill for this government, surface that recipe as a suggestion.
|
# that recipe as a suggestion. "Incomplete" has two forms, and a recipe
|
||||||
|
# qualifies on either:
|
||||||
|
# 1. empty_year -- the result has no rows at all in that year.
|
||||||
|
# 2. suppressed_component -- the result HAS rows, but a component code
|
||||||
|
# carries dollars the verb's own long view structurally excludes
|
||||||
|
# (aggregate-published, or absent from summary_categories). This is
|
||||||
|
# uscogdata#9: Public Welfare kept returning E74/E79 rows while dropping
|
||||||
|
# aggregate-only E67/E68, so form 1 never fired and the caller got a
|
||||||
|
# number a third too low with no signpost at all.
|
||||||
#
|
#
|
||||||
# This is deliberately keyed off the recipe catalog's component codes, not
|
# This is deliberately keyed off the recipe catalog's component codes, not
|
||||||
# off harmonization_map rows: no live map row carries a non-blank
|
# off harmonization_map rows: no live map row carries a non-blank
|
||||||
@@ -48,11 +56,39 @@
|
|||||||
#' "D")` for `cog_revenue()` -- see `.verb_spendrev()`). Passed through to
|
#' "D")` for `cog_revenue()` -- see `.verb_spendrev()`). Passed through to
|
||||||
#' `.attach_ig_counterparts()` to keep the intergovernmental-counterpart
|
#' `.attach_ig_counterparts()` to keep the intergovernmental-counterpart
|
||||||
#' lookup scoped to the calling verb's own flow family.
|
#' lookup scoped to the calling verb's own flow family.
|
||||||
|
#' @param long_view Name of the verb's own long view (from
|
||||||
|
#' `.select_long_view()`), passed through to `.suppressed_components()` to
|
||||||
|
#' measure the second qualifying path (uscogdata#9).
|
||||||
|
#' @param all_categories `TRUE` when the caller's `category` is the reserved
|
||||||
|
#' pseudo-category (`.ALL_CATEGORIES`). Defaults to `FALSE` so no other
|
||||||
|
#' caller's behaviour changes. When `TRUE`, the candidate-recipe sub-select
|
||||||
|
#' is scoped by `subtype_col`/`subtype_scope` instead of by `category` --
|
||||||
|
#' symmetric with `.build_verb_sql()`'s own all-categories branch (see
|
||||||
|
#' R/spending.R): the concept's subtype allowlist is the real scope
|
||||||
|
#' boundary, not any literal category value, and
|
||||||
|
#' `.ALL_CATEGORIES` ("All Categories") is never itself a row in
|
||||||
|
#' `summary_categories.category`, so leaving the category-keyed sub-select
|
||||||
|
#' in place here always returned zero candidates and silently disabled
|
||||||
|
#' signposting in all-categories mode (final whole-branch review, finding
|
||||||
|
#' 6).
|
||||||
|
#' @param subtype_col Name of the `summary_categories` subtype column to
|
||||||
|
#' scope by when `all_categories = TRUE` (`"spend_subtype"` or
|
||||||
|
#' `"revenue_subtype"` -- the same value `.build_verb_sql()` already
|
||||||
|
#' receives as its own `subtype_col`). Ignored when `all_categories =
|
||||||
|
#' FALSE`. `NULL` by default.
|
||||||
|
#' @param subtype_scope Character vector of subtype values to scope by when
|
||||||
|
#' `all_categories = TRUE` (the same value `.build_verb_sql()` already
|
||||||
|
#' receives as its own `subtype_scope` -- the concept's subtype allowlist,
|
||||||
|
#' e.g. `.expenditure_concept_subtypes(expenditure_concept)`). Ignored when
|
||||||
|
#' `all_categories = FALSE`. `NULL` by default.
|
||||||
#' @return List of `list(recipe_id, label, available_years, hint,
|
#' @return List of `list(recipe_id, label, available_years, hint,
|
||||||
#' ig_recipe_id)`, possibly empty.
|
#' ig_recipe_id, trigger, suppressed_amount, suppressed_years,
|
||||||
|
#' suppressed_codes)`, possibly empty.
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.build_suggestions <- function(con, govid, years, category, result, basis,
|
.build_suggestions <- function(con, govid, years, category, result, basis,
|
||||||
flow_prefixes) {
|
flow_prefixes, long_view,
|
||||||
|
all_categories = FALSE,
|
||||||
|
subtype_col = NULL, subtype_scope = NULL) {
|
||||||
if (!identical(basis, "harmonized") || is.null(category)) return(list())
|
if (!identical(basis, "harmonized") || is.null(category)) return(list())
|
||||||
|
|
||||||
# Exclude any recipe that is ITSELF an intergovernmental (M/L) recipe --
|
# Exclude any recipe that is ITSELF an intergovernmental (M/L) recipe --
|
||||||
@@ -67,16 +103,35 @@
|
|||||||
# flow-prefix gate below/in `.attach_ig_counterparts()`: an M/L recipe
|
# flow-prefix gate below/in `.attach_ig_counterparts()`: an M/L recipe
|
||||||
# should never be suggested as a coverage-gap filler for EITHER verb, not
|
# should never be suggested as a coverage-gap filler for EITHER verb, not
|
||||||
# just kept from being named as the *counterpart* of another suggestion.
|
# just kept from being named as the *counterpart* of another suggestion.
|
||||||
|
#
|
||||||
|
# The inner sub-select is the concept boundary (finding 6, final
|
||||||
|
# whole-branch review): in all-categories mode it is scoped by
|
||||||
|
# `subtype_col`/`subtype_scope` -- the same allowlist `.build_verb_sql()`
|
||||||
|
# applies as a WHERE predicate to make the summed result a *concept*, not
|
||||||
|
# by `category` (`.ALL_CATEGORIES` is never a row in
|
||||||
|
# `summary_categories.category`, so a category-keyed sub-select always
|
||||||
|
# came back empty here). The M/L exclusion below is unchanged either way.
|
||||||
|
candidate_scope_sql <- if (isTRUE(all_categories)) {
|
||||||
|
sprintf(
|
||||||
|
"SELECT DISTINCT item_code FROM summary_categories WHERE %s IN (%s)",
|
||||||
|
subtype_col, .sql_lit_chr(subtype_scope)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
sprintf(
|
||||||
|
"SELECT DISTINCT item_code FROM summary_categories WHERE category IN (%s)",
|
||||||
|
.sql_lit_chr(category)
|
||||||
|
)
|
||||||
|
}
|
||||||
candidates <- DBI::dbGetQuery(con, sprintf(
|
candidates <- DBI::dbGetQuery(con, sprintf(
|
||||||
"SELECT DISTINCT recipe_id FROM harmonization_recipes
|
"SELECT DISTINCT recipe_id FROM harmonization_recipes
|
||||||
WHERE component_code IN (
|
WHERE component_code IN (
|
||||||
SELECT DISTINCT item_code FROM summary_categories WHERE category IN (%s)
|
%s
|
||||||
)
|
)
|
||||||
AND recipe_id NOT IN (
|
AND recipe_id NOT IN (
|
||||||
SELECT DISTINCT recipe_id FROM harmonization_recipes
|
SELECT DISTINCT recipe_id FROM harmonization_recipes
|
||||||
WHERE LEFT(component_code, 1) IN ('M', 'L')
|
WHERE LEFT(component_code, 1) IN ('M', 'L')
|
||||||
)",
|
)",
|
||||||
.sql_lit_chr(category)
|
candidate_scope_sql
|
||||||
))$recipe_id
|
))$recipe_id
|
||||||
if (length(candidates) == 0L) return(list())
|
if (length(candidates) == 0L) return(list())
|
||||||
|
|
||||||
@@ -86,7 +141,28 @@
|
|||||||
unique(as.integer(result$year))
|
unique(as.integer(result$year))
|
||||||
}
|
}
|
||||||
gap_years <- setdiff(as.integer(years), result_years)
|
gap_years <- setdiff(as.integer(years), result_years)
|
||||||
if (length(gap_years) == 0L) return(list())
|
|
||||||
|
# Path 2 (uscogdata#9): component dollars this government holds that the
|
||||||
|
# verb's own view structurally excludes. Measured across ALL requested
|
||||||
|
# years, not just gap years -- the whole point is that a year with rows can
|
||||||
|
# still be missing dollars. Scoped to the calling verb's own flow_prefixes
|
||||||
|
# (I1) -- see `.suppressed_components()`'s own roxygen for why.
|
||||||
|
#
|
||||||
|
# This runs unconditionally whenever there are candidates -- an earlier
|
||||||
|
# revision of this fix wave tried a free, in-memory pre-check
|
||||||
|
# (`.needs_suppression_query()`) to skip the round trip on an already-
|
||||||
|
# covered path, but a scoped re-review measured it against the fixture and
|
||||||
|
# found it didn't pay for itself (it skipped ~3% of healthy calls, ~0% of
|
||||||
|
# the multi-govid batch shape it was meant to help, at a net cost increase
|
||||||
|
# once its own always-run metadata query was counted) while adding an
|
||||||
|
# untested exactness invariant -- that `result$codes_included` and this
|
||||||
|
# anti-join share the harmonized `item_code` space -- whose silent
|
||||||
|
# violation would kill signposting, the exact failure class uscogdata#9
|
||||||
|
# exists to prevent. Owner's call: keep this simple; a batch-aware
|
||||||
|
# optimization, if one is worth building, is a separate issue.
|
||||||
|
supp <- .suppressed_components(con, candidates, govid, years, long_view, flow_prefixes)
|
||||||
|
|
||||||
|
if (length(gap_years) == 0L && nrow(supp) == 0L) return(list())
|
||||||
|
|
||||||
meta <- tibble::as_tibble(DBI::dbGetQuery(con, sprintf(
|
meta <- tibble::as_tibble(DBI::dbGetQuery(con, sprintf(
|
||||||
"SELECT recipe_id, any_value(label) AS label,
|
"SELECT recipe_id, any_value(label) AS label,
|
||||||
@@ -97,11 +173,12 @@
|
|||||||
.sql_lit_chr(candidates)
|
.sql_lit_chr(candidates)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
# Which (recipe_id, year) pairs the recipe's own generic join actually
|
# Path 1 (unchanged): (recipe, year) pairs the recipe's own generic join
|
||||||
# covers for this government, restricted to the gap years -- the same
|
# covers for this government, restricted to the gap years.
|
||||||
# join .run_recipe() uses (component year_min/year_max + gov_type_scope,
|
covered <- if (length(gap_years) == 0L) {
|
||||||
# no is_aggregate filter), just checking existence instead of summing.
|
data.frame(recipe_id = character(0), year = integer(0))
|
||||||
covered <- DBI::dbGetQuery(con, sprintf(
|
} else {
|
||||||
|
DBI::dbGetQuery(con, sprintf(
|
||||||
"SELECT DISTINCT r.recipe_id, l.year
|
"SELECT DISTINCT r.recipe_id, l.year
|
||||||
FROM long l
|
FROM long l
|
||||||
JOIN harmonization_recipes r
|
JOIN harmonization_recipes r
|
||||||
@@ -116,16 +193,35 @@
|
|||||||
.sql_lit_chr(candidates), .sql_lit_chr(govid),
|
.sql_lit_chr(candidates), .sql_lit_chr(govid),
|
||||||
paste(gap_years, collapse = ",")
|
paste(gap_years, collapse = ",")
|
||||||
))
|
))
|
||||||
|
}
|
||||||
|
|
||||||
suggestions <- list()
|
suggestions <- list()
|
||||||
for (rid in candidates) {
|
for (rid in candidates) {
|
||||||
if (!rid %in% covered$recipe_id) next
|
empty_hit <- rid %in% covered$recipe_id
|
||||||
|
s_rows <- supp[supp$recipe_id == rid, , drop = FALSE]
|
||||||
|
supp_hit <- nrow(s_rows) > 0L
|
||||||
|
if (!empty_hit && !supp_hit) next
|
||||||
m <- meta[meta$recipe_id == rid, ]
|
m <- meta[meta$recipe_id == rid, ]
|
||||||
suggestions[[length(suggestions) + 1L]] <- list(
|
suggestions[[length(suggestions) + 1L]] <- list(
|
||||||
recipe_id = rid,
|
recipe_id = rid,
|
||||||
label = m$label[[1]],
|
label = m$label[[1]],
|
||||||
available_years = c(as.integer(m$year_min), as.integer(m$year_max)),
|
available_years = c(as.integer(m$year_min), as.integer(m$year_max)),
|
||||||
hint = sprintf("re-run with recipe = '%s'", rid)
|
hint = sprintf("re-run with recipe = '%s'", rid),
|
||||||
|
# An empty year is the stronger claim -- the category returned nothing
|
||||||
|
# at all -- so it wins when both paths qualify. The suppressed_* fields
|
||||||
|
# are still populated, so an empty_year fire also reports its dollars.
|
||||||
|
trigger = if (empty_hit) "empty_year" else "suppressed_component",
|
||||||
|
suppressed_amount = if (supp_hit) sum(s_rows$suppressed_amount) else 0,
|
||||||
|
suppressed_years = if (supp_hit) {
|
||||||
|
sort(unique(as.integer(s_rows$year)))
|
||||||
|
} else {
|
||||||
|
integer(0)
|
||||||
|
},
|
||||||
|
suppressed_codes = if (supp_hit) {
|
||||||
|
sort(unique(unlist(strsplit(s_rows$suppressed_codes, ",", fixed = TRUE))))
|
||||||
|
} else {
|
||||||
|
character(0)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.attach_ig_counterparts(con, suggestions, flow_prefixes)
|
.attach_ig_counterparts(con, suggestions, flow_prefixes)
|
||||||
@@ -232,12 +328,25 @@
|
|||||||
#' expressions. When a suggestion has an `ig_recipe_id`, one indented
|
#' expressions. When a suggestion has an `ig_recipe_id`, one indented
|
||||||
#' continuation line is appended naming the intergovernmental counterpart
|
#' continuation line is appended naming the intergovernmental counterpart
|
||||||
#' recipe (embedded `\n` renders as a hanging-indent continuation of the
|
#' recipe (embedded `\n` renders as a hanging-indent continuation of the
|
||||||
#' same bullet under cli, not a new bullet).
|
#' same bullet under cli, not a new bullet). Same treatment for
|
||||||
|
#' `suppressed_amount` (uscogdata#9): only present when dollars were
|
||||||
|
#' actually measured as excluded (an `empty_year` fire can carry them too --
|
||||||
|
#' see `.build_suggestions()` -- so this keys off the amount, not `trigger`).
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.inform_suggestions <- function(suggestions) {
|
.inform_suggestions <- function(suggestions) {
|
||||||
bullets <- vapply(suggestions, function(s) {
|
bullets <- vapply(suggestions, function(s) {
|
||||||
bullet <- sprintf("%s (%d-%d): %s", s$recipe_id,
|
bullet <- sprintf("%s (%d-%d): %s", s$recipe_id,
|
||||||
s$available_years[1], s$available_years[2], s$hint)
|
s$available_years[1], s$available_years[2], s$hint)
|
||||||
|
# Only present when dollars were actually measured as excluded. An
|
||||||
|
# empty_year fire can carry them too -- the year had no rows AND the
|
||||||
|
# component was suppressed -- which is strictly more informative.
|
||||||
|
if (isTRUE(s$suppressed_amount > 0)) {
|
||||||
|
bullet <- paste0(bullet, sprintf(
|
||||||
|
"\n $%s excluded from %s (%s), published as an aggregate or outside the crosswalk",
|
||||||
|
formatC(s$suppressed_amount, format = "f", digits = 0, big.mark = ","),
|
||||||
|
paste0("FY", s$suppressed_years, collapse = ", "),
|
||||||
|
paste(s$suppressed_codes, collapse = ", ")))
|
||||||
|
}
|
||||||
if (!is.null(s$ig_recipe_id)) {
|
if (!is.null(s$ig_recipe_id)) {
|
||||||
bullet <- paste0(bullet, sprintf(
|
bullet <- paste0(bullet, sprintf(
|
||||||
"\n intergovernmental counterpart: recipe = '%s'", s$ig_recipe_id))
|
"\n intergovernmental counterpart: recipe = '%s'", s$ig_recipe_id))
|
||||||
@@ -245,7 +354,7 @@
|
|||||||
bullet
|
bullet
|
||||||
}, character(1))
|
}, character(1))
|
||||||
cli::cli_inform(c(
|
cli::cli_inform(c(
|
||||||
i = "Coverage gap detected for the requested years; a harmonization recipe may fill it:",
|
i = "Incomplete coverage for the requested years; a harmonization recipe may fill it:",
|
||||||
stats::setNames(bullets, rep("*", length(bullets)))
|
stats::setNames(bullets, rep("*", length(bullets)))
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
+115
@@ -0,0 +1,115 @@
|
|||||||
|
# R/suppression.R
|
||||||
|
# Split out of R/suggestions.R (2026-08-05) to keep files under the project's
|
||||||
|
# 400-line limit. Owns the second qualifying path for coverage signposting
|
||||||
|
# (uscogdata#9): measuring, per government, the component dollars the
|
||||||
|
# calling verb's own long view structurally excludes (aggregate-published,
|
||||||
|
# or absent from summary_categories). See R/suggestions.R for the
|
||||||
|
# orchestrator (`.build_suggestions()`) that calls this and the full
|
||||||
|
# uscogdata#9 background.
|
||||||
|
|
||||||
|
#' Measure, per (recipe, year), the component dollars this government holds
|
||||||
|
#' that the calling verb's own long view structurally excludes.
|
||||||
|
#'
|
||||||
|
#' This is the second qualifying path for a suggestion (uscogdata#9). The
|
||||||
|
#' first -- row absence -- only fires when a category returns NOTHING in a
|
||||||
|
#' requested year, which is how Corrections behaves in the wide era. Public
|
||||||
|
#' Welfare is the failure mode it misses: E74/E75/E77/E79 still return rows,
|
||||||
|
#' so there is no absence to detect, while E67/E68 (aggregate-flagged 1967-
|
||||||
|
#' 2011, and absent from `summary_categories` entirely) are dropped. The
|
||||||
|
#' caller gets a plausible number a third too low, silently.
|
||||||
|
#'
|
||||||
|
#' "Structurally excluded" is decided by anti-joining the verb's REAL long
|
||||||
|
#' view rather than restating its WHERE clause, so this stays correct if
|
||||||
|
#' `spending_long_harmonized` / `revenue_long_harmonized` ever change. That
|
||||||
|
#' anti-join is keyed on `item_code`, which is sound only because
|
||||||
|
#' harmonization never renames a recipe component -- asserted by the "no
|
||||||
|
#' recipe component is ever renamed by harmonization" test in
|
||||||
|
#' tests/testthat/test-recipes.R.
|
||||||
|
#'
|
||||||
|
#' Note what this deliberately does NOT count as suppressed: a component
|
||||||
|
#' excluded from the RESULT for scoping reasons -- because it belongs to a
|
||||||
|
#' different `category`, or because `expenditure_concept` narrowed the
|
||||||
|
#' subtypes -- is still present in the view, so it never fires. Suggesting a
|
||||||
|
#' recipe is a coverage fix, not a category redefinition.
|
||||||
|
#'
|
||||||
|
#' `flow_prefixes` (uscogdata#9 review, finding I1) restricts the measured
|
||||||
|
#' components to the CALLING VERB's own flow family (`c("E","F","G")` for
|
||||||
|
#' spending, `c("T","A","U","B","C","D")` for revenue). Without this, a
|
||||||
|
#' candidate recipe belonging to the OTHER flow family is always absent from
|
||||||
|
#' this verb's view (by construction -- `cog_revenue()`'s view never carries
|
||||||
|
#' an E-coded row) and so was always reported as "suppressed", fabricating a
|
||||||
|
#' dollar claim across flow families (`cog_revenue(category = "Corrections")`
|
||||||
|
#' claimed $3.63B excluded that `cog_spending()` reports and fully accounts
|
||||||
|
#' for). Filtering on `LEFT(r.component_code, 1)` also drops M/L-prefixed
|
||||||
|
#' components from measurement under `cog_spending()` (`flow_prefixes` never
|
||||||
|
#' includes "M"/"L") -- harmless today, because a recipe's own M/L components
|
||||||
|
#' (e.g. `corrections_ig_local_combined`'s M04/M05) are present in the view
|
||||||
|
#' in every year they exist and so never fired as suppressed anyway, but
|
||||||
|
#' worth recording since this filter is now the thing relied on to prevent
|
||||||
|
#' it.
|
||||||
|
#'
|
||||||
|
#' @param con Active DuckDB connection.
|
||||||
|
#' @param candidates Character vector of recipe ids to measure.
|
||||||
|
#' @param govid Character vector of canonical_govid values.
|
||||||
|
#' @param years Integer vector of requested years.
|
||||||
|
#' @param long_view Name of the verb's long view, from `.select_long_view()`.
|
||||||
|
#' @param flow_prefixes The calling verb's own flow-type prefixes (see
|
||||||
|
#' `.build_suggestions()`). Only recipe components whose first character is
|
||||||
|
#' in this set are measured.
|
||||||
|
#' @return Tibble of `recipe_id`, `year`, `suppressed_amount` (full US
|
||||||
|
#' dollars), `suppressed_codes` (comma-joined, sorted). Zero rows when
|
||||||
|
#' nothing is suppressed.
|
||||||
|
#' @noRd
|
||||||
|
.suppressed_components <- function(con, candidates, govid, years, long_view,
|
||||||
|
flow_prefixes) {
|
||||||
|
empty <- tibble::tibble(
|
||||||
|
recipe_id = character(0), year = numeric(0),
|
||||||
|
suppressed_amount = numeric(0), suppressed_codes = character(0)
|
||||||
|
)
|
||||||
|
if (length(candidates) == 0L) return(empty)
|
||||||
|
|
||||||
|
# long_view is interpolated as a SQL IDENTIFIER, not a literal, so it can
|
||||||
|
# never be quoted safely. It is always internally derived from a fixed
|
||||||
|
# view_base, so an off-allowlist value is a programming error, not input.
|
||||||
|
if (!long_view %in% c("spending_long", "spending_long_harmonized",
|
||||||
|
"revenue_long", "revenue_long_harmonized")) {
|
||||||
|
cli::cli_abort(
|
||||||
|
"Internal error: unexpected `long_view` {.val {long_view}}.",
|
||||||
|
class = "uscogdata_internal_error"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
sql <- sprintf(
|
||||||
|
"SELECT r.recipe_id,
|
||||||
|
l.year,
|
||||||
|
SUM(l.amt) * 1000.0 AS suppressed_amount,
|
||||||
|
string_agg(DISTINCT l.item_code, ',' ORDER BY l.item_code)
|
||||||
|
AS suppressed_codes
|
||||||
|
FROM long l
|
||||||
|
JOIN harmonization_recipes r
|
||||||
|
ON l.item_code = r.component_code
|
||||||
|
AND l.year BETWEEN r.year_min AND r.year_max
|
||||||
|
AND (r.gov_type_scope = 'all'
|
||||||
|
OR (r.gov_type_scope = 'state' AND l.type = 0)
|
||||||
|
OR (r.gov_type_scope = 'local' AND l.type BETWEEN 1 AND 3))
|
||||||
|
WHERE r.recipe_id IN (%1$s)
|
||||||
|
AND l.canonical_govid IN (%2$s)
|
||||||
|
AND l.year IN (%3$s)
|
||||||
|
AND l.amt <> 0
|
||||||
|
AND LEFT(r.component_code, 1) IN (%5$s)
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM %4$s v
|
||||||
|
WHERE v.canonical_govid = l.canonical_govid
|
||||||
|
AND v.year = l.year
|
||||||
|
AND v.item_code = l.item_code
|
||||||
|
AND v.year IN (%3$s) -- restated: enables partition pruning (I3a)
|
||||||
|
AND v.canonical_govid IN (%2$s) -- restated: pushes the govid filter (I3a)
|
||||||
|
)
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2",
|
||||||
|
.sql_lit_chr(candidates), .sql_lit_chr(govid),
|
||||||
|
paste(as.integer(years), collapse = ","), long_view,
|
||||||
|
.sql_lit_chr(flow_prefixes)
|
||||||
|
)
|
||||||
|
tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
||||||
|
}
|
||||||
@@ -78,6 +78,55 @@
|
|||||||
file %in% basename(paths)
|
file %in% basename(paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#' Build the SQL path expression for the partitioned `long` table.
|
||||||
|
#'
|
||||||
|
#' DuckDB cannot expand a glob over generic HTTP: there is no directory
|
||||||
|
#' listing to expand against, and `allow_asterisks_in_http_paths` only
|
||||||
|
#' forwards the literal `**/*` as a filename, which 404s. Measured against
|
||||||
|
#' the published corpus on 2026-08-08, an explicit file list returns the
|
||||||
|
#' same 46,148,034 rows the (working) `hf://` glob does, and
|
||||||
|
#' `hive_partitioning = true` still recovers `year` from the paths.
|
||||||
|
#'
|
||||||
|
#' The manifest already enumerates every partition, so we build the list
|
||||||
|
#' from it. This is host-agnostic -- Nextcloud, HuggingFace and a local
|
||||||
|
#' fixture take the same path -- where an `hf://` URL would tie the reader
|
||||||
|
#' to one vendor's protocol and still need special-casing, since manifest
|
||||||
|
#' fetching goes through httr2, which cannot speak `hf://`.
|
||||||
|
#'
|
||||||
|
#' Falls back to the glob when the manifest carries no partition list: a
|
||||||
|
#' hand-built manifest in a test (see test-views.R) or a corpus predating
|
||||||
|
#' the field. Both are local, where globbing works.
|
||||||
|
#' @noRd
|
||||||
|
.long_files_sql <- function(url, manifest) {
|
||||||
|
parts <- manifest$files$long_partitions %||% list()
|
||||||
|
if (length(parts) == 0L) {
|
||||||
|
return(.sql_lit_chr(paste0(url, "data/long/**/*.parquet")))
|
||||||
|
}
|
||||||
|
paths <- vapply(parts, function(p) as.character(p$path), character(1))
|
||||||
|
paste0("[", .sql_lit_chr(paste0(url, paths)), "]")
|
||||||
|
}
|
||||||
|
|
||||||
|
#' Substitute the corpus-location tokens in a view's SQL text.
|
||||||
|
#'
|
||||||
|
#' One place knows the token vocabulary. `.register_views()` and the tests
|
||||||
|
#' that execute a view file directly both route through here. This exists
|
||||||
|
#' because four test sites had hand-rolled the `{url}` substitution -- one
|
||||||
|
#' of them commented as doing it "exactly as .register_views() does" -- and
|
||||||
|
#' every one of them broke the moment a second token was introduced.
|
||||||
|
#'
|
||||||
|
#' `{long_files}` must be substituted BEFORE `{url}`: it expands to a string
|
||||||
|
#' that itself contains the url, so the reverse order leaves the token in
|
||||||
|
#' place and DuckDB's parser fails on the brace.
|
||||||
|
#'
|
||||||
|
#' `manifest` defaults to empty, which routes `.long_files_sql()` to its glob
|
||||||
|
#' fallback -- correct for the local temp corpora the direct-execution tests
|
||||||
|
#' build.
|
||||||
|
#' @noRd
|
||||||
|
.render_view_sql <- function(sql, url, manifest = list()) {
|
||||||
|
sql <- gsub("\\{long_files\\}", .long_files_sql(url, manifest), sql, fixed = FALSE)
|
||||||
|
gsub("\\{url\\}", url, sql, fixed = FALSE)
|
||||||
|
}
|
||||||
|
|
||||||
#' Register DuckDB views from inst/sql/ SQL files
|
#' Register DuckDB views from inst/sql/ SQL files
|
||||||
#' @noRd
|
#' @noRd
|
||||||
.register_views <- function(con, url, manifest) {
|
.register_views <- function(con, url, manifest) {
|
||||||
@@ -91,7 +140,7 @@
|
|||||||
!.corpus_has_table(manifest, .representation_view_files[[base]])) next
|
!.corpus_has_table(manifest, .representation_view_files[[base]])) next
|
||||||
if (base %in% .balance_view_files && !.corpus_has_balance_subtype(con)) next
|
if (base %in% .balance_view_files && !.corpus_has_balance_subtype(con)) next
|
||||||
sql <- paste(readLines(f, warn = FALSE), collapse = "\n")
|
sql <- paste(readLines(f, warn = FALSE), collapse = "\n")
|
||||||
sql <- gsub("\\{url\\}", url, sql, fixed = FALSE)
|
sql <- .render_view_sql(sql, url, manifest)
|
||||||
DBI::dbExecute(con, sql)
|
DBI::dbExecute(con, sql)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,116 @@
|
|||||||
# uscogdata
|
# uscogdata
|
||||||
|
|
||||||
Curated R reader for the Civilytics US Census of Governments finance corpus.
|
<!-- badges: start -->
|
||||||
|
[](https://civilytics.r-universe.dev/uscogdata)
|
||||||
|
[](LICENSE.md)
|
||||||
|
<!-- badges: end -->
|
||||||
|
|
||||||
Provides unit-level financial profiles, geographic rollups, and peer comparisons
|
A curated R reader for the Civilytics US Census of Governments finance corpus —
|
||||||
with auditable provenance and built-in cross-vintage correctness. Reads the
|
every dollar that US state, county, municipal and township governments reported
|
||||||
published corpus (Hive-partitioned parquet + manifest.json) directly from
|
raising and spending, from **FY1967 to FY2024**, in one queryable place.
|
||||||
Nextcloud via DuckDB httpfs — no local bulk downloads required.
|
|
||||||
|
|
||||||
## Status
|
The Census of Governments is the only nationwide source for local government
|
||||||
|
finance, and it is hard to use: item codes change meaning across vintages,
|
||||||
|
government identifiers were renumbered in 2017, and an absent value means
|
||||||
|
"published zero" in one era and "not reported" in the next. This package
|
||||||
|
handles each of those problems, and it tells you when it has — every result
|
||||||
|
carries provenance describing what was converted, what was aggregated, and
|
||||||
|
which known series breaks intersect your query.
|
||||||
|
|
||||||
Under active development (Phase 2 of the cog_pipeline project). See
|
**Scope:** government types 0–3 (state, county, municipality, township).
|
||||||
`../cog_pipeline/docs/reader-specification.md` for the reader contract this
|
56 fiscal years, 46,148,034 rows, 190.6 MB. There is no source data for FY1968
|
||||||
package implements.
|
or FY1969. Special districts (type 4) and school districts (type 5) are
|
||||||
|
excluded pending validation.
|
||||||
|
|
||||||
## Installation
|
## Where the data comes from
|
||||||
|
|
||||||
|
The corpus is published and documented at the **[US Census of Governments
|
||||||
|
Finance API](https://pages.civilytics.org/cog-api/)**. Start there for how the
|
||||||
|
data was built, how the identifier and item-code reconciliation works, and what
|
||||||
|
the corpus does and does not cover.
|
||||||
|
|
||||||
|
- **[API documentation and walkthroughs](https://pages.civilytics.org/cog-api/)**
|
||||||
|
— reference, data dictionary, and worked examples such as the
|
||||||
|
[Southern states guide](https://pages.civilytics.org/cog-api/cog-api-south-guide.html)
|
||||||
|
- **[Live API](https://cog-api.civilytics.org/api/v1/)** — the same corpus over
|
||||||
|
HTTP, for Tableau, Python, or anything that isn't R
|
||||||
|
- **[Bulk corpus on Hugging Face](https://huggingface.co/datasets/civilytics/us-cog-finance)**
|
||||||
|
— CC-BY-4.0; the same parquet files this package reads
|
||||||
|
- **[Census Bureau source data](https://www.census.gov/programs-surveys/gov-finances.html)**
|
||||||
|
— the underlying public files
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
```r
|
```r
|
||||||
# pak::pkg_install("gitea.civilytics.org/Civilytics/uscogdata")
|
install.packages("uscogdata",
|
||||||
|
repos = c("https://civilytics.r-universe.dev",
|
||||||
|
"https://cloud.r-project.org"))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Or from source:
|
||||||
|
|
||||||
|
```r
|
||||||
|
pak::pkg_install("git::https://gitea.civilytics.org/Civilytics/uscogdata.git")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Quickstart
|
||||||
|
|
||||||
|
No configuration, no credentials, no download. The package reads the published
|
||||||
|
corpus over HTTPS by default.
|
||||||
|
|
||||||
|
```r
|
||||||
|
library(uscogdata)
|
||||||
|
|
||||||
|
# Resolve a place name to a canonical government id
|
||||||
|
madison <- cog_gov_search(name = "Madison", state = "WI", type = 2)
|
||||||
|
madison$canonical_govid
|
||||||
|
#> [1] "552025209777"
|
||||||
|
|
||||||
|
# Police spending, inflation-adjusted and per capita
|
||||||
|
spend <- cog_spending(
|
||||||
|
madison$canonical_govid,
|
||||||
|
years = 2012:2022,
|
||||||
|
category = "Police",
|
||||||
|
per_capita = TRUE,
|
||||||
|
adjust_to_year = 2023
|
||||||
|
)
|
||||||
|
|
||||||
|
# What did that result do to the numbers, and what should you know about them?
|
||||||
|
cog_explain(spend)
|
||||||
|
```
|
||||||
|
|
||||||
|
`years` is required — there is no implicit full-history default.
|
||||||
|
|
||||||
|
## Two ways to read the corpus
|
||||||
|
|
||||||
|
| | Remote (default) | Mirrored |
|
||||||
|
|---|---|---|
|
||||||
|
| Setup | none | `cog_mirror(dest)`, 190.6 MB once |
|
||||||
|
| Disk used | **0 MB** — HTTP range requests only | 190.6 MB |
|
||||||
|
| Per query | ~4 s (one government, one year)<br>~6 s (one government, 23 years) | local speed |
|
||||||
|
| Good for | trying it out, teaching, one-off questions | repeated analysis, offline work, reproducibility |
|
||||||
|
|
||||||
|
Nothing is written to disk in remote mode: DuckDB fetches the parquet footer,
|
||||||
|
works out which row groups it needs, and reads only those. Nothing is cached
|
||||||
|
between sessions either, so every query goes back to the network.
|
||||||
|
|
||||||
|
The default points at a public HuggingFace mirror of the corpus. If you would
|
||||||
|
rather not depend on a third party — for reproducibility, for an air-gapped
|
||||||
|
environment, or on principle — **the escape hatch is one function call**:
|
||||||
|
|
||||||
|
```r
|
||||||
|
cog_mirror("~/cog-corpus")
|
||||||
|
Sys.setenv(USCOGDATA_URL = "~/cog-corpus/")
|
||||||
|
```
|
||||||
|
|
||||||
|
After that, nothing in your analysis touches an external service.
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
- `USCOGDATA_URL` — corpus root: an HTTPS URL or a local path, **trailing slash required**
|
||||||
|
- `USCOGDATA_CACHE_DIR` — where the manifest is cached (default: user cache dir)
|
||||||
|
- `USCOGDATA_MANIFEST_TTL_SECS` — manifest re-fetch interval (default 3600)
|
||||||
|
|
||||||
## Amounts are in full US dollars
|
## Amounts are in full US dollars
|
||||||
|
|
||||||
Every amount column this package returns — `amt_nominal`, `amt_real`,
|
Every amount column this package returns — `amt_nominal`, `amt_real`,
|
||||||
@@ -29,111 +121,127 @@ own `amt` column preserves that. The verbs multiply by 1000 on the way out, so
|
|||||||
you never have to. The conversion is recorded in every result:
|
you never have to. The conversion is recorded in every result:
|
||||||
|
|
||||||
```r
|
```r
|
||||||
r <- cog_spending("552025209777", 2020L)
|
attr(spend, "provenance")$transformations$units_conversion
|
||||||
attr(r, "provenance")$transformations$units_conversion
|
#> $applied TRUE
|
||||||
#> $applied TRUE $source_unit "$1,000s (raw Census)" $target_unit "$USD" $multiplier 1000
|
#> $source_unit "$1,000s (raw Census)"
|
||||||
|
#> $target_unit "$USD"
|
||||||
|
#> $multiplier 1000
|
||||||
```
|
```
|
||||||
|
|
||||||
**Do not multiply again.** If you have read elsewhere that COG amounts are in
|
**Do not multiply again.** If you have read elsewhere that COG amounts are in
|
||||||
`$1,000s` — true of the raw corpus, and of `cog_explorer`'s conventions doc —
|
`$1,000s` — which is true of the raw Census files and of the corpus's own `amt`
|
||||||
that rule does not apply to anything a `cog_*()` verb hands you. Applying it
|
column — that rule does not apply to anything a `cog_*()` verb hands you.
|
||||||
twice overstates every figure by 1000x, and the result looks plausible rather
|
Applying it twice overstates every figure by 1000x, and the result looks
|
||||||
than obviously wrong.
|
plausible rather than obviously wrong.
|
||||||
|
|
||||||
## Configuration
|
## Concepts worth understanding before you publish a number
|
||||||
|
|
||||||
- `USCOGDATA_URL` — corpus root URL (public Nextcloud share, trailing slash)
|
### Primary vs Direct vs Total spending
|
||||||
- `USCOGDATA_CACHE_DIR` — optional override for the manifest cache directory
|
|
||||||
- `USCOGDATA_MANIFEST_TTL_SECS` — optional manifest re-fetch TTL (default 3600)
|
|
||||||
|
|
||||||
## Primary vs Direct vs Total spending
|
|
||||||
|
|
||||||
`cog_spending(..., expenditure_concept = c("primary", "direct", "total"))`
|
`cog_spending(..., expenditure_concept = c("primary", "direct", "total"))`
|
||||||
controls whose spending a result counts. Concepts are defined as sets of the
|
controls *whose* spending a result counts. Concepts are defined as sets of the
|
||||||
crosswalk's `spend_subtype` values — never item-code first letters, which
|
crosswalk's `spend_subtype` values, never item-code first letters — the letter
|
||||||
cannot classify correctly (the letter `Y` alone spans revenue, expenditure,
|
`Y` alone spans revenue, expenditure and balance codes.
|
||||||
and balance codes):
|
|
||||||
|
|
||||||
- `"primary"` (the default) is the government's own service provision:
|
- **`"primary"`** (default) — the government's own service provision: current
|
||||||
current operations, capital outlay, and assistance payments.
|
operations, capital outlay, assistance payments.
|
||||||
- `"direct"` is Census's published Direct Expenditure: `primary` plus
|
- **`"direct"`** — Census's published Direct Expenditure: `primary` plus
|
||||||
interest on debt and insurance trust benefit payments (e.g. pensions).
|
interest on debt and insurance trust benefits (e.g. pensions).
|
||||||
- `"total"` additionally adds the intergovernmental leg — money handed to
|
- **`"total"`** — adds the intergovernmental leg, money handed to other
|
||||||
other governments to spend (`M`/`L` codes plus `Q11`/`Q12`/`Q18` state
|
governments to spend. Meaningful for one government's own budget over time,
|
||||||
payments to school systems) — which is meaningful for describing one
|
but it double-counts when summed across governments: a state's payment to a
|
||||||
government's own budget over time, but double-counts when summed across
|
county is the same dollar the county reports as its own direct spending.
|
||||||
governments (a state's payment to a county is the same dollar the county
|
|
||||||
reports as its own direct spending).
|
|
||||||
|
|
||||||
**Rule of thumb: any figure that spans more than one government uses
|
**Rule of thumb: any figure spanning more than one government uses `primary`
|
||||||
`primary` or `direct`.** `cog_geographic_rollup()` and `cog_peer_compare()`
|
or `direct`.** `cog_geographic_rollup()` and `cog_peer_compare()` enforce that
|
||||||
enforce this by refusing `expenditure_concept = "total"`. See
|
by refusing `"total"` outright. Worked examples in
|
||||||
`vignette("total-spending", package = "uscogdata")` for the full
|
`vignette("total-spending", package = "uscogdata")`.
|
||||||
explanation with worked examples.
|
|
||||||
|
|
||||||
## General vs Total revenue
|
### General vs Total revenue
|
||||||
|
|
||||||
`cog_revenue(..., revenue_concept = c("general", "total"))` selects between
|
`cog_revenue(..., revenue_concept = c("general", "total"))`:
|
||||||
Census's two published revenue concepts, again defined as crosswalk
|
|
||||||
`revenue_subtype` sets rather than item-code prefixes:
|
|
||||||
|
|
||||||
- `"general"` (the default) is Census **General Revenue**: own-source
|
- **`"general"`** (default) — Census General Revenue: own-source taxes,
|
||||||
(taxes, charges, miscellaneous) plus federal, state and local
|
charges and miscellaneous, plus federal, state and local aid.
|
||||||
intergovernmental aid.
|
- **`"total"`** — General plus utility revenue (`A91`–`A94`), liquor store
|
||||||
- `"total"` is Census **Total Revenue**: `general` plus utility revenue
|
revenue (`A90`), and insurance trust revenue.
|
||||||
(`A91`–`A94`), liquor store revenue (`A90`), and insurance trust revenue
|
|
||||||
(unemployment and workers' compensation `Y` codes plus the
|
|
||||||
employee-retirement `X` codes).
|
|
||||||
|
|
||||||
The manual defines the first by subtracting the other three from the second,
|
Census defines these by its own identity:
|
||||||
so the two are related by Census's own identity:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
Total Revenue = General + Utility + Liquor Store + Insurance Trust
|
Total Revenue = General + Utility + Liquor Store + Insurance Trust
|
||||||
```
|
```
|
||||||
|
|
||||||
Two things worth knowing before switching to `"total"`:
|
Two things to know before switching to `"total"`. **Utility revenue is large
|
||||||
|
for cities** — measured on the bundled fixture, utility plus liquor store is
|
||||||
|
15.9% of city revenue, against 1.2% for states and 1.7% for counties. And the
|
||||||
|
**employee-retirement (`X`) codes stop at FY2016**, when those systems moved to
|
||||||
|
the separate Annual Survey of Public Pensions, so a `"total"` series steps down
|
||||||
|
at the FY2016/FY2017 boundary for reasons of collection scope, not revenue
|
||||||
|
(series breaks `SB197`–`SB209`).
|
||||||
|
|
||||||
- **Utility revenue is large for cities.** Measured on the bundled fixture,
|
### Reporting coverage: the Census is only sometimes a census
|
||||||
utility plus liquor store revenue is 15.9% of city (type 2) revenue, versus
|
|
||||||
1.2% for states and 1.7% for counties. `general` excludes it by definition.
|
|
||||||
- **The employee-retirement (`X`) codes stop at FY2016**, when those systems
|
|
||||||
moved out of the annual finance file into the separate Annual Survey of
|
|
||||||
Public Pensions. A `"total"` series therefore steps down at the
|
|
||||||
FY2016/FY2017 seam for reasons of collection scope, not revenue (series
|
|
||||||
breaks `SB197`–`SB202`, in the corpus's `series_breaks` table).
|
|
||||||
|
|
||||||
## Developer notes
|
**The Census of Governments is a complete enumeration only in years ending in
|
||||||
|
2 and 7.** Every other year is a sample, and the sample varies enormously —
|
||||||
|
measured on the bundled fixture, Wisconsin's 608-city universe rolls up 597
|
||||||
|
governments in FY2012 and 112 in FY2019.
|
||||||
|
|
||||||
### Testing
|
A statewide total resting on a fifth of the universe looks exactly like one
|
||||||
|
resting on all of it, so every multi-government result now says which it is:
|
||||||
The package ships a bundled fixture corpus at `inst/extdata/fixture_corpus/` —
|
|
||||||
a 15 MB four-year slice (2011, 2012, 2019, 2020) of the full corpus covering
|
|
||||||
all 50 states. `tests/testthat/setup.R` automatically points `USCOGDATA_URL`
|
|
||||||
at this fixture, so the full test suite runs offline with no network
|
|
||||||
dependency:
|
|
||||||
|
|
||||||
```r
|
```r
|
||||||
devtools::test() # uses bundled fixture, no credentials required
|
attr(rollup, "provenance")$coverage # per-year n_units_reporting, is_census_year
|
||||||
```
|
```
|
||||||
|
|
||||||
### Releasing against the live corpus
|
`cog_geographic_rollup()`, `cog_peer_compare()` and `cog_find_peers()` take a
|
||||||
|
`coverage` argument — `"all"` (default), `"census"` (census years only), or
|
||||||
|
`"consistent"` (only units reporting in every requested year, a balanced
|
||||||
|
panel).
|
||||||
|
|
||||||
Before cutting a release, run the test suite against the published corpus to
|
`n_units_reporting` is **category-conditional**, and it is not a response rate. A government that was surveyed and genuinely spends
|
||||||
catch any drift between the fixture and the real data:
|
nothing in the requested category is indistinguishable from one never surveyed.
|
||||||
|
|
||||||
|
### Absent cells mean two different things
|
||||||
|
|
||||||
|
Before FY2012, an absent cell means Census published `$0`. From FY2012 on, it
|
||||||
|
means not reported. `cog_spending(..., complete = TRUE)` fills the requested
|
||||||
|
grid and labels every row with which it is, via `value_source`:
|
||||||
|
|
||||||
|
| `value_source` | meaning | `amt_nominal` |
|
||||||
|
|---|---|---|
|
||||||
|
| `reported` | the corpus carries this cell | as published |
|
||||||
|
| `census_zero` | dense-source year (≤ FY2011), absent — Census published `$0` | `0` |
|
||||||
|
| `not_reported` | sparse-source year (≥ FY2012), absent — unknown | `NA` |
|
||||||
|
|
||||||
|
That `NA` is deliberate. Filling a modern absence with `0` would invent data.
|
||||||
|
|
||||||
|
### Series breaks surface on their own
|
||||||
|
|
||||||
|
Catalogued breaks that intersect your query appear in provenance whether or not
|
||||||
|
you went looking for them — `series_break_refs` for breaks in a specific item code, and
|
||||||
|
`corpus_break_refs` for caveats about the corpus as a whole (dollar precision
|
||||||
|
across the 1976/1977 boundary, the FY2017 identifier change, the FY2012
|
||||||
|
dense→sparse representation change). `cog_explain()` prints both.
|
||||||
|
|
||||||
|
## How to cite
|
||||||
|
|
||||||
```r
|
```r
|
||||||
Sys.setenv(USCOGDATA_URL = "<published-corpus-url-with-trailing-slash>")
|
citation("uscogdata")
|
||||||
devtools::test()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
When the live-corpus run is clean, strip the fixture from the built package by
|
The corpus itself is published under CC-BY-4.0. Cite it as:
|
||||||
adding this line to `.Rbuildignore`:
|
|
||||||
|
|
||||||
```
|
> Civilytics Consulting. US Census of Governments finance corpus.
|
||||||
^inst/extdata/fixture_corpus$
|
> https://huggingface.co/datasets/civilytics/us-cog-finance
|
||||||
```
|
|
||||||
|
|
||||||
The test suite is URL-agnostic — `setup.R` falls back to `USCOGDATA_URL` when
|
## Contributing
|
||||||
the bundled fixture is absent, so no test code changes are needed for the
|
|
||||||
release run or after stripping the fixture.
|
Development happens on [Gitea](https://gitea.civilytics.org/Civilytics/uscogdata);
|
||||||
|
[GitHub](https://github.com/civilytics/uscogdata) is a mirror that accepts
|
||||||
|
issues and pull requests. See [CONTRIBUTING.md](CONTRIBUTING.md) for how a
|
||||||
|
patch gets from there to here.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT © Civilytics Consulting LLC. See [LICENSE.md](LICENSE.md).
|
||||||
|
|||||||
+21
-5
@@ -1,4 +1,5 @@
|
|||||||
url: ~
|
url: https://civilytics.r-universe.dev/uscogdata
|
||||||
|
|
||||||
template:
|
template:
|
||||||
bootstrap: 5
|
bootstrap: 5
|
||||||
|
|
||||||
@@ -15,11 +16,26 @@ reference:
|
|||||||
- cog_gov_search
|
- cog_gov_search
|
||||||
- cog_basket_resolution
|
- cog_basket_resolution
|
||||||
- cog_basket_unresolved
|
- cog_basket_unresolved
|
||||||
- title: Session
|
- title: Comparison & aggregation
|
||||||
|
desc: Peer cohorts and geographic aggregates.
|
||||||
contents:
|
contents:
|
||||||
- has_keyword("internal")
|
- cog_find_peers
|
||||||
|
- cog_peer_compare
|
||||||
|
- cog_geographic_rollup
|
||||||
|
- title: Corpus metadata
|
||||||
|
desc: >
|
||||||
|
What the corpus contains, where a given result came from, and how to
|
||||||
|
hold a local copy of it.
|
||||||
|
contents:
|
||||||
|
- cog_categories
|
||||||
|
- cog_recipes
|
||||||
|
- cog_manifest
|
||||||
|
- cog_explain
|
||||||
|
- cog_mirror
|
||||||
|
|
||||||
articles:
|
articles:
|
||||||
- title: Getting started
|
- title: Concepts
|
||||||
navbar: ~
|
navbar: ~
|
||||||
contents: []
|
contents:
|
||||||
|
- total-spending
|
||||||
|
- population-denominators
|
||||||
|
|||||||
Binary file not shown.
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"schema_version": 6,
|
"schema_version": 6,
|
||||||
"built_at": "2026-07-31T00:47:27Z",
|
"built_at": "2026-08-03T16:51:32Z",
|
||||||
"pipeline_commit": "aadb46b",
|
"pipeline_commit": "e7394a4",
|
||||||
"fixture_note": "Four-year (2011, 2012, 2019, 2020) fixture for uscogdata tests. Full corpus available via USCOGDATA_URL. Regenerated from the sparsified schema-v6 corpus: the wide era (<= FY2011) no longer stores explicit zeros, so FY2011 absence means Census published $0 while FY2012+ absence means not reported. representation.parquet and code_set.parquet carry that rule and ship in full, as do every other metadata table in the publish tree. 2011/2012 straddle both the wide-aggregate -> modern-leaf format boundary (exercised by basis=\"harmonized\" and recipe= queries) and the dense -> sparse representation boundary (SB194); 2019/2020 retain the prior per-capita/CPI regression anchors. Regenerated via data-raw/regenerate_fixture_corpus.R.",
|
"fixture_note": "Four-year (2011, 2012, 2019, 2020) fixture for uscogdata tests. Full corpus available via USCOGDATA_URL. Regenerated from the sparsified schema-v6 corpus: the wide era (<= FY2011) no longer stores explicit zeros, so FY2011 absence means Census published $0 while FY2012+ absence means not reported. representation.parquet and code_set.parquet carry that rule and ship in full, as do every other metadata table in the publish tree. 2011/2012 straddle both the wide-aggregate -> modern-leaf format boundary (exercised by basis=\"harmonized\" and recipe= queries) and the dense -> sparse representation boundary (SB194); 2019/2020 retain the prior per-capita/CPI regression anchors. Regenerated via data-raw/regenerate_fixture_corpus.R.",
|
||||||
"data_vintage": {
|
"data_vintage": {
|
||||||
"source_vintages": {
|
"source_vintages": {
|
||||||
@@ -105,7 +105,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "data/series_breaks.parquet",
|
"path": "data/series_breaks.parquet",
|
||||||
"sha256": "06dcc995ff533e57cc65fa25086cc9bf83ba592c58bf7cc99269dc2576f69944",
|
"sha256": "731998516cd802f63fcf7fb66053c7a62b7be955ab0794cad4a4979cb7628b87",
|
||||||
"description": "series_breaks.parquet"
|
"description": "series_breaks.parquet"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -22,8 +22,8 @@
|
|||||||
"description": "How the intergovernmental leg was assembled; null for 'primary' and 'direct'."
|
"description": "How the intergovernmental leg was assembled; null for 'primary' and 'direct'."
|
||||||
},
|
},
|
||||||
"expenditure_concept_direct_suppressed": {
|
"expenditure_concept_direct_suppressed": {
|
||||||
"type": "boolean",
|
"type": ["boolean", "null"],
|
||||||
"description": "TRUE when expenditure_concept = 'total' and at least one requested (year, category) has intergovernmental rows but NO Direct rows in this corpus (typically a legacy aggregate-only family) -- those result rows report the intergovernmental leg alone, not Direct + IG. Always FALSE for expenditure_concept = 'primary' or 'direct'. See the affected rows' `notes` for the recovering recipe, if any."
|
"description": "TRUE when expenditure_concept = 'total' and at least one requested (year, category) has intergovernmental rows but NO Direct rows in this corpus (typically a legacy aggregate-only family) -- those result rows report the intergovernmental leg alone, not Direct + IG. Always FALSE for expenditure_concept = 'primary' or 'direct'. null (NA) when expenditure_concept = 'total' AND category = 'All Categories': the detector keys on per-category rows, which that mode collapses, so suppression cannot be computed -- see `expenditure_concept_note`. See the affected rows' `notes` for the recovering recipe, if any."
|
||||||
},
|
},
|
||||||
"revenue_concept": {
|
"revenue_concept": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
@@ -33,7 +33,48 @@
|
|||||||
},
|
},
|
||||||
"harmonization": { "type": "object" },
|
"harmonization": { "type": "object" },
|
||||||
"recipe": { "type": ["object", "null"] },
|
"recipe": { "type": ["object", "null"] },
|
||||||
"suggestions": { "type": "array" },
|
"suggestions": {
|
||||||
|
"type": "array",
|
||||||
|
"description": "Harmonization recipes that would fill incomplete coverage in the requested years for this government. Empty on a healthy query, on an un-scoped (category = NULL) query, on basis = 'raw', and on a recipe = query (which resolves its own coverage).",
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["recipe_id", "label", "available_years", "hint", "ig_recipe_id",
|
||||||
|
"trigger", "suppressed_amount", "suppressed_years", "suppressed_codes"],
|
||||||
|
"properties": {
|
||||||
|
"recipe_id": { "type": "string" },
|
||||||
|
"label": { "type": "string" },
|
||||||
|
"available_years": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "integer" },
|
||||||
|
"description": "[year_min, year_max] of the recipe's component coverage."
|
||||||
|
},
|
||||||
|
"hint": { "type": "string" },
|
||||||
|
"ig_recipe_id": {
|
||||||
|
"type": ["string", "null"],
|
||||||
|
"description": "The intergovernmental (M/L) counterpart recipe covering the same function suffixes, or null. Never set for revenue recipes."
|
||||||
|
},
|
||||||
|
"trigger": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["empty_year", "suppressed_component"],
|
||||||
|
"description": "Why this fired. 'empty_year': the result has no rows at all in a requested year. 'suppressed_component': the result HAS rows, but a component code carries dollars this government reports in the requested years that the verb's underlying long view structurally excludes -- aggregate-published, carrying no harmonized code, or absent from summary_categories. This is NOT the same thing as 'excluded from the result': a component present in the view under a different category (a scoping choice, e.g. a different `category` or a narrower `expenditure_concept`) contributes 0 and never fires. 'empty_year' wins when both apply, being the stronger claim; the suppressed_* fields are populated either way, using the same underlying-view measurement, and can be 0 even on an 'empty_year' fire."
|
||||||
|
},
|
||||||
|
"suppressed_amount": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Full US dollars this government reports, in the recipe's component codes, in the requested years, that the verb's underlying long view structurally excludes (aggregate-published, carrying no harmonized code, or absent from summary_categories) -- summed across those years. This is NOT the same quantity as 'what the result excludes': a component present in the view under a different category or a narrower `expenditure_concept` is scoped out on purpose, counts as 0 here, and is not suppression. 0 does not always mean full coverage -- see 'trigger' and 'empty_year'. May be negative where Census publishes a negative `amt` for the excluded rows."
|
||||||
|
},
|
||||||
|
"suppressed_years": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "integer" },
|
||||||
|
"description": "The requested years contributing to suppressed_amount."
|
||||||
|
},
|
||||||
|
"suppressed_codes": {
|
||||||
|
"type": "array",
|
||||||
|
"items": { "type": "string" },
|
||||||
|
"description": "The excluded component item codes, sorted."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"scope": { "type": "object" },
|
"scope": { "type": "object" },
|
||||||
"codes_summed": { "type": "object" },
|
"codes_summed": { "type": "object" },
|
||||||
"aggregate_fallback": { "type": ["object", "null"] },
|
"aggregate_fallback": { "type": ["object", "null"] },
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
CREATE OR REPLACE VIEW long AS
|
CREATE OR REPLACE VIEW long AS
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM read_parquet('{url}data/long/**/*.parquet', hive_partitioning = true);
|
-- {long_files} carries its own quoting: a bracketed list of every partition
|
||||||
|
-- the manifest enumerates, or a single quoted glob on fallback. Do NOT wrap
|
||||||
|
-- it in quotes. See .long_files_sql() in R/views.R for why a glob alone
|
||||||
|
-- cannot work over HTTP.
|
||||||
|
FROM read_parquet({long_files}, hive_partitioning = true);
|
||||||
|
|||||||
+6
-1
@@ -28,7 +28,12 @@ argument: for holdings, `category` is a strict coarsening of
|
|||||||
every combination would be either redundant or empty.
|
every combination would be either redundant or empty.
|
||||||
`category = "Fund Balances"` is exactly the `general` family
|
`category = "Fund Balances"` is exactly the `general` family
|
||||||
(`W01`/`W31`/`W61`). `balance_subtype` is returned, so a finer split is
|
(`W01`/`W31`/`W61`). `balance_subtype` is returned, so a finer split is
|
||||||
one `dplyr::filter()` away.}
|
one `dplyr::filter()` away. The reserved pseudo-category
|
||||||
|
`"All Categories"` (see [cog_spending()]) is **not** supported here and
|
||||||
|
errors with class `uscogdata_all_categories_unsupported`: it sums a
|
||||||
|
concept's subtype scope, and holdings are a stock with no concept
|
||||||
|
vocabulary to sum across. Omit `category` to get every category broken
|
||||||
|
out instead.}
|
||||||
|
|
||||||
\item{per_capita}{Divide holdings by population. Note this is a **stock per
|
\item{per_capita}{Divide holdings by population. Note this is a **stock per
|
||||||
resident** (reserves per person), which is *not* comparable to
|
resident** (reserves per person), which is *not* comparable to
|
||||||
|
|||||||
@@ -16,7 +16,10 @@ balance), `"spending"`, `"revenue"`, or `"balance"`.}
|
|||||||
\value{
|
\value{
|
||||||
Tibble with columns `category`, `category_type`, `subtype`,
|
Tibble with columns `category`, `category_type`, `subtype`,
|
||||||
`n_codes`, `item_codes` (comma-separated, alphabetical). Sorted by
|
`n_codes`, `item_codes` (comma-separated, alphabetical). Sorted by
|
||||||
`category_type`, `category`, `subtype`.
|
`category_type`, `category`, `subtype`. Includes one row per flow for the
|
||||||
|
reserved pseudo-category `"All Categories"`, which carries `NA` for
|
||||||
|
`subtype`, `n_codes` and `item_codes` because it is a query mode rather
|
||||||
|
than a crosswalk entry — see [cog_spending()]'s `category` argument.
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Returns the category taxonomy exposed by the corpus's
|
Returns the category taxonomy exposed by the corpus's
|
||||||
|
|||||||
@@ -21,3 +21,33 @@ Prints the structured provenance attached to a tibble returned by any
|
|||||||
`cog_*` verb, or returns it as a list for downstream use (MCP tools,
|
`cog_*` verb, or returns it as a list for downstream use (MCP tools,
|
||||||
dashboards, JSON export).
|
dashboards, JSON export).
|
||||||
}
|
}
|
||||||
|
\section{Two kinds of series break}{
|
||||||
|
|
||||||
|
Catalogued breaks reach you without being asked for, in two disjoint
|
||||||
|
fields, because a caveat about one series and a caveat about the whole
|
||||||
|
corpus are different claims:
|
||||||
|
|
||||||
|
* **`series_break_refs`** — breaks matched against the item codes actually
|
||||||
|
present in this result. A break in one code you queried.
|
||||||
|
* **`corpus_break_refs`** — breaks catalogued with `fin_code = "ALL"`,
|
||||||
|
which are statements about the corpus rather than about any one code:
|
||||||
|
dollar precision across the 1976/1977 boundary (`SB085`), imputation
|
||||||
|
exclusion from FY2002 (`SB087`), the FY2012 dense-to-sparse
|
||||||
|
representation change (`SB194`), and the FY2017 government-identifier
|
||||||
|
change (`SB086`). These are selected on the break-year window alone.
|
||||||
|
|
||||||
|
`SB194` is the one most likely to matter: a query spanning FY2011 to FY2012
|
||||||
|
crosses the boundary where an absent cell stops meaning "Census published
|
||||||
|
$0" and starts meaning "not reported".
|
||||||
|
}
|
||||||
|
|
||||||
|
\section{Other provenance blocks}{
|
||||||
|
|
||||||
|
`transformations$units_conversion` records the `$1,000s`-to-dollars
|
||||||
|
multiply that every amount column has already had applied.
|
||||||
|
`transformations$per_capita` records the population denominator and its
|
||||||
|
year range. `coverage` and `coverage_mode` appear on multi-government
|
||||||
|
results (see [cog_geographic_rollup()]). `completion` appears when
|
||||||
|
`complete = TRUE`. `balance_caveats` appears on [cog_balances()] results.
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ cog_geographic_rollup(
|
|||||||
`canonical_govid` values. At least one layer required.}
|
`canonical_govid` values. At least one layer required.}
|
||||||
|
|
||||||
\item{category}{Single category name or character vector (passed through
|
\item{category}{Single category name or character vector (passed through
|
||||||
to [cog_spending()]).}
|
to [cog_spending()]), or the reserved `"All Categories"` for one summed
|
||||||
|
row per `(year, canonical_govid, subtype)` covering every category in the
|
||||||
|
concept's scope. `"All Categories"` is the efficient way to build a
|
||||||
|
geographic total: without it a caller must issue one rollup per category
|
||||||
|
and sum the results themselves.}
|
||||||
|
|
||||||
\item{years}{Integer vector of years.}
|
\item{years}{Integer vector of years.}
|
||||||
|
|
||||||
@@ -80,3 +84,23 @@ the result. The dropped govids are recorded in
|
|||||||
(gov type 4) and school districts (gov type 5) from per-capita rollups
|
(gov type 4) and school districts (gov type 5) from per-capita rollups
|
||||||
by design — see `vignette('population-denominators')`.
|
by design — see `vignette('population-denominators')`.
|
||||||
}
|
}
|
||||||
|
\section{Reading `coverage`}{
|
||||||
|
|
||||||
|
`provenance$coverage` reports `n_units_reporting` against
|
||||||
|
`n_units_expected` per year. **`n_units_reporting` is category-conditional:
|
||||||
|
it counts governments with rows for the category you asked for, not
|
||||||
|
governments collected that year.** A government that was surveyed and
|
||||||
|
genuinely spends nothing in that category is indistinguishable here from one
|
||||||
|
that was never surveyed.
|
||||||
|
|
||||||
|
The ratio is therefore **not a response rate** and must not be used as one.
|
||||||
|
In FY2022 — a complete census year — Georgia reports 393 of 567 cities for
|
||||||
|
`category = "Police"`; the 174-city gap is overwhelmingly cities that
|
||||||
|
contract policing to the county sheriff, not non-response.
|
||||||
|
|
||||||
|
The comparison that *is* valid is the same category across a census year
|
||||||
|
(ending in 2 or 7) and a sample year, where the real-zero component is
|
||||||
|
roughly constant and the difference reflects the survey cycle. `is_census_year`
|
||||||
|
marks which is which.
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,3 +107,23 @@ call. Those summary rows are quantiles **within each category**, not
|
|||||||
quantiles of each peer's total — see the `@return` section before summing
|
quantiles of each peer's total — see the `@return` section before summing
|
||||||
them.
|
them.
|
||||||
}
|
}
|
||||||
|
\section{Reading `coverage`}{
|
||||||
|
|
||||||
|
`provenance$coverage` reports `n_units_reporting` against
|
||||||
|
`n_units_expected` per year. **`n_units_reporting` is category-conditional:
|
||||||
|
it counts cohort members with rows for the category you asked for, not
|
||||||
|
cohort members collected that year.** A government that was surveyed and
|
||||||
|
genuinely spends nothing in that category is indistinguishable here from one
|
||||||
|
that was never surveyed.
|
||||||
|
|
||||||
|
The ratio is therefore **not a response rate** and must not be used as one.
|
||||||
|
In FY2022 — a complete census year — Georgia reports 393 of 567 cities for
|
||||||
|
`category = "Police"`; the 174-city gap is overwhelmingly cities that
|
||||||
|
contract policing to the county sheriff, not non-response.
|
||||||
|
|
||||||
|
The comparison that *is* valid is the same category across a census year
|
||||||
|
(ending in 2 or 7) and a sample year, where the real-zero component is
|
||||||
|
roughly constant and the difference reflects the survey cycle. `is_census_year`
|
||||||
|
marks which is which.
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
+22
-2
@@ -13,7 +13,9 @@ cog_revenue(
|
|||||||
basis = c("harmonized", "raw"),
|
basis = c("harmonized", "raw"),
|
||||||
recipe = NULL,
|
recipe = NULL,
|
||||||
revenue_concept = c("general", "total"),
|
revenue_concept = c("general", "total"),
|
||||||
complete = FALSE
|
complete = FALSE,
|
||||||
|
limit = NULL,
|
||||||
|
offset = NULL
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
@@ -22,7 +24,16 @@ cog_revenue(
|
|||||||
\item{years}{Integer vector of years.}
|
\item{years}{Integer vector of years.}
|
||||||
|
|
||||||
\item{category}{Character vector of category names (from
|
\item{category}{Character vector of category names (from
|
||||||
`summary_categories.category`), or `NULL` for all categories.}
|
`summary_categories.category`), or `NULL` for all categories broken out
|
||||||
|
one row each. The reserved value `"All Categories"` instead returns a
|
||||||
|
single summed row per `(year, canonical_govid, subtype)`, covering every
|
||||||
|
category inside the requested concept's subtype scope. It cannot be
|
||||||
|
combined with other category names, and it is not the same thing as
|
||||||
|
`revenue_concept = "total"`: the concept chooses which subtypes are in
|
||||||
|
scope, `"All Categories"` chooses whether rows inside that scope are
|
||||||
|
broken out or summed. Because the result keeps one row per
|
||||||
|
`revenue_subtype`, filtering the returned frame to
|
||||||
|
`revenue_subtype == "own_source"` gives an own-source revenue total.}
|
||||||
|
|
||||||
\item{per_capita}{If `TRUE`, adds `amt_per_capita_nominal` (and
|
\item{per_capita}{If `TRUE`, adds `amt_per_capita_nominal` (and
|
||||||
`amt_per_capita_real` when `adjust_to_year` is set) using the per-year
|
`amt_per_capita_real` when `adjust_to_year` is set) using the per-year
|
||||||
@@ -106,6 +117,15 @@ possibly-misleading `"harmonized"`/`"raw"` value.}
|
|||||||
`recipe` or with `expenditure_concept = "total"` (class
|
`recipe` or with `expenditure_concept = "total"` (class
|
||||||
`uscogdata_complete_unsupported`) — neither draws its cells from
|
`uscogdata_complete_unsupported`) — neither draws its cells from
|
||||||
`code_set`.}
|
`code_set`.}
|
||||||
|
|
||||||
|
\item{limit}{Maximum number of result rows to return, pushed into the SQL
|
||||||
|
query itself (`LIMIT`/`OFFSET`) rather than applied after the full
|
||||||
|
result is materialized. `NULL` (the default) returns every matching row,
|
||||||
|
exactly as before this parameter existed. Mutually exclusive with
|
||||||
|
`recipe` and with `complete = TRUE` -- see `offset` and `total_rows`.}
|
||||||
|
|
||||||
|
\item{offset}{Rows to skip before `limit` starts counting (0-based).
|
||||||
|
Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||||
|
|||||||
+33
-4
@@ -13,7 +13,9 @@ cog_spending(
|
|||||||
basis = c("harmonized", "raw"),
|
basis = c("harmonized", "raw"),
|
||||||
recipe = NULL,
|
recipe = NULL,
|
||||||
expenditure_concept = c("primary", "direct", "total"),
|
expenditure_concept = c("primary", "direct", "total"),
|
||||||
complete = FALSE
|
complete = FALSE,
|
||||||
|
limit = NULL,
|
||||||
|
offset = NULL
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
@@ -22,7 +24,16 @@ cog_spending(
|
|||||||
\item{years}{Integer vector of years.}
|
\item{years}{Integer vector of years.}
|
||||||
|
|
||||||
\item{category}{Character vector of category names (from
|
\item{category}{Character vector of category names (from
|
||||||
`summary_categories.category`), or `NULL` for all categories.}
|
`summary_categories.category`), or `NULL` for all categories broken out
|
||||||
|
one row each. The reserved value `"All Categories"` instead returns a
|
||||||
|
single summed row per `(year, canonical_govid, subtype)`, covering every
|
||||||
|
category inside the requested concept's subtype scope. It cannot be
|
||||||
|
combined with other category names, and it is not the same thing as
|
||||||
|
`expenditure_concept = "total"`: the concept chooses which subtypes are in
|
||||||
|
scope, `"All Categories"` chooses whether rows inside that scope are
|
||||||
|
broken out or summed. Because the result keeps one row per
|
||||||
|
`spend_subtype`, filtering the returned frame to
|
||||||
|
`spend_subtype == "operations"` gives an operating-expenditure total.}
|
||||||
|
|
||||||
\item{per_capita}{If `TRUE`, adds `amt_per_capita_nominal` (and
|
\item{per_capita}{If `TRUE`, adds `amt_per_capita_nominal` (and
|
||||||
`amt_per_capita_real` when `adjust_to_year` is set) using the per-year
|
`amt_per_capita_real` when `adjust_to_year` is set) using the per-year
|
||||||
@@ -97,7 +108,12 @@ possibly-misleading `"harmonized"`/`"raw"` value.}
|
|||||||
component (when one exists), and
|
component (when one exists), and
|
||||||
`provenance$expenditure_concept_direct_suppressed` is `TRUE` -- the
|
`provenance$expenditure_concept_direct_suppressed` is `TRUE` -- the
|
||||||
figure in those rows is the intergovernmental leg alone, not Direct +
|
figure in those rows is the intergovernmental leg alone, not Direct +
|
||||||
IG.}
|
IG. When `category = "All Categories"` is combined with
|
||||||
|
`expenditure_concept = "total"`, this detection cannot run (it keys on
|
||||||
|
per-category rows, which all-categories mode collapses to one literal
|
||||||
|
value), so `expenditure_concept_direct_suppressed` is `NA` rather than a
|
||||||
|
possibly-false `FALSE`; query an explicit `category` to get a real
|
||||||
|
answer.}
|
||||||
|
|
||||||
\item{complete}{If `TRUE`, fill the requested grid so that a cell the
|
\item{complete}{If `TRUE`, fill the requested grid so that a cell the
|
||||||
corpus does not carry still appears, labelled with **why** it is
|
corpus does not carry still appears, labelled with **why** it is
|
||||||
@@ -121,6 +137,15 @@ possibly-misleading `"harmonized"`/`"raw"` value.}
|
|||||||
`recipe` or with `expenditure_concept = "total"` (class
|
`recipe` or with `expenditure_concept = "total"` (class
|
||||||
`uscogdata_complete_unsupported`) — neither draws its cells from
|
`uscogdata_complete_unsupported`) — neither draws its cells from
|
||||||
`code_set`.}
|
`code_set`.}
|
||||||
|
|
||||||
|
\item{limit}{Maximum number of result rows to return, pushed into the SQL
|
||||||
|
query itself (`LIMIT`/`OFFSET`) rather than applied after the full
|
||||||
|
result is materialized. `NULL` (the default) returns every matching row,
|
||||||
|
exactly as before this parameter existed. Mutually exclusive with
|
||||||
|
`recipe` and with `complete = TRUE` -- see `offset` and `total_rows`.}
|
||||||
|
|
||||||
|
\item{offset}{Rows to skip before `limit` starts counting (0-based).
|
||||||
|
Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||||
@@ -130,7 +155,11 @@ Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
|||||||
and `value_source` when `complete = TRUE`.
|
and `value_source` when `complete = TRUE`.
|
||||||
Carries a `provenance` attribute matching `inst/schemas/provenance-v1.json`,
|
Carries a `provenance` attribute matching `inst/schemas/provenance-v1.json`,
|
||||||
whose `completion` block reports `applied`, `rows_filled`, and the
|
whose `completion` block reports `applied`, `rows_filled`, and the
|
||||||
per-year `absence_means` rule that was applied.
|
per-year `absence_means` rule that was applied. When `limit` is set,
|
||||||
|
also carries a `total_rows` attribute: the full unpaginated row count,
|
||||||
|
computed by the same query (`COUNT(*) OVER()`) rather than a second
|
||||||
|
round trip -- so a caller walking pages never has to ask "how many are
|
||||||
|
there" separately.
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
One row per `(year, canonical_govid, spend_subtype, category)`. Amounts are
|
One row per `(year, canonical_govid, spend_subtype, category)`. Amounts are
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,296 @@
|
|||||||
|
# `uscogdata` 0.3.0 — public release
|
||||||
|
|
||||||
|
**Date:** 2026-08-08 · **Status:** design, awaiting approval
|
||||||
|
**Scope:** release-readiness, README, NEWS. Distribution mechanics recorded here as
|
||||||
|
decided, sequenced after the package is clean.
|
||||||
|
|
||||||
|
`uscogdata` is feature-complete and the corpus it reads has been public on
|
||||||
|
HuggingFace since 2026-08-07 (294 downloads as of this writing). The API built on
|
||||||
|
it is live. What does not exist is a public *package*: the repo is private, there
|
||||||
|
is no install path, and — measured, not assumed — **a stranger who installed it
|
||||||
|
today could not read the corpus at all.**
|
||||||
|
|
||||||
|
This spec covers making that untrue.
|
||||||
|
|
||||||
|
## Decisions locked
|
||||||
|
|
||||||
|
| Decision | Choice |
|
||||||
|
|---|---|
|
||||||
|
| Canonical source | `gitea.civilytics.org/Civilytics/uscogdata`, flipped public |
|
||||||
|
| Public mirror | `github.com/civilytics/uscogdata` — issues, PRs, multi-OS check, CDN |
|
||||||
|
| Mirror mechanism | Gitea Actions non-force `git push` (not a push mirror) |
|
||||||
|
| Binaries | `civilytics.r-universe.dev`, registry pinned to a release tag |
|
||||||
|
| Author of record | Jared E. Knowles `<jared@civilytics.com>`, ORCID `0000-0003-0005-9478` |
|
||||||
|
| Copyright | Civilytics Consulting LLC (`cph`, `fnd`) |
|
||||||
|
| License | MIT (package) · CC-BY-4.0 (corpus) |
|
||||||
|
| Corrections intake | Deferred — see *Out of scope* |
|
||||||
|
| Other packages | Parked until this one walks the path end to end |
|
||||||
|
|
||||||
|
## P0 — the corpus is unreachable
|
||||||
|
|
||||||
|
Two independent faults, either of which alone is fatal.
|
||||||
|
|
||||||
|
**No corpus URL exists.** `R/config.R` defaults to the literal
|
||||||
|
`REPLACE_WITH_SHARE_TOKEN` sentinel, and no file in the repo supplies a working
|
||||||
|
one. A new user calling any verb gets `uscogdata_url_not_configured` with no path
|
||||||
|
to resolution.
|
||||||
|
|
||||||
|
**Remote reads are broken regardless.** Every partitioned view globs:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
FROM read_parquet('{url}data/long/**/*.parquet', hive_partitioning = true)
|
||||||
|
```
|
||||||
|
|
||||||
|
DuckDB 1.5.5 refuses globs over generic HTTP. Its suggested
|
||||||
|
`allow_asterisks_in_http_paths` escape hatch does not help — it forwards the
|
||||||
|
literal `**/*` as a filename and 404s, because plain HTTP exposes no directory
|
||||||
|
listing to expand against.
|
||||||
|
|
||||||
|
The package therefore works only against a **local path**. That is how the API
|
||||||
|
runs it (`CORPUS_HOST_PATH` is a host mount on maxwell) and how the tests run
|
||||||
|
(bundled fixture), which is why the fault went unnoticed. The README's headline
|
||||||
|
claim — *"Reads the published corpus directly from Nextcloud via DuckDB httpfs —
|
||||||
|
no local bulk downloads required"* — is currently false.
|
||||||
|
|
||||||
|
### Fix: enumerate from the manifest, do not glob
|
||||||
|
|
||||||
|
`manifest.json` already lists every partition under `files.long_partitions[]`
|
||||||
|
with `path`, `year`, `sha256`, `row_count` and `size_bytes` — 56 of them.
|
||||||
|
Substituting an explicit file list for the glob was measured against the
|
||||||
|
published corpus on 2026-08-08:
|
||||||
|
|
||||||
|
| Path | Result |
|
||||||
|
|---|---|
|
||||||
|
| `https://…/data/long/**/*.parquet` (default) | error — globs unsupported over HTTP |
|
||||||
|
| same, `allow_asterisks_in_http_paths = true` | error — literal `**/*` 404s |
|
||||||
|
| `hf://datasets/civilytics/us-cog-finance/…` glob | 46,148,034 rows |
|
||||||
|
| **explicit list over plain https** | **46,148,034 rows** |
|
||||||
|
|
||||||
|
`hive_partitioning = true` still recovers `year` from the paths under
|
||||||
|
enumeration, so no downstream view or verb changes.
|
||||||
|
|
||||||
|
Enumeration is preferred over `hf://` deliberately. It is **host-agnostic** —
|
||||||
|
Nextcloud, HuggingFace, or any static server take the same code path — where
|
||||||
|
`hf://` would tie the default to one vendor's protocol and still need
|
||||||
|
special-casing, since manifest fetching goes through `httr2`, which cannot speak
|
||||||
|
`hf://`. Enumeration also *removes* a dependency (globbing) rather than adding
|
||||||
|
one, and the manifest's per-file `sha256` becomes available for integrity
|
||||||
|
checking later.
|
||||||
|
|
||||||
|
Views are registered from `inst/sql/` with `{url}` substitution in
|
||||||
|
`R/views.R:.register_views()`. The list must be built once per session from the
|
||||||
|
already-fetched manifest and substituted the same way, so the change is confined
|
||||||
|
to view registration and does not touch verb code.
|
||||||
|
|
||||||
|
### Fix: ship a working default
|
||||||
|
|
||||||
|
`R/config.R`'s default becomes the public HuggingFace `resolve/main/` URL:
|
||||||
|
CC-BY-4.0, no token to publish, CDN-backed, and it keeps maxwell's uplink out of
|
||||||
|
the path — the same reasoning behind the GitHub mirror and r-universe.
|
||||||
|
|
||||||
|
This means `library(uscogdata)` followed by a verb works with **zero
|
||||||
|
configuration**, which is what makes the package demonstrable in a README and
|
||||||
|
later in a post. `USCOGDATA_URL` and `options(uscogdata.url=)` continue to
|
||||||
|
override, so the Nextcloud copy and local mirrors are unaffected.
|
||||||
|
|
||||||
|
The `uscogdata_url_not_configured` error class stays — it still fires for an
|
||||||
|
explicitly-set empty or placeholder URL — but ceases to be the default
|
||||||
|
experience.
|
||||||
|
|
||||||
|
### Consequence: `cog_mirror()` is promoted
|
||||||
|
|
||||||
|
Measured cost of the remote default, from efron on a good connection:
|
||||||
|
|
||||||
|
| | |
|
||||||
|
|---|---|
|
||||||
|
| Whole corpus | **190.6 MB**, 56 partitions, 46,148,034 rows, FY1967–FY2024 |
|
||||||
|
| One government, one year | 1.5 s |
|
||||||
|
| One government, all 56 years | 2.8 s |
|
||||||
|
| Disk written | **0.00 MB** — range requests only; `external_file_cache` is in-memory |
|
||||||
|
|
||||||
|
Nothing persists locally beyond the shared `httpfs` extension in `~/.duckdb` (a
|
||||||
|
few MB, once per machine, across all DuckDB use). Costs are RAM and per-query
|
||||||
|
bandwidth, since nothing caches between sessions.
|
||||||
|
|
||||||
|
Those timings are raw scans. Real verbs additionally join crosswalks, resolve
|
||||||
|
categories and assemble provenance, so end-to-end verb latency will be higher and
|
||||||
|
**must be re-measured once the fix lands** — it cannot be measured today.
|
||||||
|
|
||||||
|
The corpus being only 190.6 MB makes `cog_mirror()` a first-class option rather
|
||||||
|
than a developer footnote. The README presents **both paths**:
|
||||||
|
|
||||||
|
- **Remote (default, zero setup)** — trying it out, teaching, one-off questions.
|
||||||
|
- **Mirrored (`cog_mirror()`, 190 MB once)** — repeated or heavy analysis,
|
||||||
|
offline work, reproducibility, or preferring not to depend on HuggingFace.
|
||||||
|
|
||||||
|
The second is also the honest answer to the vendor-dependency question raised by
|
||||||
|
defaulting to HuggingFace: **the escape hatch is one function call and 190 MB**,
|
||||||
|
after which no analysis touches an external service. The README says so
|
||||||
|
explicitly. That is the difference between a convenience default and lock-in.
|
||||||
|
|
||||||
|
## Release-readiness fixes
|
||||||
|
|
||||||
|
| # | Issue | Fix |
|
||||||
|
|---|---|---|
|
||||||
|
| 1 | `MaxCorpusSchema: 5` in DESCRIPTION; `.validate_schema()` accepts `4,5,6,7`; published corpus is **7** | `MaxCorpusSchema: 7` |
|
||||||
|
| 2 | `^vignettes$` in `.Rbuildignore` — both vignettes absent from the installed package, while README tells users to run `vignette("total-spending")` | Remove `^vignettes$`, `^doc$`, `^Meta$`. Both vignettes build offline (`total-spending` reads the bundled fixture; `population-denominators` is `eval = FALSE`) |
|
||||||
|
| 3 | `_pkgdown.yml` reference index covers 6 of 14 exports — pkgdown errors on missing topics | Add `cog_categories`, `cog_explain`, `cog_find_peers`, `cog_geographic_rollup`, `cog_manifest`, `cog_mirror`, `cog_peer_compare`, `cog_recipes`; set `url:` |
|
||||||
|
| 4 | No `URL:` / `BugReports:` in DESCRIPTION | Add both, pointing at the GitHub mirror |
|
||||||
|
| 5 | No `LICENSE.md`; `LICENSE` holder reads `Civilytics` | `usethis::use_mit_license("Civilytics Consulting LLC")` |
|
||||||
|
| 6 | README instructs stripping the fixture at release | Delete that section — see below |
|
||||||
|
| 7 | `Authors@R` is an org with no human | Jared E. Knowles `aut`/`cre` + ORCID; Civilytics Consulting LLC `cph`/`fnd` |
|
||||||
|
|
||||||
|
**On #6.** The advice to add `^inst/extdata/fixture_corpus$` to `.Rbuildignore`
|
||||||
|
is CRAN-sized thinking (5 MB limit) and this package is not going to CRAN.
|
||||||
|
Stripping the 15 MB fixture would break `total-spending.Rmd`, which reads from
|
||||||
|
it, and would leave r-universe and GitHub Actions unable to run the 28 test files
|
||||||
|
without a corpus credential. **The fixture is what lets `R CMD check` pass
|
||||||
|
anywhere with zero secrets** — precisely what public CI needs. It ships.
|
||||||
|
|
||||||
|
## README
|
||||||
|
|
||||||
|
The current README addresses someone standing inside the repo tree: status reads
|
||||||
|
"Under active development (Phase 2 of the cog_pipeline project)", it points at
|
||||||
|
`../cog_pipeline/docs/reader-specification.md`, the install line is commented
|
||||||
|
out, and developer, testing and release sections sit above anything a user needs.
|
||||||
|
|
||||||
|
Restructured around a stranger, in this order:
|
||||||
|
|
||||||
|
1. **What this is** — one paragraph, and what the corpus covers (types 0–3,
|
||||||
|
FY1967–FY2024, 46M rows, 190.6 MB).
|
||||||
|
2. **Install** — r-universe first (binaries), git second.
|
||||||
|
3. **Quickstart that actually runs** — resolve a government, get its history,
|
||||||
|
print provenance. No configuration step.
|
||||||
|
4. **Two ways to read the corpus** — remote default vs `cog_mirror()`, with the
|
||||||
|
measured numbers and the independence note.
|
||||||
|
5. **Amounts are in full US dollars** — kept near the top. This is the errata
|
||||||
|
most likely to produce a wrong answer that looks plausible.
|
||||||
|
6. **Concepts** — primary/direct/total spending, general/total revenue,
|
||||||
|
coverage. Condensed, linking to the vignettes for the full treatment.
|
||||||
|
7. **How to cite** — `citation("uscogdata")`, corpus CC-BY-4.0 attribution.
|
||||||
|
8. **Contributing** — canonical-on-Gitea PR flow.
|
||||||
|
|
||||||
|
Developer notes, testing instructions and release procedure move to
|
||||||
|
`CONTRIBUTING.md`. Every path reference to a sibling repo is removed or replaced
|
||||||
|
with a URL that resolves for someone who has only this repo.
|
||||||
|
|
||||||
|
## NEWS.md
|
||||||
|
|
||||||
|
`NEWS.md` currently holds two sections. `0.2.0` is a legitimate changelog — the
|
||||||
|
`"All Categories"` reserved value, the coverage-signposting fix, the
|
||||||
|
`n_units_reporting` documentation — and it stays. Beneath it,
|
||||||
|
`0.1.0 (development)` is a pre-release churn log: changes described relative to
|
||||||
|
states no user has ever seen ("Breaking: corpus schema_version 4", "the package
|
||||||
|
now requires…"), spanning the package's entire pre-release development. To a
|
||||||
|
newcomer deciding whether to depend on this, that section reads as instability.
|
||||||
|
|
||||||
|
**A new `0.3.0` section is added at the top, framed as the first public
|
||||||
|
release**: what the package does, what the corpus covers, and the caveats that
|
||||||
|
are genuinely load-bearing. **`0.2.0` is kept verbatim.** **`0.1.0 (development)`
|
||||||
|
is dropped** — that history stays in git, where it belongs.
|
||||||
|
|
||||||
|
The version is `0.3.0` rather than `0.2.0` because this release changes
|
||||||
|
user-visible behaviour: remote corpus reads go from broken to working, and the
|
||||||
|
default URL from a dead placeholder to a live corpus. It is also not `1.0.0` —
|
||||||
|
the corpus still excludes government types 4 and 5 pending validation, so a
|
||||||
|
stability promise would overclaim. No git tag exists for any prior version;
|
||||||
|
`chore: release 0.2.0` bumped `DESCRIPTION` and `NEWS` only.
|
||||||
|
|
||||||
|
The substantive content is migrated, not deleted. These are hard-won and belong
|
||||||
|
in documentation rather than buried in a changelog:
|
||||||
|
|
||||||
|
| Content | Destination |
|
||||||
|
|---|---|
|
||||||
|
| Coverage disclosure on multi-government aggregates (census vs sample years) | README concepts + `cog_geographic_rollup()` docs |
|
||||||
|
| `complete = TRUE` three-way absence semantics (`reported` / `census_zero` / `not_reported`) | `cog_spending()` / `cog_revenue()` docs |
|
||||||
|
| Series-break and corpus-break surfacing | README + `cog_explain()` docs |
|
||||||
|
| $1,000s → full dollars conversion | README, already prominent |
|
||||||
|
| Per-year F-33 population denominators | `population-denominators` vignette, already there |
|
||||||
|
|
||||||
|
This also makes NEWS reusable as raw material for the release announcement,
|
||||||
|
which is the stated downstream purpose.
|
||||||
|
|
||||||
|
## Distribution mechanics
|
||||||
|
|
||||||
|
Recorded as decided; executed after the package is clean and checks are green.
|
||||||
|
|
||||||
|
**Sequence matters.** r-universe publishes check results the moment a package is
|
||||||
|
registered. Registering before the fixes above land means a red badge on day one,
|
||||||
|
which is a worse first impression than a week's delay.
|
||||||
|
|
||||||
|
1. `gitleaks` over full history. A coarse grep found nothing across 140 commits
|
||||||
|
and the default corpus URL is still the placeholder sentinel, but a proper
|
||||||
|
scan is the gate on an irreversible action.
|
||||||
|
2. Flip the Gitea repo public. Disable Gitea issues on it, so there is exactly
|
||||||
|
one inbox.
|
||||||
|
3. Create `github.com/civilytics/uscogdata`. Add `.github/workflows/` for the
|
||||||
|
Windows/macOS/Linux `R CMD check` matrix — the platforms the Gitea runner
|
||||||
|
cannot provide, and which this package has never been tested on despite
|
||||||
|
depending on duckdb and httr2. Gitea reads `.gitea/workflows`, GitHub reads
|
||||||
|
`.github/workflows`; both live in one tree without colliding.
|
||||||
|
4. Gitea Actions workflow pushing to GitHub **without `--force`**, so divergence
|
||||||
|
fails loudly in CI rather than silently overwriting.
|
||||||
|
5. Add `jared@civilytics.com` as a verified secondary email on the GitHub
|
||||||
|
account — r-universe links maintainer identity by matching DESCRIPTION's email
|
||||||
|
against registered GitHub emails, and the association only takes effect on the
|
||||||
|
next build.
|
||||||
|
6. Tag `v0.3.0`. Create `github.com/civilytics/civilytics.r-universe.dev` with a
|
||||||
|
`packages.json` pinned to the tag, pointing at the GitHub mirror rather than
|
||||||
|
Gitea so clone traffic stays off maxwell. Install the r-universe app.
|
||||||
|
|
||||||
|
### PR flow
|
||||||
|
|
||||||
|
Never press Merge on GitHub. A merge there is overwritten by the next sync, the
|
||||||
|
PR still displays "Merged", and nothing says otherwise.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git remote add github https://github.com/civilytics/uscogdata.git
|
||||||
|
git config --add remote.github.fetch '+refs/pull/*/head:refs/remotes/github/pr/*'
|
||||||
|
git fetch github
|
||||||
|
git switch -c pr-42 github/pr/42 # test
|
||||||
|
git switch main && git merge --no-ff pr-42
|
||||||
|
git push origin main # Gitea -> mirror -> GitHub
|
||||||
|
```
|
||||||
|
|
||||||
|
GitHub auto-closes a PR as merged once its head commit becomes an ancestor of the
|
||||||
|
base branch, so `--no-ff` — which preserves the contributor's SHAs — makes the PR
|
||||||
|
close itself when the mirror pushes. **For external PRs, merge; do not squash or
|
||||||
|
rebase.** Squashing rewrites the SHAs, the auto-close never fires, and closing by
|
||||||
|
hand reads to a first-time contributor as rejection.
|
||||||
|
|
||||||
|
`CONTRIBUTING.md` states this, and a GitHub Action comments it on incoming PRs.
|
||||||
|
No CLA; no DCO.
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
|
||||||
|
The release is not done until all of these pass:
|
||||||
|
|
||||||
|
1. `R CMD check --as-cran` clean on Linux, and on Windows and macOS via the
|
||||||
|
GitHub matrix. This package has never been checked on the latter two.
|
||||||
|
2. Full test suite (28 files) green against the **bundled fixture**, offline,
|
||||||
|
with no credentials — the property public CI depends on.
|
||||||
|
3. Full test suite green against the **live corpus**, which additionally
|
||||||
|
exercises the enumeration fix that the fixture's local path cannot.
|
||||||
|
4. `pkgdown::build_site()` completes.
|
||||||
|
5. Both vignettes present in the built tarball and
|
||||||
|
`vignette("total-spending", package = "uscogdata")` resolves from an
|
||||||
|
installed copy.
|
||||||
|
6. **Cold-start check on a machine that has never seen this package:** install
|
||||||
|
from r-universe, `library(uscogdata)`, run the README quickstart verbatim with
|
||||||
|
no environment variables set. This is the only test that catches the P0 class
|
||||||
|
of fault, and its absence is why the fault survived.
|
||||||
|
7. End-to-end verb latency re-measured against the live corpus and the README's
|
||||||
|
numbers updated if they moved.
|
||||||
|
|
||||||
|
## Out of scope
|
||||||
|
|
||||||
|
- **Corrections intake.** Deferred by decision. Consequence: the release cannot
|
||||||
|
invite data-error reports or make the "traceable and correctable" claim that
|
||||||
|
most distinguishes this corpus from Census's own files. `BugReports:` points at
|
||||||
|
package issues only. A verified correction should eventually terminate as a
|
||||||
|
`lineage_event` or `series_break` row so it propagates through provenance to
|
||||||
|
every consumer — that design is unstarted.
|
||||||
|
- **Announcement posts.** Deferred. The API announcement is gated on corrections
|
||||||
|
landing and merits a Civic Pulse edition.
|
||||||
|
- **The rest of the R package backlog.** Parked until this one completes the path.
|
||||||
|
- **`cog_pipeline` publication.** Stays private.
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
test_that('cog_geographic_rollup() accepts "All Categories" and agrees with per-category sums', {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
govs <- cog_gov_search(name = NULL, state = "WI", type = 2L)
|
||||||
|
expect_gt(nrow(govs), 1L)
|
||||||
|
ids <- list(city = utils::head(govs$canonical_govid, 25L))
|
||||||
|
|
||||||
|
by_cat <- cog_geographic_rollup(ids, category = NULL, years = 2019L)
|
||||||
|
total <- cog_geographic_rollup(ids, category = "All Categories", years = 2019L)
|
||||||
|
|
||||||
|
expect_setequal(unique(total$category), "All Categories")
|
||||||
|
# one row per (govid, subtype) that appears in the per-category result
|
||||||
|
key_by_cat <- unique(paste(by_cat$canonical_govid, by_cat$spend_subtype))
|
||||||
|
key_total <- paste(total$canonical_govid, total$spend_subtype)
|
||||||
|
expect_setequal(key_total, key_by_cat)
|
||||||
|
|
||||||
|
lhs <- tapply(by_cat$amt_nominal, paste(by_cat$canonical_govid, by_cat$spend_subtype), sum)
|
||||||
|
rhs <- tapply(total$amt_nominal, key_total, sum)
|
||||||
|
expect_equal(as.numeric(rhs[names(lhs)]), as.numeric(lhs), tolerance = 1e-8)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" survives per_capita and inflation adjustment through the rollup', {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
govs <- cog_gov_search(name = NULL, state = "WI", type = 2L)
|
||||||
|
ids <- list(city = utils::head(govs$canonical_govid, 10L))
|
||||||
|
r <- cog_geographic_rollup(ids, category = "All Categories", years = 2019L,
|
||||||
|
per_capita = TRUE, adjust_to_year = 2020L)
|
||||||
|
expect_true(all(c("amt_per_capita_nominal", "amt_real", "amt_per_capita_real") %in% names(r)))
|
||||||
|
expect_setequal(unique(r$category), "All Categories")
|
||||||
|
expect_true(all(is.finite(r$amt_real)))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('cog_geographic_rollup() still refuses expenditure_concept = "total" with "All Categories"', {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
govs <- cog_gov_search(name = NULL, state = "WI", type = 2L)
|
||||||
|
ids <- list(city = utils::head(govs$canonical_govid, 5L))
|
||||||
|
expect_error(
|
||||||
|
cog_geographic_rollup(ids, category = "All Categories", years = 2019L,
|
||||||
|
expenditure_concept = "total")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("n_units_reporting is category-conditional, not a response rate", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
govs <- cog_gov_search(name = NULL, state = "WI", type = 2L)
|
||||||
|
ids <- list(city = govs$canonical_govid)
|
||||||
|
|
||||||
|
police <- cog_geographic_rollup(ids, category = "Police", years = 2012L)
|
||||||
|
allcat <- cog_geographic_rollup(ids, category = "All Categories", years = 2012L)
|
||||||
|
|
||||||
|
cov_police <- cog_explain(police, format = "list")$coverage
|
||||||
|
cov_all <- cog_explain(allcat, format = "list")$coverage
|
||||||
|
|
||||||
|
# Same year, same requested govids, same collection -- yet a single category
|
||||||
|
# reports fewer units than the all-categories query. That gap is real zeros,
|
||||||
|
# not non-response, which is exactly why the ratio is not a response rate.
|
||||||
|
expect_lte(cov_police$n_units_reporting, cov_all$n_units_reporting)
|
||||||
|
expect_identical(cov_police$n_units_expected, cov_all$n_units_expected)
|
||||||
|
})
|
||||||
@@ -0,0 +1,267 @@
|
|||||||
|
# Baseline at branch point: 843 PASS / 0 FAIL / 0 SKIP / 0 WARN (2026-08-05, origin/main 2fc9e75)
|
||||||
|
|
||||||
|
test_that(".build_verb_sql emits a literal category and no category filter in all-categories mode", {
|
||||||
|
sql <- uscogdata:::.build_verb_sql(
|
||||||
|
view = "spending_annotated",
|
||||||
|
subtype_col = "spend_subtype",
|
||||||
|
govid = "552025209777",
|
||||||
|
years = 2019L,
|
||||||
|
category = NULL,
|
||||||
|
subtype_scope = c("operations", "capital"),
|
||||||
|
all_categories = TRUE
|
||||||
|
)
|
||||||
|
|
||||||
|
expect_match(sql, "'All Categories' AS category", fixed = TRUE)
|
||||||
|
# no category filter of any kind
|
||||||
|
expect_false(grepl("AND category IN", sql, fixed = TRUE))
|
||||||
|
# category is not a grouping key
|
||||||
|
expect_false(grepl("GROUP BY year, canonical_govid, gov_name, xwalk_gov_name, spend_subtype, category",
|
||||||
|
sql, fixed = TRUE))
|
||||||
|
# the subtype allowlist still applies -- this is what makes the sum a concept
|
||||||
|
expect_match(sql, "AND spend_subtype IN ('operations','capital')", fixed = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".build_verb_sql is unchanged when all_categories is FALSE", {
|
||||||
|
args <- list(
|
||||||
|
view = "spending_annotated", subtype_col = "spend_subtype",
|
||||||
|
govid = "552025209777", years = 2019L, category = NULL,
|
||||||
|
subtype_scope = c("operations", "capital")
|
||||||
|
)
|
||||||
|
old <- do.call(uscogdata:::.build_verb_sql, args)
|
||||||
|
new <- do.call(uscogdata:::.build_verb_sql, c(args, list(all_categories = FALSE)))
|
||||||
|
expect_identical(old, new)
|
||||||
|
expect_match(new, "GROUP BY year, canonical_govid, gov_name, xwalk_gov_name, spend_subtype, category",
|
||||||
|
fixed = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".ALL_CATEGORIES is the exact reserved string", {
|
||||||
|
expect_identical(uscogdata:::.ALL_CATEGORIES, "All Categories")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('cog_spending(category = "All Categories") sums to the per-category total', {
|
||||||
|
gov <- "552025209777"
|
||||||
|
by_cat <- cog_spending(gov, 2019L)
|
||||||
|
total <- cog_spending(gov, 2019L, category = "All Categories")
|
||||||
|
|
||||||
|
expect_true(nrow(total) > 0L)
|
||||||
|
expect_setequal(unique(total$category), "All Categories")
|
||||||
|
# one row per subtype present in the by-category result
|
||||||
|
expect_setequal(unique(total$spend_subtype), unique(by_cat$spend_subtype))
|
||||||
|
expect_equal(nrow(total), length(unique(by_cat$spend_subtype)))
|
||||||
|
|
||||||
|
# the dollars agree, per subtype
|
||||||
|
lhs <- tapply(by_cat$amt_nominal, by_cat$spend_subtype, sum)
|
||||||
|
rhs <- tapply(total$amt_nominal, total$spend_subtype, sum)
|
||||||
|
expect_equal(as.numeric(rhs[names(lhs)]), as.numeric(lhs), tolerance = 1e-8)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" respects expenditure_concept', {
|
||||||
|
gov <- "552025209777"
|
||||||
|
prim <- cog_spending(gov, 2019L, category = "All Categories",
|
||||||
|
expenditure_concept = "primary")
|
||||||
|
dir <- cog_spending(gov, 2019L, category = "All Categories",
|
||||||
|
expenditure_concept = "direct")
|
||||||
|
# direct = primary plus interest and insurance benefits, so it is never smaller
|
||||||
|
expect_gte(sum(dir$amt_nominal), sum(prim$amt_nominal))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" works on revenue and respects revenue_concept', {
|
||||||
|
gov <- "552025209777"
|
||||||
|
gen <- cog_revenue(gov, 2019L, category = "All Categories",
|
||||||
|
revenue_concept = "general")
|
||||||
|
tot <- cog_revenue(gov, 2019L, category = "All Categories",
|
||||||
|
revenue_concept = "total")
|
||||||
|
expect_setequal(unique(gen$category), "All Categories")
|
||||||
|
expect_gte(sum(tot$amt_nominal), sum(gen$amt_nominal))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" cannot be combined with another category', {
|
||||||
|
expect_error(
|
||||||
|
cog_spending("552025209777", 2019L, category = c("All Categories", "Police")),
|
||||||
|
class = "uscogdata_all_categories_not_combinable"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" is recorded in provenance', {
|
||||||
|
r <- cog_spending("552025209777", 2019L, category = "All Categories")
|
||||||
|
expect_identical(cog_explain(r, format = "list")$category, "All Categories")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" combines with subtype to give operating totals', {
|
||||||
|
gov <- "552025209777"
|
||||||
|
ops_by_cat <- cog_spending(gov, 2019L)
|
||||||
|
ops_by_cat <- ops_by_cat[ops_by_cat$spend_subtype == "operations", ]
|
||||||
|
ops_total <- cog_spending(gov, 2019L, category = "All Categories")
|
||||||
|
ops_total <- ops_total[ops_total$spend_subtype == "operations", ]
|
||||||
|
expect_equal(sum(ops_total$amt_nominal), sum(ops_by_cat$amt_nominal),
|
||||||
|
tolerance = 1e-8)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('cog_categories() advertises "All Categories" for both flows', {
|
||||||
|
all <- cog_categories()
|
||||||
|
rows <- all[all$category == "All Categories", ]
|
||||||
|
expect_setequal(rows$category_type, c("expenditure", "revenue"))
|
||||||
|
expect_true(all(is.na(rows$subtype)))
|
||||||
|
expect_true(all(is.na(rows$n_codes)))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('cog_categories(type=) still scopes, including the pseudo-category', {
|
||||||
|
sp <- cog_categories(type = "spending")
|
||||||
|
expect_setequal(unique(sp$category_type), "expenditure")
|
||||||
|
expect_true("All Categories" %in% sp$category)
|
||||||
|
|
||||||
|
rev <- cog_categories(type = "revenue")
|
||||||
|
expect_setequal(unique(rev$category_type), "revenue")
|
||||||
|
expect_true("All Categories" %in% rev$category)
|
||||||
|
|
||||||
|
# balances have no concept vocabulary, so no pseudo-category
|
||||||
|
bal <- cog_categories(type = "balance")
|
||||||
|
expect_false("All Categories" %in% bal$category)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('cog_categories(pattern=) matches the pseudo-category', {
|
||||||
|
hit <- cog_categories(pattern = "^All Categories$")
|
||||||
|
expect_equal(nrow(hit), 2L)
|
||||||
|
})
|
||||||
|
|
||||||
|
# --- final whole-branch review fixes ---------------------------------------
|
||||||
|
|
||||||
|
test_that('complete = TRUE is refused when combined with "All Categories"', {
|
||||||
|
# .completion_grid_sql() would emit `AND c.category IN ('All Categories')`,
|
||||||
|
# match zero crosswalk rows, and the early return in .complete_result()
|
||||||
|
# would stamp completion$applied = TRUE, rows_filled = 0 -- reading as "the
|
||||||
|
# grid was checked and nothing was missing" when nothing was actually
|
||||||
|
# checked. Filling a summed row has no defined semantics, so the verb must
|
||||||
|
# refuse the combination outright (finding 2).
|
||||||
|
expect_error(
|
||||||
|
cog_spending("552025209777", 2019L, category = "All Categories",
|
||||||
|
complete = TRUE),
|
||||||
|
class = "uscogdata_complete_unsupported"
|
||||||
|
)
|
||||||
|
expect_error(
|
||||||
|
cog_revenue("552025209777", 2019L, category = "All Categories",
|
||||||
|
complete = TRUE),
|
||||||
|
class = "uscogdata_complete_unsupported"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('cog_balances() rejects "All Categories" instead of silently returning zero rows', {
|
||||||
|
# cog_balances() reuses .validate_verb_inputs() but did not pass
|
||||||
|
# allow_all_categories = TRUE, so "All Categories" used to become
|
||||||
|
# `AND category IN ('All Categories')` against balance_annotated -- 0
|
||||||
|
# matching crosswalk rows, 0 rows back, no error (finding 3). Holdings are
|
||||||
|
# a stock with no concept vocabulary to sum across, so the honest answer is
|
||||||
|
# to refuse, the same way cog_spending()/cog_revenue() refuse other
|
||||||
|
# nonsensical combinations.
|
||||||
|
expect_error(
|
||||||
|
cog_balances("552025209777", 2019L, category = "All Categories"),
|
||||||
|
class = "uscogdata_all_categories_unsupported"
|
||||||
|
)
|
||||||
|
# An ordinary category still works -- this is not a blanket regression.
|
||||||
|
r <- suppressMessages(
|
||||||
|
cog_balances("552025209777", 2019L, category = "Fund Balances")
|
||||||
|
)
|
||||||
|
expect_gt(nrow(r), 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('expenditure_concept_direct_suppressed is NA, not FALSE, when categories are collapsed', {
|
||||||
|
# .detect_direct_suppressed() keys on
|
||||||
|
# paste(year, canonical_govid, category, sep = "\r"). In all-categories
|
||||||
|
# mode every row carries the literal "All Categories" value, so an IG-only
|
||||||
|
# row's key collides with any ordinary Direct row for the same
|
||||||
|
# (year, govid) -- has_direct reads TRUE whenever the government has ANY
|
||||||
|
# direct spending at all, candidate is always empty, and the detector can
|
||||||
|
# never fire. Before the fix this silently reported FALSE, an affirmative
|
||||||
|
# claim the code did not actually compute (finding 1). NA is the honest
|
||||||
|
# answer: cog_explain(x, format = "list") is required here, since without
|
||||||
|
# format = "list" it returns the result tibble, not the provenance list.
|
||||||
|
gov <- "552025209777"
|
||||||
|
t <- cog_spending(gov, 2019L, category = "All Categories",
|
||||||
|
expenditure_concept = "total")
|
||||||
|
prov <- cog_explain(t, format = "list")
|
||||||
|
expect_true(is.na(prov$expenditure_concept_direct_suppressed))
|
||||||
|
expect_false(isTRUE(prov$expenditure_concept_direct_suppressed))
|
||||||
|
expect_match(prov$expenditure_concept_note, "unavailable", fixed = TRUE)
|
||||||
|
|
||||||
|
# A per-category "total" query on the same government/year is unaffected --
|
||||||
|
# the detector can still key correctly and reports a strict logical.
|
||||||
|
t_by_cat <- cog_spending(gov, 2019L, expenditure_concept = "total")
|
||||||
|
prov_by_cat <- cog_explain(t_by_cat, format = "list")
|
||||||
|
expect_false(is.na(prov_by_cat$expenditure_concept_direct_suppressed))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" still signposts coverage gaps (finding 6, final whole-branch review)', {
|
||||||
|
# .build_suggestions()'s candidate sub-select used to be keyed on
|
||||||
|
# `category`, e.g. `WHERE category IN ('All Categories')`. Since
|
||||||
|
# .ALL_CATEGORIES is never itself a row in summary_categories.category,
|
||||||
|
# that sub-select always came back empty in all-categories mode, so
|
||||||
|
# `candidates` was empty and .build_suggestions() short-circuited to
|
||||||
|
# list() -- coverage signposting was structurally impossible for the one
|
||||||
|
# mode whose whole selling point is "you cannot sum the wrong scope"
|
||||||
|
# (uscogdata#9's entire point, silently defeated).
|
||||||
|
#
|
||||||
|
# AL state government, FY2011, category = "Corrections": this category has
|
||||||
|
# no legacy leaf rows in FY2011 (aggregate-flagged E04/E05 family), so the
|
||||||
|
# per-category query returns 0 rows and 3 recipe-hint suggestions fire
|
||||||
|
# (empty_year path). All-categories mode does not have an empty year --
|
||||||
|
# the government has other primary spending in FY2011 -- but the same
|
||||||
|
# suppressed Corrections dollars are still excluded from the summed total,
|
||||||
|
# so the fix (scoping the candidate sub-select by subtype_col/subtype_scope
|
||||||
|
# instead of by category, symmetric with .build_verb_sql()) must still
|
||||||
|
# surface them via the suppressed_component path.
|
||||||
|
gov <- "010000226085"
|
||||||
|
|
||||||
|
by_cat <- suppressMessages(cog_spending(gov, 2011L, category = "Corrections"))
|
||||||
|
sugg_by_cat <- cog_explain(by_cat, format = "list")$suggestions
|
||||||
|
expect_gt(length(sugg_by_cat), 0L)
|
||||||
|
|
||||||
|
all_cat <- suppressMessages(cog_spending(gov, 2011L, category = "All Categories"))
|
||||||
|
sugg_all_cat <- cog_explain(all_cat, format = "list")$suggestions
|
||||||
|
expect_gt(length(sugg_all_cat), 0L)
|
||||||
|
|
||||||
|
# The same Corrections recipe that fired per-category must also fire in
|
||||||
|
# all-categories mode -- not just some unrelated recipe.
|
||||||
|
ids_by_cat <- vapply(sugg_by_cat, function(s) s$recipe_id %||% "", character(1))
|
||||||
|
ids_all_cat <- vapply(sugg_all_cat, function(s) s$recipe_id %||% "", character(1))
|
||||||
|
expect_true("corrections_combined" %in% ids_by_cat)
|
||||||
|
expect_true("corrections_combined" %in% ids_all_cat)
|
||||||
|
|
||||||
|
# In all-categories mode the government DOES have other primary spending
|
||||||
|
# in FY2011 (the year itself is not a gap), so the suggestion can only have
|
||||||
|
# fired via the suppressed_component path, not empty_year.
|
||||||
|
corr_all <- sugg_all_cat[[which(ids_all_cat == "corrections_combined")]]
|
||||||
|
expect_identical(corr_all$trigger, "suppressed_component")
|
||||||
|
expect_gt(corr_all$suppressed_amount, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that('"All Categories" candidate scoping is symmetric with .build_verb_sql() -- subtype, not category', {
|
||||||
|
# Direct assertion on the mechanism itself (finding 6): in all-categories
|
||||||
|
# mode .build_suggestions() must scope its candidate recipe sub-select by
|
||||||
|
# subtype_col/subtype_scope, not by the literal "All Categories" value.
|
||||||
|
# Passing all_categories = FALSE with the identical category value proves
|
||||||
|
# the branch -- not merely the subtype_col/subtype_scope arguments' mere
|
||||||
|
# presence -- is what changes the query.
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
|
||||||
|
none <- uscogdata:::.build_suggestions(
|
||||||
|
con, govid = "010000226085", years = 2011L,
|
||||||
|
category = "All Categories", result = NULL, basis = "harmonized",
|
||||||
|
flow_prefixes = c("E", "F", "G"),
|
||||||
|
long_view = "spending_long_harmonized",
|
||||||
|
all_categories = FALSE,
|
||||||
|
subtype_col = "spend_subtype",
|
||||||
|
subtype_scope = c("operations", "capital", "assistance")
|
||||||
|
)
|
||||||
|
expect_length(none, 0L)
|
||||||
|
|
||||||
|
scoped <- uscogdata:::.build_suggestions(
|
||||||
|
con, govid = "010000226085", years = 2011L,
|
||||||
|
category = "All Categories", result = NULL, basis = "harmonized",
|
||||||
|
flow_prefixes = c("E", "F", "G"),
|
||||||
|
long_view = "spending_long_harmonized",
|
||||||
|
all_categories = TRUE,
|
||||||
|
subtype_col = "spend_subtype",
|
||||||
|
subtype_scope = c("operations", "capital", "assistance")
|
||||||
|
)
|
||||||
|
expect_gt(length(scoped), 0L)
|
||||||
|
})
|
||||||
@@ -66,7 +66,7 @@ test_that("inst/sql/26-balance_long.sql enforces NOT is_aggregate (real SQL text
|
|||||||
sql_dir <- system.file("sql", package = "uscogdata")
|
sql_dir <- system.file("sql", package = "uscogdata")
|
||||||
.read_view_sql <- function(filename) {
|
.read_view_sql <- function(filename) {
|
||||||
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
||||||
gsub("\\{url\\}", paste0(tmp, "/"), txt, fixed = FALSE)
|
uscogdata:::.render_view_sql(txt, paste0(tmp, "/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
con <- DBI::dbConnect(duckdb::duckdb())
|
con <- DBI::dbConnect(duckdb::duckdb())
|
||||||
|
|||||||
@@ -29,7 +29,9 @@ test_that("cog_categories(type = 'spending') returns only expenditure rows", {
|
|||||||
# joined with the I/Q/Y flow batch -- the last two characters of Census's
|
# joined with the I/Q/Y flow batch -- the last two characters of Census's
|
||||||
# expenditure taxonomy. `interest` is what makes the three-concept model
|
# expenditure taxonomy. `interest` is what makes the three-concept model
|
||||||
# computable: primary = direct minus debt service.
|
# computable: primary = direct minus debt service.
|
||||||
expect_true(all(r$subtype %in%
|
# Exclude pseudo-category which has NA for subtype
|
||||||
|
r_crosswalk <- r[r$category != "All Categories", ]
|
||||||
|
expect_true(all(r_crosswalk$subtype %in%
|
||||||
c("operations", "capital", "intergovernmental", "assistance",
|
c("operations", "capital", "intergovernmental", "assistance",
|
||||||
"interest", "insurance_benefits")))
|
"interest", "insurance_benefits")))
|
||||||
})
|
})
|
||||||
@@ -54,7 +56,9 @@ test_that("cog_categories(type = 'revenue') returns only revenue rows", {
|
|||||||
# plus the employee-retirement X codes), utility (A91-A94) and liquor store
|
# plus the employee-retirement X codes), utility (A91-A94) and liquor store
|
||||||
# (A90) revenue by definition, which is what makes both of its published
|
# (A90) revenue by definition, which is what makes both of its published
|
||||||
# revenue concepts computable -- see `revenue_concept` in `?cog_revenue`.
|
# revenue concepts computable -- see `revenue_concept` in `?cog_revenue`.
|
||||||
expect_true(all(r$subtype %in%
|
# Exclude pseudo-category which has NA for subtype
|
||||||
|
r_crosswalk <- r[r$category != "All Categories", ]
|
||||||
|
expect_true(all(r_crosswalk$subtype %in%
|
||||||
c("own_source", "federal", "state", "local_aid",
|
c("own_source", "federal", "state", "local_aid",
|
||||||
"insurance_trust", "utility", "liquor_store")))
|
"insurance_trust", "utility", "liquor_store")))
|
||||||
})
|
})
|
||||||
@@ -69,6 +73,8 @@ test_that("cog_categories(pattern = ...) filters case-insensitively", {
|
|||||||
test_that("cog_categories has one row per (category, subtype)", {
|
test_that("cog_categories has one row per (category, subtype)", {
|
||||||
skip_if_no_corpus()
|
skip_if_no_corpus()
|
||||||
r <- cog_categories()
|
r <- cog_categories()
|
||||||
|
# Exclude pseudo-category which is not a crosswalk entry
|
||||||
|
r <- r[r$category != "All Categories", ]
|
||||||
key <- paste(r$category, r$subtype, sep = "|")
|
key <- paste(r$category, r$subtype, sep = "|")
|
||||||
expect_equal(length(key), length(unique(key)))
|
expect_equal(length(key), length(unique(key)))
|
||||||
})
|
})
|
||||||
@@ -76,6 +82,8 @@ test_that("cog_categories has one row per (category, subtype)", {
|
|||||||
test_that("cog_categories item_codes is non-empty comma-separated string", {
|
test_that("cog_categories item_codes is non-empty comma-separated string", {
|
||||||
skip_if_no_corpus()
|
skip_if_no_corpus()
|
||||||
r <- cog_categories()
|
r <- cog_categories()
|
||||||
|
# Exclude pseudo-category which has NA for n_codes and item_codes
|
||||||
|
r <- r[r$category != "All Categories", ]
|
||||||
expect_true(all(nzchar(r$item_codes)))
|
expect_true(all(nzchar(r$item_codes)))
|
||||||
expect_true(all(r$n_codes >= 1L))
|
expect_true(all(r$n_codes >= 1L))
|
||||||
# n_codes should equal count of commas + 1
|
# n_codes should equal count of commas + 1
|
||||||
|
|||||||
@@ -64,3 +64,109 @@ test_that(".resolve_url does not invent a slash for an empty setting", {
|
|||||||
withr::local_options(uscogdata.url = "")
|
withr::local_options(uscogdata.url = "")
|
||||||
expect_equal(.resolve_url(), "")
|
expect_equal(.resolve_url(), "")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test_that("the default corpus URL is real, not a placeholder", {
|
||||||
|
# setup.R points USCOGDATA_URL at the bundled fixture for the whole suite,
|
||||||
|
# so both the env var and the option have to be cleared to see the default.
|
||||||
|
withr::local_envvar(USCOGDATA_URL = NA)
|
||||||
|
withr::local_options(uscogdata.url = NULL)
|
||||||
|
url <- .resolve_url()
|
||||||
|
expect_false(grepl("REPLACE_WITH", url, fixed = TRUE))
|
||||||
|
expect_match(url, "^https://")
|
||||||
|
expect_match(url, "/$")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("an explicitly-set sentinel URL still aborts", {
|
||||||
|
# The guard must survive the default change: a user who half-edited a
|
||||||
|
# copied config still gets the actionable error.
|
||||||
|
withr::local_envvar(
|
||||||
|
USCOGDATA_URL = "https://other.example/s/REPLACE_WITH_SHARE_TOKEN/x/"
|
||||||
|
)
|
||||||
|
expect_error(
|
||||||
|
.check_url_configured(.resolve_url()),
|
||||||
|
class = "uscogdata_url_not_configured"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("DESCRIPTION carries release metadata", {
|
||||||
|
skip_if_no_source_tree("DESCRIPTION")
|
||||||
|
d <- read.dcf(source_tree_path("DESCRIPTION"))
|
||||||
|
fields <- colnames(d)
|
||||||
|
|
||||||
|
expect_true(all(c("URL", "BugReports") %in% fields))
|
||||||
|
expect_match(d[1, "Authors@R"], "Knowles", fixed = TRUE)
|
||||||
|
expect_match(d[1, "Authors@R"], "0000-0003-0005-9478", fixed = TRUE)
|
||||||
|
expect_match(d[1, "Authors@R"], "Civilytics Consulting LLC", fixed = TRUE)
|
||||||
|
|
||||||
|
# The gate in .validate_schema() accepts up to 7 and the published corpus
|
||||||
|
# IS 7; DESCRIPTION must not claim otherwise.
|
||||||
|
expect_equal(as.integer(d[1, "MaxCorpusSchema"]), 7L)
|
||||||
|
|
||||||
|
# Authors@R must actually parse -- a malformed person() call is only
|
||||||
|
# caught at citation()/build time otherwise.
|
||||||
|
people <- eval(parse(text = d[1, "Authors@R"]))
|
||||||
|
expect_s3_class(people, "person")
|
||||||
|
expect_true("cre" %in% unlist(lapply(people, function(p) p$role)))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("LICENSE and LICENSE.md name the same copyright holder", {
|
||||||
|
skip_if_no_source_tree("LICENSE", "LICENSE.md")
|
||||||
|
holder <- sub("^COPYRIGHT HOLDER:\\s*", "",
|
||||||
|
grep("^COPYRIGHT HOLDER:", readLines(source_tree_path("LICENSE"),
|
||||||
|
warn = FALSE), value = TRUE))
|
||||||
|
full <- paste(readLines(source_tree_path("LICENSE.md"), warn = FALSE), collapse = "\n")
|
||||||
|
|
||||||
|
expect_equal(holder, "Civilytics Consulting LLC")
|
||||||
|
expect_match(full, holder, fixed = TRUE)
|
||||||
|
# usethis::use_mit_license() writes LICENSE.md but leaves an existing
|
||||||
|
# LICENSE alone, which is how the two came to disagree in the first place.
|
||||||
|
expect_match(full, "MIT License", fixed = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("vignettes are not excluded from the build", {
|
||||||
|
skip_if_no_source_tree(".Rbuildignore")
|
||||||
|
ignore <- readLines(source_tree_path(".Rbuildignore"), warn = FALSE)
|
||||||
|
expect_false(any(grepl("^\\^vignettes\\$$", ignore)))
|
||||||
|
# The fixture is what lets R CMD check run offline with no credentials on
|
||||||
|
# r-universe and GitHub Actions. It must never be excluded.
|
||||||
|
expect_false(any(grepl("fixture_corpus", ignore, fixed = TRUE)))
|
||||||
|
# doc/ and Meta/ ARE build artefacts of devtools::build_vignettes() and must
|
||||||
|
# stay excluded -- R CMD build regenerates inst/doc/ from vignettes/ on its
|
||||||
|
# own, and leaving them in earns a "non-standard file at top level" NOTE.
|
||||||
|
expect_true(any(grepl("^\\^doc\\$$", ignore)))
|
||||||
|
expect_true(any(grepl("^\\^Meta\\$$", ignore)))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("_pkgdown.yml indexes every exported topic", {
|
||||||
|
skip_if_no_source_tree("_pkgdown.yml", "NAMESPACE")
|
||||||
|
exports <- grep("^export\\(", readLines(source_tree_path("NAMESPACE"), warn = FALSE),
|
||||||
|
value = TRUE)
|
||||||
|
exports <- sub("^export\\((.*)\\)$", "\\1", exports)
|
||||||
|
yml <- paste(readLines(source_tree_path("_pkgdown.yml"), warn = FALSE), collapse = "\n")
|
||||||
|
missing <- exports[!vapply(exports,
|
||||||
|
function(e) grepl(paste0("\\b", e, "\\b"), yml),
|
||||||
|
logical(1))]
|
||||||
|
# pkgdown errors on topics missing from the index, so an unlisted export
|
||||||
|
# means the docs site does not build at all.
|
||||||
|
expect_equal(missing, character(0))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("README is written for a stranger, not a repo insider", {
|
||||||
|
skip_if_no_source_tree("README.md")
|
||||||
|
r <- paste(readLines(source_tree_path("README.md"), warn = FALSE), collapse = "\n")
|
||||||
|
|
||||||
|
# No paths that only resolve inside a maintainer's checkout.
|
||||||
|
expect_false(grepl("../cog_pipeline", r, fixed = TRUE))
|
||||||
|
# A real, uncommented install line.
|
||||||
|
expect_match(r, "install.packages", fixed = TRUE)
|
||||||
|
expect_false(grepl("# pak::pkg_install", r, fixed = TRUE))
|
||||||
|
# The errata most likely to produce a plausible-looking wrong answer.
|
||||||
|
expect_match(r, "full US dollars", fixed = TRUE)
|
||||||
|
# The release advice that conflicts with public CI is gone.
|
||||||
|
expect_false(grepl("Rbuildignore", r, fixed = TRUE))
|
||||||
|
# Both read paths documented.
|
||||||
|
expect_match(r, "cog_mirror", fixed = TRUE)
|
||||||
|
# cog_spending() has no default for `years`; a quickstart that omits it
|
||||||
|
# errors on the reader's first call.
|
||||||
|
expect_match(r, "years\\s*=", perl = TRUE)
|
||||||
|
})
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
# Network-gated. Set USCOGDATA_LIVE_TEST=true to run.
|
||||||
|
#
|
||||||
|
# This file exists because the defect fixed for 0.3.0 -- no remote corpus was
|
||||||
|
# readable at all, because DuckDB cannot expand a glob over generic HTTP --
|
||||||
|
# survived precisely because every other test path used a LOCAL corpus (the
|
||||||
|
# bundled fixture), and so did the API in production (a host mount). Nothing
|
||||||
|
# ever exercised the package the way a new user does.
|
||||||
|
skip_live <- function() {
|
||||||
|
testthat::skip_if_not(
|
||||||
|
identical(tolower(Sys.getenv("USCOGDATA_LIVE_TEST", "")), "true"),
|
||||||
|
"live-corpus test: set USCOGDATA_LIVE_TEST=true to run"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
# The suite's setup.R pins USCOGDATA_URL to the bundled fixture, so reaching
|
||||||
|
# the default requires clearing both the env var and the option.
|
||||||
|
with_default_corpus <- function(code) {
|
||||||
|
withr::local_envvar(
|
||||||
|
USCOGDATA_URL = NA, USCOGDATA_FIXTURE_URL = NA,
|
||||||
|
.local_envir = parent.frame()
|
||||||
|
)
|
||||||
|
withr::local_options(uscogdata.url = NULL, .local_envir = parent.frame())
|
||||||
|
cog_close()
|
||||||
|
withr::defer(cog_close(), envir = parent.frame())
|
||||||
|
force(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
test_that("the package reads the public corpus with no configuration at all", {
|
||||||
|
skip_live()
|
||||||
|
with_default_corpus({
|
||||||
|
g <- cog_gov_search(name = "Madison", state = "WI", type = 2)
|
||||||
|
expect_gt(nrow(g), 0)
|
||||||
|
|
||||||
|
s <- cog_spending(g$canonical_govid[1], years = 2022)
|
||||||
|
expect_gt(nrow(s), 0)
|
||||||
|
expect_true(all(c("amt_nominal", "year", "category") %in% names(s)))
|
||||||
|
|
||||||
|
# Amounts are full dollars, already x1000. A city's annual spending is
|
||||||
|
# millions, not thousands -- this catches a regression that dropped or
|
||||||
|
# doubled the conversion.
|
||||||
|
expect_gt(sum(s$amt_nominal, na.rm = TRUE), 1e6)
|
||||||
|
|
||||||
|
p <- attr(s, "provenance")
|
||||||
|
expect_true(isTRUE(p$transformations$units_conversion$applied))
|
||||||
|
expect_equal(p$transformations$units_conversion$multiplier, 1000)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("a multi-decade query reads across many partitions", {
|
||||||
|
skip_live()
|
||||||
|
with_default_corpus({
|
||||||
|
g <- cog_gov_search(name = "Madison", state = "WI", type = 2)
|
||||||
|
# `years` is required on cog_spending() -- there is no full-history
|
||||||
|
# default at the reader level (the API's /profile route supplies one).
|
||||||
|
s <- cog_spending(g$canonical_govid[1], years = 2000:2022)
|
||||||
|
# Enumeration builds one read_parquet() path per requested partition. If
|
||||||
|
# the list were truncated, or silently collapsed to a single file, the
|
||||||
|
# returned span is what catches it.
|
||||||
|
expect_gt(diff(range(s$year)), 10)
|
||||||
|
expect_gt(length(unique(s$year)), 5)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
test_that(".long_files_sql enumerates every partition the manifest lists", {
|
||||||
|
manifest <- list(files = list(long_partitions = list(
|
||||||
|
list(year = 2011L, path = "data/long/year=2011/part-0.parquet"),
|
||||||
|
list(year = 2012L, path = "data/long/year=2012/part-0.parquet")
|
||||||
|
)))
|
||||||
|
expect_equal(
|
||||||
|
uscogdata:::.long_files_sql("https://example.org/corpus/", manifest),
|
||||||
|
paste0(
|
||||||
|
"['https://example.org/corpus/data/long/year=2011/part-0.parquet',",
|
||||||
|
"'https://example.org/corpus/data/long/year=2012/part-0.parquet']"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".long_files_sql falls back to the glob when no partition list is present", {
|
||||||
|
# test-views.R registers views with a hand-built manifest that has no
|
||||||
|
# `files` element. That must keep working: the glob is valid for the
|
||||||
|
# local paths such a manifest is used with.
|
||||||
|
expect_equal(
|
||||||
|
uscogdata:::.long_files_sql("/tmp/corpus/", list(schema_version = 4L)),
|
||||||
|
"'/tmp/corpus/data/long/**/*.parquet'"
|
||||||
|
)
|
||||||
|
expect_equal(
|
||||||
|
uscogdata:::.long_files_sql("/tmp/corpus/", list(files = list(long_partitions = list()))),
|
||||||
|
"'/tmp/corpus/data/long/**/*.parquet'"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("the enumerated list matches the bundled fixture's partition count", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
m <- jsonlite::fromJSON(
|
||||||
|
file.path(fixture_corpus_path(), "manifest.json"), simplifyVector = FALSE
|
||||||
|
)
|
||||||
|
out <- uscogdata:::.long_files_sql(fixture_corpus_path(), m)
|
||||||
|
expect_equal(
|
||||||
|
lengths(regmatches(out, gregexpr("part-0\\.parquet", out))),
|
||||||
|
length(m$files$long_partitions)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("no view SQL survives rendering with an unsubstituted token", {
|
||||||
|
# Introducing {long_files} broke four test sites that had hand-rolled the
|
||||||
|
# {url} substitution -- each failed with a DuckDB parser error on the
|
||||||
|
# surviving brace. This asserts the whole SQL directory renders clean, so
|
||||||
|
# a future token cannot reintroduce that silently.
|
||||||
|
sql_dir <- system.file("sql", package = "uscogdata")
|
||||||
|
for (f in list.files(sql_dir, pattern = "\\.sql$", full.names = TRUE)) {
|
||||||
|
rendered <- uscogdata:::.render_view_sql(
|
||||||
|
paste(readLines(f, warn = FALSE), collapse = "\n"), "/tmp/corpus/"
|
||||||
|
)
|
||||||
|
expect_false(grepl("\\{[a-z_]+\\}", rendered), label = basename(f))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("registered `long` view reads through the enumerated list", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
with_fixture_corpus({
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
n <- DBI::dbGetQuery(con, "SELECT count(*) AS n FROM long")$n
|
||||||
|
expect_gt(n, 0)
|
||||||
|
yrs <- DBI::dbGetQuery(con, "SELECT DISTINCT year FROM long ORDER BY year")$year
|
||||||
|
expect_true(all(c(2011, 2012, 2019, 2020) %in% yrs))
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -4,12 +4,14 @@
|
|||||||
# protect users from silent failures when USCOGDATA_URL is misconfigured
|
# protect users from silent failures when USCOGDATA_URL is misconfigured
|
||||||
# or returns non-JSON content.
|
# or returns non-JSON content.
|
||||||
|
|
||||||
test_that("cog_open aborts with actionable error when URL is the placeholder default", {
|
test_that("cog_open aborts with actionable error when URL contains the sentinel", {
|
||||||
uscogdata:::cog_close()
|
uscogdata:::cog_close()
|
||||||
on.exit(uscogdata:::cog_close(), add = TRUE)
|
on.exit(uscogdata:::cog_close(), add = TRUE)
|
||||||
|
|
||||||
placeholder <- "https://cloud.civilytics.org/s/REPLACE_WITH_SHARE_TOKEN/download/"
|
# No longer the package default (that is the public HF corpus). This is a
|
||||||
withr::with_envvar(c(USCOGDATA_URL = placeholder), {
|
# user who copied a config template and did not finish editing it.
|
||||||
|
sentinel_url <- "https://cloud.civilytics.org/s/REPLACE_WITH_SHARE_TOKEN/download/"
|
||||||
|
withr::with_envvar(c(USCOGDATA_URL = sentinel_url), {
|
||||||
expect_error(
|
expect_error(
|
||||||
uscogdata:::cog_open(),
|
uscogdata:::cog_open(),
|
||||||
class = "uscogdata_url_not_configured"
|
class = "uscogdata_url_not_configured"
|
||||||
@@ -35,8 +37,10 @@ test_that("placeholder guard error names both env var and option as remediation"
|
|||||||
uscogdata:::cog_close()
|
uscogdata:::cog_close()
|
||||||
on.exit(uscogdata:::cog_close(), add = TRUE)
|
on.exit(uscogdata:::cog_close(), add = TRUE)
|
||||||
|
|
||||||
placeholder <- "https://cloud.civilytics.org/s/REPLACE_WITH_SHARE_TOKEN/download/"
|
# No longer the package default (that is the public HF corpus). This is a
|
||||||
withr::with_envvar(c(USCOGDATA_URL = placeholder), {
|
# user who copied a config template and did not finish editing it.
|
||||||
|
sentinel_url <- "https://cloud.civilytics.org/s/REPLACE_WITH_SHARE_TOKEN/download/"
|
||||||
|
withr::with_envvar(c(USCOGDATA_URL = sentinel_url), {
|
||||||
msg <- tryCatch(uscogdata:::cog_open(), error = conditionMessage)
|
msg <- tryCatch(uscogdata:::cog_open(), error = conditionMessage)
|
||||||
expect_match(msg, "USCOGDATA_URL", fixed = TRUE)
|
expect_match(msg, "USCOGDATA_URL", fixed = TRUE)
|
||||||
expect_match(msg, "uscogdata.url", fixed = TRUE)
|
expect_match(msg, "uscogdata.url", fixed = TRUE)
|
||||||
@@ -111,7 +115,7 @@ test_that("cog_manifest returns the active session's parsed manifest", {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test_that(".validate_schema accepts schema_version 4, 5 and 6, rejects others", {
|
test_that(".validate_schema accepts schema_version 4 through 7, rejects others", {
|
||||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 4L)))
|
expect_silent(uscogdata:::.validate_schema(list(schema_version = 4L)))
|
||||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 5L)))
|
expect_silent(uscogdata:::.validate_schema(list(schema_version = 5L)))
|
||||||
# v6 = FIPS geography harmonization (2026-07-22): _code -> _asof rename +
|
# v6 = FIPS geography harmonization (2026-07-22): _code -> _asof rename +
|
||||||
@@ -119,12 +123,22 @@ test_that(".validate_schema accepts schema_version 4, 5 and 6, rejects others",
|
|||||||
# renamed columns and its geography comes from the xwalk, so v6 is accepted
|
# renamed columns and its geography comes from the xwalk, so v6 is accepted
|
||||||
# without behavioural change -- see .validate_schema()'s note.
|
# without behavioural change -- see .validate_schema()'s note.
|
||||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 6L)))
|
expect_silent(uscogdata:::.validate_schema(list(schema_version = 6L)))
|
||||||
|
# v7 = `data_year` APPENDED as column 29 (cog_pipeline #80, 2026-08-03), the
|
||||||
|
# most recent fiscal year contributing to a collapsed key. Appended, never
|
||||||
|
# inserted: canonical_govid stays at position 26, so nothing this package
|
||||||
|
# reads shifts. Verified against the real v7 corpus before widening the
|
||||||
|
# allow-list -- cog_spending()/cog_balances() return correctly for FY2024 AND
|
||||||
|
# for FY2012, so the new column is inert here.
|
||||||
|
expect_silent(uscogdata:::.validate_schema(list(schema_version = 7L)))
|
||||||
expect_error(
|
expect_error(
|
||||||
uscogdata:::.validate_schema(list(schema_version = 3L)),
|
uscogdata:::.validate_schema(list(schema_version = 3L)),
|
||||||
"schema_version"
|
"schema_version"
|
||||||
)
|
)
|
||||||
|
# The upper bound still has to be ENFORCED, not just moved. Without this the
|
||||||
|
# test would no longer prove that an unknown future schema is refused, and a
|
||||||
|
# v8 corpus with a genuinely breaking change would sail through.
|
||||||
expect_error(
|
expect_error(
|
||||||
uscogdata:::.validate_schema(list(schema_version = 7L)),
|
uscogdata:::.validate_schema(list(schema_version = 8L)),
|
||||||
"schema_version"
|
"schema_version"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -214,3 +214,255 @@ test_that("no signposting under basis = 'raw'", {
|
|||||||
prov <- attr(r, "provenance")
|
prov <- attr(r, "provenance")
|
||||||
expect_length(prov$suggestions, 0L)
|
expect_length(prov$suggestions, 0L)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# --- uscogdata#9: partial-coverage signposting ------------------------------
|
||||||
|
|
||||||
|
test_that("no recipe component is ever renamed by harmonization", {
|
||||||
|
# The suppression trigger anti-joins the verb's long view on item_code.
|
||||||
|
# That is only sound because harmonization never rewrites a recipe
|
||||||
|
# component's code -- every component whose harmonized_code differs has
|
||||||
|
# harmonized_code IS NULL (and is aggregate-flagged). If this ever fails,
|
||||||
|
# .suppressed_components() would report reachable dollars as suppressed.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
n <- DBI::dbGetQuery(con,
|
||||||
|
"SELECT COUNT(*) AS renamed FROM long
|
||||||
|
WHERE item_code IN (SELECT DISTINCT component_code FROM harmonization_recipes)
|
||||||
|
AND harmonized_code IS NOT NULL
|
||||||
|
AND harmonized_code <> item_code")$renamed
|
||||||
|
expect_equal(as.integer(n), 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".select_long_view maps annotated view bases to their long views", {
|
||||||
|
expect_equal(
|
||||||
|
uscogdata:::.select_long_view("spending_annotated", "harmonized"),
|
||||||
|
"spending_long_harmonized")
|
||||||
|
expect_equal(
|
||||||
|
uscogdata:::.select_long_view("revenue_annotated", "harmonized"),
|
||||||
|
"revenue_long_harmonized")
|
||||||
|
expect_equal(
|
||||||
|
uscogdata:::.select_long_view("spending_annotated", "raw"),
|
||||||
|
"spending_long")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".suppressed_components measures the E67/E68 dollars Public Welfare drops", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
s <- uscogdata:::.suppressed_components(
|
||||||
|
con,
|
||||||
|
candidates = c("welfare_cash_e67_wide", "welfare_cash_e68_wide"),
|
||||||
|
govid = "061037123085", years = 2011L,
|
||||||
|
long_view = "spending_long_harmonized",
|
||||||
|
flow_prefixes = c("E", "F", "G"))
|
||||||
|
|
||||||
|
expect_s3_class(s, "tbl_df")
|
||||||
|
expect_equal(nrow(s), 2L)
|
||||||
|
s <- s[order(s$recipe_id), ]
|
||||||
|
expect_equal(s$recipe_id, c("welfare_cash_e67_wide", "welfare_cash_e68_wide"))
|
||||||
|
expect_equal(s$suppressed_amount, c(1803872000, 271589000))
|
||||||
|
expect_equal(s$suppressed_codes, c("E67", "E68"))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".suppressed_components finds nothing in a modern year", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
s <- uscogdata:::.suppressed_components(
|
||||||
|
con,
|
||||||
|
candidates = c("welfare_cash_e67_wide", "welfare_cash_e68_wide"),
|
||||||
|
govid = "061037123085", years = 2019L,
|
||||||
|
long_view = "spending_long_harmonized",
|
||||||
|
flow_prefixes = c("E", "F", "G"))
|
||||||
|
expect_equal(nrow(s), 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".suppressed_components rejects a long_view outside the allowlist", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
expect_error(
|
||||||
|
uscogdata:::.suppressed_components(
|
||||||
|
con, candidates = "welfare_cash_e67_wide", govid = "061037123085",
|
||||||
|
years = 2011L, long_view = "long; DROP TABLE x",
|
||||||
|
flow_prefixes = c("E", "F", "G")),
|
||||||
|
class = "uscogdata_internal_error")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that(".suppressed_components never measures a component from the other flow family (I1)", {
|
||||||
|
# uscogdata#9 review, finding I1: without the flow_prefixes filter, a
|
||||||
|
# candidate recipe entirely outside the calling verb's own flow family is
|
||||||
|
# ALWAYS absent from that verb's view (by construction), so it was always
|
||||||
|
# reported as "suppressed" -- fabricating a dollar claim. E67/E68 are
|
||||||
|
# Public Welfare EXPENDITURE codes; scoping the measurement to revenue's
|
||||||
|
# own flow_prefixes must find nothing for them.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
s <- uscogdata:::.suppressed_components(
|
||||||
|
con,
|
||||||
|
candidates = c("welfare_cash_e67_wide", "welfare_cash_e68_wide"),
|
||||||
|
govid = "061037123085", years = 2011L,
|
||||||
|
long_view = "revenue_long_harmonized",
|
||||||
|
flow_prefixes = c("T", "A", "U", "B", "C", "D"))
|
||||||
|
expect_equal(nrow(s), 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: Public Welfare signposts its suppressed E67/E68 dollars", {
|
||||||
|
# The bug: E74/E79 return rows for FY2011, so there is no row-absence gap,
|
||||||
|
# so nothing fired -- while E67 ($1,803,872,000) and E68 ($271,589,000) were
|
||||||
|
# dropped for being aggregate-published. LA County reports $3,185,943,000
|
||||||
|
# and omits $2,075,461,000, a 39% understatement, silently.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- suppressMessages(
|
||||||
|
cog_spending("061037123085", years = 2011L, category = "Public Welfare"))
|
||||||
|
sugg <- attr(r, "provenance")$suggestions
|
||||||
|
|
||||||
|
expect_length(sugg, 2L)
|
||||||
|
ids <- vapply(sugg, function(s) s$recipe_id, character(1))
|
||||||
|
expect_setequal(ids, c("welfare_cash_e67_wide", "welfare_cash_e68_wide"))
|
||||||
|
|
||||||
|
e67 <- sugg[[which(ids == "welfare_cash_e67_wide")]]
|
||||||
|
expect_equal(e67$trigger, "suppressed_component")
|
||||||
|
expect_equal(e67$suppressed_amount, 1803872000)
|
||||||
|
expect_equal(e67$suppressed_years, 2011L)
|
||||||
|
expect_equal(e67$suppressed_codes, "E67")
|
||||||
|
expect_equal(e67$hint, "re-run with recipe = 'welfare_cash_e67_wide'")
|
||||||
|
|
||||||
|
e68 <- sugg[[which(ids == "welfare_cash_e68_wide")]]
|
||||||
|
expect_equal(e68$trigger, "suppressed_component")
|
||||||
|
expect_equal(e68$suppressed_amount, 271589000)
|
||||||
|
expect_equal(e68$suppressed_codes, "E68")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: an empty_year fire keeps its trigger and gains the dollars", {
|
||||||
|
# Corrections is the case that already worked: zero rows in FY2011, so the
|
||||||
|
# row-absence path fires. It must keep firing, keep trigger = "empty_year",
|
||||||
|
# keep its IG counterpart -- and now also report what was suppressed.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- suppressMessages(
|
||||||
|
cog_spending("061037123085", years = 2011L, category = "Corrections"))
|
||||||
|
sugg <- attr(r, "provenance")$suggestions
|
||||||
|
|
||||||
|
expect_length(sugg, 3L)
|
||||||
|
ids <- vapply(sugg, function(s) s$recipe_id, character(1))
|
||||||
|
expect_setequal(ids, c("corrections_combined", "corrections_capital_combined",
|
||||||
|
"corrections_other_capital_combined"))
|
||||||
|
expect_true(all(vapply(sugg, function(s) s$trigger, character(1)) == "empty_year"))
|
||||||
|
|
||||||
|
cc <- sugg[[which(ids == "corrections_combined")]]
|
||||||
|
expect_equal(cc$suppressed_amount, 1371460000)
|
||||||
|
expect_equal(cc$suppressed_codes, "E05")
|
||||||
|
expect_equal(cc$ig_recipe_id, "corrections_ig_local_combined")
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: the revenue verb inherits the same trigger", {
|
||||||
|
# Alaska state FY2011 Miscellaneous Revenue reports $943,842,000 from
|
||||||
|
# U11/U20/U30 while dropping $1,899,995,000 of aggregate-published `U4-`
|
||||||
|
# rents and royalties -- the omission is LARGER than the reported figure.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- suppressMessages(
|
||||||
|
cog_revenue("020000227749", years = 2011L,
|
||||||
|
category = "Miscellaneous Revenue"))
|
||||||
|
sugg <- attr(r, "provenance")$suggestions
|
||||||
|
|
||||||
|
expect_length(sugg, 1L)
|
||||||
|
expect_equal(sugg[[1]]$recipe_id, "rents_royalties_u4_wide")
|
||||||
|
expect_equal(sugg[[1]]$trigger, "suppressed_component")
|
||||||
|
expect_equal(sugg[[1]]$suppressed_amount, 1899995000)
|
||||||
|
expect_equal(sugg[[1]]$suppressed_codes, "U4-")
|
||||||
|
# A revenue recipe must never be handed an M/L expenditure counterpart.
|
||||||
|
expect_null(sugg[[1]]$ig_recipe_id)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("I1: cog_revenue never fabricates suppressed dollars for an expenditure-only recipe", {
|
||||||
|
# uscogdata#9 review, finding I1: Corrections is an expenditure-only
|
||||||
|
# category (E04/E05). cog_revenue() naturally returns zero rows for it, so
|
||||||
|
# corrections_combined still fires as an empty_year suggestion (its own
|
||||||
|
# generic join finds real E04/E05 data for this government) -- but before
|
||||||
|
# the flow_prefixes fix, .suppressed_components() measured E04/E05 against
|
||||||
|
# cog_revenue()'s OWN view (which can never contain an E-coded row by
|
||||||
|
# construction) and reported the full $3,631,945,000 as "suppressed",
|
||||||
|
# when cog_spending() for the same gov/years/category actually returns
|
||||||
|
# $3,691,029,000 -- nothing was suppressed at all.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- suppressMessages(
|
||||||
|
cog_revenue("061037123085", years = 2019:2020, category = "Corrections"))
|
||||||
|
sugg <- attr(r, "provenance")$suggestions
|
||||||
|
ids <- vapply(sugg, function(s) s$recipe_id, character(1))
|
||||||
|
expect_true("corrections_combined" %in% ids)
|
||||||
|
|
||||||
|
hit <- sugg[[which(ids == "corrections_combined")]]
|
||||||
|
expect_equal(hit$suppressed_amount, 0)
|
||||||
|
expect_equal(hit$suppressed_years, integer(0))
|
||||||
|
expect_equal(hit$suppressed_codes, character(0))
|
||||||
|
|
||||||
|
# And cog_spending() for the identical gov/years/category is unaffected --
|
||||||
|
# it actually finds the E04/E05 dollars the buggy measurement claimed were
|
||||||
|
# excluded.
|
||||||
|
sp <- suppressMessages(
|
||||||
|
cog_spending("061037123085", years = 2019:2020, category = "Corrections"))
|
||||||
|
expect_equal(sum(sp$amt_nominal), 3691029000)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: no partial-coverage fire in a modern year", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- cog_spending("061037123085", years = 2019L, category = "Public Welfare")
|
||||||
|
expect_length(attr(r, "provenance")$suggestions, 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: leaf-and-classified wide-era families never fire", {
|
||||||
|
# higher_ed_e18_wide and general_gov_e89_wide are the control group: their
|
||||||
|
# components (E16/E18, E85/E89) are ordinary classified leaves even in the
|
||||||
|
# wide era, so widening the trigger must leave them silent. This is the
|
||||||
|
# measurement that refutes "it would fire on every category in every legacy
|
||||||
|
# year" -- corpus-wide on the fixture, these two produce zero suppressed rows.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
con <- uscogdata:::.ensure_session()
|
||||||
|
n <- DBI::dbGetQuery(con,
|
||||||
|
"SELECT COUNT(*) AS n
|
||||||
|
FROM long l
|
||||||
|
JOIN harmonization_recipes r
|
||||||
|
ON l.item_code = r.component_code
|
||||||
|
AND l.year BETWEEN r.year_min AND r.year_max
|
||||||
|
WHERE r.recipe_id IN ('higher_ed_e18_wide', 'general_gov_e89_wide')
|
||||||
|
AND l.amt <> 0
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM spending_long_harmonized v
|
||||||
|
WHERE v.canonical_govid = l.canonical_govid
|
||||||
|
AND v.year = l.year AND v.item_code = l.item_code)")$n
|
||||||
|
expect_equal(as.integer(n), 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: the cli message reports the suppressed dollars", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
expect_message(
|
||||||
|
cog_spending("061037123085", years = 2011L, category = "Public Welfare"),
|
||||||
|
"1,803,872,000", fixed = TRUE)
|
||||||
|
expect_message(
|
||||||
|
cog_spending("061037123085", years = 2011L, category = "Public Welfare"),
|
||||||
|
"FY2011", fixed = TRUE)
|
||||||
|
expect_message(
|
||||||
|
cog_spending("061037123085", years = 2011L, category = "Public Welfare"),
|
||||||
|
"E67", fixed = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("uscogdata#9: cog_explain() reports the suppressed dollars", {
|
||||||
|
# cog_explain()'s whole "print" output -- including the Suggestions
|
||||||
|
# section built from cli::cli_ul() -- is emitted on the message stream
|
||||||
|
# (verified empirically 2026-08-04: capture.output(..., type = "output")
|
||||||
|
# returns character(0) for this call; testthat::capture_messages() is what
|
||||||
|
# actually carries it), so that is the stream this test captures.
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- suppressMessages(
|
||||||
|
cog_spending("061037123085", years = 2011L, category = "Public Welfare"))
|
||||||
|
out <- paste(testthat::capture_messages(cog_explain(r)), collapse = "")
|
||||||
|
expect_match(out, "271,589,000", fixed = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("the provenance schema documents the suggestion trigger fields", {
|
||||||
|
sch <- jsonlite::fromJSON(
|
||||||
|
system.file("schemas", "provenance-v1.json", package = "uscogdata"),
|
||||||
|
simplifyVector = FALSE)
|
||||||
|
props <- sch$properties$suggestions$items$properties
|
||||||
|
expect_true(all(c("trigger", "suppressed_amount", "suppressed_years",
|
||||||
|
"suppressed_codes") %in% names(props)))
|
||||||
|
expect_setequal(unlist(props$trigger$enum),
|
||||||
|
c("empty_year", "suppressed_component"))
|
||||||
|
})
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# Mirror of test-spending-pagination.R for cog_revenue(), which shares the
|
||||||
|
# same .verb_spendrev()/.build_verb_sql() pushdown -- see that file for the
|
||||||
|
# incident this fixes.
|
||||||
|
|
||||||
|
test_that("cog_revenue limit/offset page correctly and report total_rows", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_revenue("121011212191", years = 2019:2020, category = NULL)
|
||||||
|
page <- cog_revenue("121011212191", years = 2019:2020, category = NULL,
|
||||||
|
limit = 5L, offset = 3L)
|
||||||
|
expect_equal(nrow(page), 5L)
|
||||||
|
expect_equal(page[c("year", "canonical_govid", "revenue_subtype", "category")],
|
||||||
|
full[4:8, c("year", "canonical_govid", "revenue_subtype", "category")],
|
||||||
|
ignore_attr = TRUE)
|
||||||
|
expect_equal(attr(page, "total_rows"), nrow(full))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("cog_revenue limit unset by default leaves total_rows absent", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- cog_revenue("121011212191", 2020L, "Property Tax")
|
||||||
|
expect_null(attr(r, "total_rows"))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("cog_revenue complete + limit conflict aborts the same way as cog_spending", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
expect_error(
|
||||||
|
cog_revenue("121011212191", 2020L, "Property Tax", complete = TRUE, limit = 5L),
|
||||||
|
class = "uscogdata_complete_pagination_conflict"
|
||||||
|
)
|
||||||
|
})
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
# cog-api's paginate() used to slice an already-fully-materialized result:
|
||||||
|
# every page of a deep sweep re-ran the whole query and re-listified every
|
||||||
|
# row, just to keep 1000 and discard the rest. For a 193,105-row fleet-wide
|
||||||
|
# query walked 194 pages deep, that repeated the full cost 194 times and
|
||||||
|
# wedged the production server for hours (2026-08-06 incident). limit/offset
|
||||||
|
# here push the slice into the SQL itself, so a page costs O(limit), not
|
||||||
|
# O(full result).
|
||||||
|
|
||||||
|
test_that("limit without offset returns the first page, matching the unpaginated head", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_spending("121011212191", years = 2019:2020, category = NULL)
|
||||||
|
page <- cog_spending("121011212191", years = 2019:2020, category = NULL,
|
||||||
|
limit = 10L)
|
||||||
|
expect_equal(nrow(page), 10L)
|
||||||
|
expect_equal(page[c("year", "canonical_govid", "spend_subtype", "category")],
|
||||||
|
full[1:10, c("year", "canonical_govid", "spend_subtype", "category")],
|
||||||
|
ignore_attr = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("offset skips ahead without gaps or overlap", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_spending("121011212191", years = 2019:2020, category = NULL)
|
||||||
|
page2 <- cog_spending("121011212191", years = 2019:2020, category = NULL,
|
||||||
|
limit = 10L, offset = 10L)
|
||||||
|
expect_equal(nrow(page2), 10L)
|
||||||
|
expect_equal(page2[c("year", "canonical_govid", "spend_subtype", "category")],
|
||||||
|
full[11:20, c("year", "canonical_govid", "spend_subtype", "category")],
|
||||||
|
ignore_attr = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("walking every page reconstructs the unpaginated result exactly", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_spending("121011212191", years = 2019:2020, category = NULL)
|
||||||
|
n <- nrow(full)
|
||||||
|
limit <- 7L
|
||||||
|
pages <- list()
|
||||||
|
offset <- 0L
|
||||||
|
repeat {
|
||||||
|
p <- cog_spending("121011212191", years = 2019:2020, category = NULL,
|
||||||
|
limit = limit, offset = offset)
|
||||||
|
if (nrow(p) == 0L) break
|
||||||
|
pages[[length(pages) + 1L]] <- p
|
||||||
|
offset <- offset + limit
|
||||||
|
if (offset > n + limit) stop("test runaway: paging did not terminate")
|
||||||
|
}
|
||||||
|
walked <- dplyr::bind_rows(pages)
|
||||||
|
expect_equal(nrow(walked), n)
|
||||||
|
key_cols <- c("year", "canonical_govid", "spend_subtype", "category", "amt_nominal")
|
||||||
|
expect_equal(walked[key_cols], full[key_cols], ignore_attr = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("total_rows attribute reports the full unpaginated count", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_spending("121011212191", years = 2019:2020, category = NULL)
|
||||||
|
page <- cog_spending("121011212191", years = 2019:2020, category = NULL,
|
||||||
|
limit = 5L, offset = 0L)
|
||||||
|
expect_equal(attr(page, "total_rows"), nrow(full))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("offset past the end returns zero rows, not an error", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_spending("121011212191", years = 2019:2020, category = NULL)
|
||||||
|
page <- cog_spending("121011212191", years = 2019:2020, category = NULL,
|
||||||
|
limit = 10L, offset = nrow(full) + 100L)
|
||||||
|
expect_equal(nrow(page), 0L)
|
||||||
|
expect_equal(attr(page, "total_rows"), nrow(full))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("limit is unset by default -- unpaginated calls are unaffected", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
r <- cog_spending("121011212191", 2020L, "Corrections")
|
||||||
|
expect_null(attr(r, "total_rows"))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("per_capita and adjust_to_year still apply correctly within a page", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
full <- cog_spending("121011212191", years = 2020L, category = NULL,
|
||||||
|
per_capita = TRUE, adjust_to_year = 2022L)
|
||||||
|
page <- cog_spending("121011212191", years = 2020L, category = NULL,
|
||||||
|
per_capita = TRUE, adjust_to_year = 2022L,
|
||||||
|
limit = 3L, offset = 2L)
|
||||||
|
expect_equal(page[c("amt_nominal", "amt_real", "amt_per_capita_nominal",
|
||||||
|
"amt_per_capita_real")],
|
||||||
|
full[3:5, c("amt_nominal", "amt_real", "amt_per_capita_nominal",
|
||||||
|
"amt_per_capita_real")],
|
||||||
|
ignore_attr = TRUE)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("complete = TRUE with limit aborts -- pagination over a partial grid is undefined", {
|
||||||
|
skip_if_no_corpus()
|
||||||
|
expect_error(
|
||||||
|
cog_spending("121011212191", 2020L, "Corrections", complete = TRUE, limit = 5L),
|
||||||
|
class = "uscogdata_complete_pagination_conflict"
|
||||||
|
)
|
||||||
|
})
|
||||||
@@ -103,7 +103,7 @@ test_that("inst/sql/22- and 23- harmonized views enforce every WHERE predicate (
|
|||||||
sql_dir <- system.file("sql", package = "uscogdata")
|
sql_dir <- system.file("sql", package = "uscogdata")
|
||||||
.read_view_sql <- function(filename) {
|
.read_view_sql <- function(filename) {
|
||||||
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
||||||
gsub("\\{url\\}", paste0(tmp, "/"), txt, fixed = FALSE)
|
uscogdata:::.render_view_sql(txt, paste0(tmp, "/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
con <- DBI::dbConnect(duckdb::duckdb())
|
con <- DBI::dbConnect(duckdb::duckdb())
|
||||||
@@ -183,7 +183,7 @@ test_that("inst/sql/24- and 25- IG views retain aggregates, COALESCE NULL harmon
|
|||||||
sql_dir <- system.file("sql", package = "uscogdata")
|
sql_dir <- system.file("sql", package = "uscogdata")
|
||||||
.read_view_sql <- function(filename) {
|
.read_view_sql <- function(filename) {
|
||||||
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
||||||
gsub("\\{url\\}", paste0(tmp, "/"), txt, fixed = FALSE)
|
uscogdata:::.render_view_sql(txt, paste0(tmp, "/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
con <- DBI::dbConnect(duckdb::duckdb())
|
con <- DBI::dbConnect(duckdb::duckdb())
|
||||||
@@ -335,7 +335,7 @@ test_that(".harmonization_view_files guard is necessary: registration against a
|
|||||||
sql_dir <- system.file("sql", package = "uscogdata")
|
sql_dir <- system.file("sql", package = "uscogdata")
|
||||||
.read_view_sql <- function(filename) {
|
.read_view_sql <- function(filename) {
|
||||||
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
txt <- paste(readLines(file.path(sql_dir, filename), warn = FALSE), collapse = "\n")
|
||||||
gsub("\\{url\\}", url, txt, fixed = FALSE)
|
uscogdata:::.render_view_sql(txt, url)
|
||||||
}
|
}
|
||||||
con2 <- DBI::dbConnect(duckdb::duckdb())
|
con2 <- DBI::dbConnect(duckdb::duckdb())
|
||||||
on.exit(DBI::dbDisconnect(con2, shutdown = TRUE), add = TRUE)
|
on.exit(DBI::dbDisconnect(con2, shutdown = TRUE), add = TRUE)
|
||||||
|
|||||||
Reference in New Issue
Block a user