Skip to content

Commit f8b9fe8

Browse files
committed
created get-metadata function
1 parent 3913315 commit f8b9fe8

9 files changed

+93
-49
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ importFrom(rlang,sym)
3939
importFrom(stats,setNames)
4040
importFrom(stringr,str_pad)
4141
importFrom(stringr,str_trim)
42+
importFrom(tibble,as_tibble)
4243
importFrom(utils,data)
4344
importFrom(utils,read.csv)

R/demo-crowding.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ demo_crowding <- function(area, areatype, crowding, race) {
7070
req_perform()
7171

7272
resp <- process_resp(resp, "demographics")
73-
73+
7474
resp$data <- resp$data %>%
7575
setNames(c(
7676
get_area(areatype),

R/demo-education.R

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
#' areatype = "state",
4545
#' education = "at least bachelors degree",
4646
#' sex = "both sexes",
47-
#' race = "all races (includes hispanic)",
48-
#' include_metadata = TRUE
47+
#' race = "all races (includes hispanic)"
4948
#' )
5049
#'
5150
#' demo_education(
@@ -54,7 +53,7 @@
5453
#' education = "less than 9th grade"
5554
#' )
5655
#' }
57-
demo_education <- function(area, areatype, education, sex = NULL, race = NULL, include_metadata=FALSE) {
56+
demo_education <- function(area, areatype, education, sex = NULL, race = NULL) {
5857
req <- create_request("demographics")
5958

6059
if (education == "less than 9th grade" && (!is.null(race) || !is.null(sex))) {
@@ -90,28 +89,17 @@ demo_education <- function(area, areatype, education, sex = NULL, race = NULL, i
9089
resp <- resp %>%
9190
req_perform()
9291

93-
resp <- process_resp(resp, "demographics", include_metadata)
92+
resp <- process_resp(resp, "demographics")
9493

95-
if (include_metadata == TRUE) {
96-
resp$data <- resp$data %>%
97-
setNames(c(
98-
get_area(areatype),
99-
"Percent",
100-
"Households",
101-
"Rank"
102-
)) %>%
103-
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
104-
105-
result_df <- process_metadata(resp)
106-
107-
} else {
108-
resp %>%
109-
setNames(c(
110-
get_area(areatype),
111-
"Percent",
112-
"Households",
113-
"Rank"
114-
)) %>%
115-
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
116-
}
94+
resp$data <- resp$data %>%
95+
setNames(c(
96+
get_area(areatype),
97+
"Percent",
98+
"Households",
99+
"Rank"
100+
)) %>%
101+
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
102+
103+
process_metadata(resp)
104+
117105
}

R/parse_metadata.R renamed to R/get-metadata.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11

2+
# result <- demo_crowding(
3+
# area = "WA",
4+
# areatype = "county",
5+
# crowding = "household with >1 person per room",
6+
# race = "All Races (includes Hispanic)"
7+
# )
8+
29
#helper function to remove key from values
310
extract_values <- function(key, resp_metadata) {
411
values <- resp_metadata[grep(key, resp_metadata)]
512
values <- gsub(paste0("^\\s*", key, ":?\\s*"), "", values)
613
return(values)
714
}
815

9-
parse_metadata <- function(resp) {
10-
resp_metadata <- resp$metadata
16+
17+
get_metadata <- function(input_tbl) {
18+
resp_metadata <- attr(input_tbl, "metadata")
1119

1220
resp_metadata <- gsub("\\\"", "", resp_metadata)
13-
21+
1422
report_header <- resp_metadata[1]
1523
data_report <- c(resp_metadata[2], resp_metadata[3], resp_metadata[4])
1624
sortedby <- extract_values("Sorted by", resp_metadata)
1725
createdby <- extract_values("Created by", resp_metadata)
1826
data_sources <- extract_values("Source", resp_metadata)
1927
data_dictionary <- extract_values("For more information about", resp_metadata)
2028
data_limitations <- extract_values("Data for", resp_metadata)
21-
22-
29+
30+
2331
demo_metadata_list <- list(
2432
report_header = report_header,
2533
data_report = data_report,

R/process-metadata.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' Custom print function
2+
#'
3+
#' This custom print function processes the
4+
#' metadata output for a response object
5+
#'
6+
#' @param x
7+
#'
8+
#' @export
9+
print.cancerprof_tbl <- function(x, ...) {
10+
#cat("Metadata:", "\n")
11+
# we actually need to figure out how to use pillar here
12+
cat("\033[38;5;246m# Access metadata with `get_metadata()`\033[39m", "\n")
13+
# for (i in seq_along(attr(x, "metadata"))) {
14+
# cat(names(attr(x, "metadata"))[i], attr(x, "metadata")[[i]], "\n")
15+
# }
16+
NextMethod(x, ...)
17+
}
18+
19+
#' Process Metadata
20+
#'
21+
#' This function sets the class of the response data
22+
#' to use the custom print function
23+
#'
24+
#' @param resp A response object
25+
#'
26+
#' @returns A response object with Metadata and a tibble
27+
#'
28+
#' @examples
29+
#' \dontrun{
30+
#' process_metadata(resp)
31+
#' }
32+
process_metadata <- function(resp) {
33+
34+
resp_data <- resp$data
35+
resp_metadata <- resp$metadata
36+
37+
class(resp_data) <- c("cancerprof_tbl", class(resp_data))
38+
attr(resp_data, "metadata") <- resp_metadata
39+
40+
#print(resp_data)
41+
return(resp_data)
42+
}

man/demo_crowding.Rd

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/demo_education.Rd

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/print.cancerprof_tbl.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/process_metadata.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)