Skip to content

Commit

Permalink
Support data retrieval if metadata is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
elipousson committed Sep 21, 2024
1 parent bed8114 commit 4139a88
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ importFrom(dplyr,join_by)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,rename)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
Expand Down Expand Up @@ -138,6 +139,7 @@ importFrom(tidycensus,moe_ratio)
importFrom(tidycensus,moe_sum)
importFrom(tidyr,pivot_wider)
importFrom(tidyselect,all_of)
importFrom(tidyselect,any_of)
importFrom(tidyselect,eval_select)
importFrom(tidyselect,everything)
importFrom(tidyselect,starts_with)
Expand Down
57 changes: 43 additions & 14 deletions R/get_acs_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,27 @@ get_acs_metadata <- function(survey = "acs5",

cli::cli_progress_step(msg)

data <- readr::read_csv(
file = file,
...,
progress = progress,
show_col_types = show_col_types
data <- rlang::try_fetch(
readr::read_csv(
file = file,
...,
progress = progress,
show_col_types = show_col_types
),
error = \(cnd) {
NULL
}
)

if (is.null(data)) {
cli::cli_warn(
"CensusReporter pre-computed {metadata} metadata for {acs_label} is not
available."
)
return(data.frame())
}


if (cache_data) {
cli::cli_progress_step("Caching {metadata} metadata")

Expand Down Expand Up @@ -147,7 +161,7 @@ label_acs_metadata <- function(data,
call = call
)

if (perc && all(has_name(data, geoid_col))) {
if (perc && all(has_name(data, c(geoid_col, "denominator_column_id")))) {
data <- join_acs_percent(data, geoid_col = geoid_col)
}

Expand Down Expand Up @@ -185,12 +199,14 @@ label_acs_table_metadata <- function(data,
.after = all_of(variable_col)
)

data <- dplyr::left_join(
data,
table_metadata,
by = dplyr::join_by({{ table_id_col }}),
na_matches = "never"
)
if (!is_empty(table_metadata)) {
data <- dplyr::left_join(
data,
table_metadata,
by = dplyr::join_by({{ table_id_col }}),
na_matches = "never"
)
}

if (is_character(data[[table_id_col]]) && !all(is.na(data[[table_id_col]]))) {
has_race_iteration <- any(
Expand Down Expand Up @@ -240,7 +256,8 @@ join_acs_race_iteration <- function(data,
#' @rdname label_acs_metadata
#' @name label_acs_column_metadata
#' @export
#' @importFrom dplyr mutate left_join all_of
#' @importFrom dplyr mutate left_join row_number
#' @importFrom tidyselect all_of any_of
#' @importFrom stringr str_extract str_remove
label_acs_column_metadata <- function(data,
survey = "acs5",
Expand All @@ -263,9 +280,21 @@ label_acs_column_metadata <- function(data,
data,
"{table_id_col}" := str_table_id(.data[[variable_col]]),
"{column_id_col}" := stringr::str_remove(.data[[variable_col]], "_"),
.after = all_of(variable_col)
.after = tidyselect::all_of(variable_col)
)

if (is_empty(column_metadata)) {
data <- dplyr::mutate(
data,
line_number = dplyr::row_number(),
.by = tidyselect::any_of(c("year", "GEOID", table_id_col))
)

# FIXME: Consider using metadata from tidycensus::load_variables if the
# CensusReport data is unavailable
return(data)
}

data <- dplyr::left_join(
data,
column_metadata,
Expand Down

0 comments on commit 4139a88

Please sign in to comment.