diff --git a/R/demo-crowding.R b/R/demo-crowding.R index 6cfc1b6..2a007e8 100644 --- a/R/demo-crowding.R +++ b/R/demo-crowding.R @@ -55,7 +55,7 @@ #' } demo_crowding <- function(area, areatype, crowding, race, include_metadata=FALSE) { req <- create_request("demographics") - + resp <- req %>% req_url_query( stateFIPS = fips_scp(area), @@ -71,7 +71,7 @@ demo_crowding <- function(area, areatype, crowding, race, include_metadata=FALSE req_perform() resp <- process_resp(resp, "demographics", include_metadata) - + if (include_metadata == TRUE) { resp$data <- resp$data %>% setNames(c( @@ -81,7 +81,6 @@ demo_crowding <- function(area, areatype, crowding, race, include_metadata=FALSE "Rank" )) %>% mutate(across(c("Percent", "Households"), \(x) as.numeric(x))) - return(resp) } else { resp %>% setNames(c( @@ -92,4 +91,5 @@ demo_crowding <- function(area, areatype, crowding, race, include_metadata=FALSE )) %>% mutate(across(c("Percent", "Households"), \(x) as.numeric(x))) } + resp <- process_metadata(resp) } diff --git a/R/process_metadata.R b/R/process_metadata.R new file mode 100644 index 0000000..1106ca2 --- /dev/null +++ b/R/process_metadata.R @@ -0,0 +1,20 @@ +process_metadata <- function(resp) { + resp_data <- resp$data + resp_metadata <- resp$metadata + + resp_df <- resp_data %>% as_tibble() + + class(resp_df) <- c("my_tbl", class(resp_df)) + + attr(resp_df, "metadata") <- resp_metadata + + print.my_tbl <- function(x) { + cat("Metadata:", "\n") + for (i in seq_along(attr(x, "metadata"))) { + cat(names(attr(x, "metadata"))[i], attr(x, "metadata")[[i]], "\n") + } + NextMethod(x) + invisible(x) + } + print.my_tbl(resp_df) +}