Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,24 @@ To add a new ACS table to the package:

1. **Add a `register_table()` call in `R/table_registry.R`** with:
- `raw_variables` (manual) or `raw_variable_source` (select_variables) for raw ACS variables
- `compute_fn` that calculates derived indicators using `safe_divide()` and `dplyr::across()`
- `codebook_entries` with structured entries (types: `simple_percent`, `across_percent`, `across_sum`, `complex`, `one_minus`, `metadata`)
- `definitions` using the DSL functions: `define_percent()`, `define_sum()`, `define_complement()`, `define_metadata()`
2. **Add any new global variables** to the `utils::globalVariables()` call at the bottom of `R/table_registry.R`
3. **Verify**: `devtools::load_all()` then `list_tables()` shows your table
4. **Verify codebook**: the codebook auto-generates from `codebook_entries` -- no changes to `R/generate_codebook.R` needed
4. **Verify codebook**: the codebook auto-generates from `definitions` -- no changes to `R/generate_codebook.R` needed
5. **Verify MOEs**: `R/calculate_cvs.R` parses codebook definition strings -- no changes needed if definitions follow standard patterns
6. **Update pretty names** if needed (`R/make_pretty_names.R` -- rarely needed)

### Codebook entry types
### DSL functions for definitions

| Type | Use case | Key fields |
| Function | Use case | Key params |
|---|---|---|
| `simple_percent` | Single numerator / denominator | `output`, `numerator`, `denominator` |
| `across_percent` | `dplyr::across()` percentages | `input_regex`, `exclude_regex`, `output_suffix`, `denominator` or `denominator_fn` |
| `across_sum` | `dplyr::across()` sums (e.g., male + female) | `input_regex`, `addend_fn`, `output_naming_fn` |
| `complex` | Multi-variable numerator/denominator | `output`, `numerator_regex` or `numerator_variables`, `denominator_variables`, optional `subtract_*` (denominator) or `numerator_subtract_*` (numerator) |
| `one_minus` | Complement (1 - x) | `output`, `source_variable` |
| `metadata` | Non-computed variables | `output`, `definition_text` |
| `define_percent(numerator, denominator)` | Single percentage | `numerator`, `denominator`, `output` (inferred) |
| `define_percent(..., each = TRUE)` | Batch percentages | `numerator` (regex), `denominator` or `denominator_replace`, `exclude` |
| `define_percent(numerator, denominator, subtract_from_*)` | Complex percentage | `subtract_from_numerator`, `subtract_from_denominator`, `exclude` |
| `define_sum(columns, output)` | Sum columns | `columns` (character vector) |
| `define_sum(..., each = TRUE)` | Batch pairwise sums | `columns` (regex), `add_replace`, `output_replace` |
| `define_complement(source, output)` | Complement (1 - x) | `source`, `output` |
| `define_metadata(output, definition)` | Non-computed variables | `output`, `definition` |

### Quality checks for new variables

Expand Down
5 changes: 2 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

export("%>%")
export(compile_acs_data)
export(define_across_percent)
export(define_across_sum)
export(define_complement)
export(define_metadata)
export(define_one_minus)
export(define_percent)
export(define_sum)
export(filter_variables)
export(get_acs_codebook)
export(interpolate_acs)
Expand Down
3 changes: 2 additions & 1 deletion R/auto_percent.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ generate_auto_definitions = function(nodes, denominator_mode = "parent",
} else {
output = paste0(numerator, "_percent")
}
define_percent(output = output, numerator = numerator, denominator = denominator)
list(type = "simple_percent", output = output,
numerator = numerator, denominator = denominator)
}) %>% purrr::compact()
}

Expand Down
19 changes: 9 additions & 10 deletions R/compile_acs_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ safe_divide = function(x, y) { dplyr::if_else(y == 0, 0, x / y) }
#' Use the \code{denominator} parameter to control how percentages are
#' calculated for these tables.
#' \item \strong{DSL definition objects} created with \code{\link{define_percent}},
#' \code{\link{define_across_percent}}, \code{\link{define_across_sum}},
#' \code{\link{define_one_minus}}, or \code{\link{define_metadata}}.
#' These let you compute custom derived variables from the columns
#' produced by the tables you request. User definitions are executed
#' after all registered and auto-table definitions, and their results
#' appear in the codebook and have MOEs computed automatically.
#' \code{\link{define_sum}}, \code{\link{define_complement}}, or
#' \code{\link{define_metadata}}. These let you compute custom derived
#' variables from the columns produced by the tables you request. User
#' definitions are executed after all registered and auto-table
#' definitions, and their results appear in the codebook and have MOEs
#' computed automatically.
#' }
#' When mixing strings and definitions, wrap everything in \code{list()}
#' (e.g., \code{list("snap", define_percent(...))}).
Expand Down Expand Up @@ -92,10 +92,9 @@ safe_divide = function(x, y) { dplyr::if_else(y == 0, 0, x / y) }
#' df = compile_acs_data(
#' tables = list(
#' "snap",
#' define_percent("snap_not_received_percent",
#' numerator_variables = c("snap_universe", "snap_received"),
#' numerator_subtract_variables = c("snap_received"),
#' denominator_variables = c("snap_universe"))),
#' define_percent("snap_universe", "snap_universe",
#' subtract_from_numerator = "snap_received",
#' output = "snap_not_received_percent")),
#' years = 2022, geography = "county", states = "DC")
#' }
#' @export
Expand Down
3 changes: 2 additions & 1 deletion R/generate_codebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#' @param auto_table_entries A list of auto-generated table entries from
#' \code{build_auto_table_entry()}. Default is an empty list.
#' @param user_definitions A list of user-supplied DSL definition objects
#' (e.g., from \code{define_percent()}). Default is an empty list.
#' (e.g., from \code{define_percent()}, \code{define_sum()}). Default is an
#' empty list.
#' @returns A tibble containing the names and definitions of variables returned from
#' \code{urbnindicators::compile_acs_data()}.
#' @examples
Expand Down
Loading
Loading