From 916212c32750258ad04a8ad0121c67b85102f9e8 Mon Sep 17 00:00:00 2001 From: Jared Knowles Date: Wed, 29 Apr 2026 19:02:37 -0400 Subject: [PATCH] feat(explain): render new per-capita provenance fields cog_explain() now prints denominator_source, popyear_range, and pop_source_counts under the Transformations section. --- R/explain.R | 10 ++++++++++ tests/testthat/test-explain.R | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/R/explain.R b/R/explain.R index 7bf3961..2e7a537 100644 --- a/R/explain.R +++ b/R/explain.R @@ -74,6 +74,16 @@ cog_explain <- function(result, format = c("print", "list")) { pc <- prov$transformations$per_capita if (isTRUE(pc$applied)) { cli::cli_text("Per-capita denominator: {pc$denominator_source}") + if (length(pc$popyear_range) == 2L) { + cli::cli_text( + " popyear range: {pc$popyear_range[1]}-{pc$popyear_range[2]}" + ) + } + if (!is.null(pc$pop_source_counts)) { + cli::cli_text( + " pop_source counts: census_f33={pc$pop_source_counts$census_f33}, unavailable={pc$pop_source_counts$unavailable}" + ) + } } infl <- prov$transformations$inflation if (isTRUE(infl$applied)) { diff --git a/tests/testthat/test-explain.R b/tests/testthat/test-explain.R index ee4b2c1..127660d 100644 --- a/tests/testthat/test-explain.R +++ b/tests/testthat/test-explain.R @@ -30,3 +30,18 @@ test_that("cog_explain errors on non-verb input", { df <- tibble::tibble(a = 1) expect_error(cog_explain(df), "provenance") }) + +test_that("cog_explain prints denominator + popyear_range + counts", { + skip_if_no_corpus() + with_fixture_corpus({ + r <- cog_spending("101006006", years = 2019:2020, + category = "Police", per_capita = TRUE) + out <- paste(c( + capture.output(cog_explain(r)), + capture.output(cog_explain(r), type = "message") + ), collapse = "\n") + expect_true(grepl("Census F-33", out)) + expect_true(grepl("popyear", out, ignore.case = TRUE)) + expect_true(grepl("census_f33", out)) + }) +})