Skip to content

Commit b97e835

Browse files
authored
Merge pull request #102 from getwilds/metadata-sk
2 parents f06abb2 + ece6f11 commit b97e835

13 files changed

+197
-125
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,cancerprof_metadata)
34
S3method(print,cancerprof_tbl)
45
export("%>%")
56
export(demo_crowding)
@@ -39,5 +40,6 @@ importFrom(rlang,sym)
3940
importFrom(stats,setNames)
4041
importFrom(stringr,str_pad)
4142
importFrom(stringr,str_trim)
43+
importFrom(tibble,as_tibble)
4244
importFrom(utils,data)
4345
importFrom(utils,read.csv)

R/demo-crowding.R

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#' @importFrom httr2 req_url_query req_perform
2121
#' @importFrom stats setNames
2222
#' @importFrom dplyr mutate across
23+
#' @importFrom tibble as_tibble
2324
#'
2425
#' @returns A data frame with the following columns: Area, Area Code,
2526
#' Percent, Households, Rank.
@@ -34,8 +35,7 @@
3435
#' area = "WA",
3536
#' areatype = "county",
3637
#' crowding = "household with >1 person per room",
37-
#' race = "All Races (includes Hispanic)",
38-
#' include_metadata = TRUE
38+
#' race = "All Races (includes Hispanic)"
3939
#' )
4040
#'
4141
#' demo_crowding(
@@ -49,13 +49,12 @@
4949
#' area = "pr",
5050
#' areatype = "hsa",
5151
#' crowding = "household with >1 person per room",
52-
#' race = "black",
53-
#' include_metadata = TRUE
52+
#' race = "black"
5453
#' )
5554
#' }
56-
demo_crowding <- function(area, areatype, crowding, race, include_metadata=FALSE) {
55+
demo_crowding <- function(area, areatype, crowding, race) {
5756
req <- create_request("demographics")
58-
57+
5958
resp <- req %>%
6059
req_url_query(
6160
stateFIPS = fips_scp(area),
@@ -69,29 +68,17 @@ demo_crowding <- function(area, areatype, crowding, race, include_metadata=FALSE
6968
output = 1
7069
) %>%
7170
req_perform()
71+
72+
resp <- process_resp(resp, "demographics")
7273

73-
resp <- process_resp(resp, "demographics", include_metadata)
74-
75-
if (include_metadata == TRUE) {
76-
resp$data <- resp$data %>%
77-
setNames(c(
78-
get_area(areatype),
79-
"Percent",
80-
"Households",
81-
"Rank"
82-
)) %>%
83-
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
84-
85-
result_df <- process_metadata(resp)
86-
87-
} else {
88-
resp %>%
89-
setNames(c(
90-
get_area(areatype),
91-
"Percent",
92-
"Households",
93-
"Rank"
94-
)) %>%
95-
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
96-
}
74+
resp$data <- resp$data %>%
75+
setNames(c(
76+
get_area(areatype),
77+
"Percent",
78+
"Households",
79+
"Rank"
80+
)) %>%
81+
mutate(across(c("Percent", "Households"), \(x) as.numeric(x)))
82+
83+
process_metadata(resp)
9784
}

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/get-metadata.R

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
#helper function to remove key from values
3+
extract_values <- function(key, resp_metadata) {
4+
values <- resp_metadata[grep(key, resp_metadata)]
5+
values <- gsub(paste0("^\\s*", key, ":?\\s*"), "", values)
6+
return(values)
7+
}
8+
9+
#' Custom print function
10+
#'
11+
#' This custom print function processes the
12+
#' metadata output for a response object
13+
#'
14+
#' @param x
15+
#'
16+
#' @export
17+
print.cancerprof_metadata <- function(x, ...) {
18+
19+
cat("\033[38;5;246m# Data Report: \033[39m", "\n")
20+
cat(paste(x$data_report, '"\n', sep = "", collapse = " "), "\n")
21+
22+
cat("\033[38;5;246m# Sorted By: \033[39m", "\n")
23+
cat(x$sortedby, "\n")
24+
cat("\n")
25+
26+
cat("\033[38;5;246m# Created By: \033[39m", "\n")
27+
cat(x$createdby, "\n")
28+
cat("\n")
29+
30+
cat("\033[38;5;246m# Data Sources: \033[39m", "\n")
31+
cat(x$data_sources, "\n")
32+
cat("\n")
33+
34+
cat("\033[38;5;246m# Data Dictionary: \033[39m", "\n")
35+
cat(x$data_dictionary, "\n")
36+
cat("\n")
37+
38+
cat("\033[38;5;246m# Data Limitations: \033[39m", "\n")
39+
cat(x$data_limitations, "\n")
40+
41+
invisible(x)
42+
}
43+
44+
45+
get_metadata <- function(input_tbl) {
46+
resp_metadata <- attr(input_tbl, "metadata")
47+
48+
resp_metadata <- gsub("\\\"", "", resp_metadata)
49+
50+
data_report <- c(resp_metadata[1], resp_metadata[2], resp_metadata[3], resp_metadata[4])
51+
sortedby <- extract_values("Sorted by", resp_metadata)
52+
createdby <- extract_values("Created by", resp_metadata)
53+
data_sources <- extract_values("Source", resp_metadata)
54+
data_dictionary <- extract_values("For more information about", resp_metadata)
55+
data_limitations <- extract_values("Data for", resp_metadata)
56+
57+
58+
demo_metadata_list <- list(
59+
data_report = data_report,
60+
sortedby = sortedby,
61+
createdby = createdby,
62+
data_sources = data_sources,
63+
data_dictionary = data_dictionary,
64+
data_limitations = data_limitations
65+
)
66+
67+
class(demo_metadata_list) <- c("cancerprof_metadata", class(demo_metadata_list))
68+
69+
print.cancerprof_metadata(demo_metadata_list)
70+
}

R/parse_metadata.R

Lines changed: 0 additions & 35 deletions
This file was deleted.

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+
}

R/process-resp.R

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
#' @importFrom rlang sym
1515
#' @importFrom utils read.csv data
1616
#' @importFrom stringr str_trim
17+
#' @importFrom tibble as_tibble
1718
#'
1819
#' @returns A processed response data frame
1920
#'
2021
#' @noRd
2122
#'
2223
#' @examples
2324
#' process_resp(resp, "demographics")
24-
process_resp <- function(resp, topic, include_metadata) {
25+
process_resp <- function(resp, topic) {
2526
if (httr2::resp_content_type(resp) != "text/csv") {
2627
cli_abort("Invalid input, please check documentation for valid arguments.")
2728
}
@@ -83,15 +84,10 @@ process_resp <- function(resp, topic, include_metadata) {
8384
as_tibble()
8485

8586
#store metadata
86-
if (include_metadata == TRUE) {
87-
#return dataframe AND metadata
88-
resp_metadata <- c(
89-
resp_lines[1: (index_first_line_break - 1)], resp_lines[(index_second_line_break + 1): line_length]
90-
)
91-
resp_with_metadata <- list(metadata = resp_metadata, data = resp)
92-
93-
return(resp_with_metadata)
94-
} else {
95-
return(resp)
96-
}
87+
88+
#return dataframe AND metadata
89+
resp_metadata <- c(
90+
resp_lines[1: (index_first_line_break - 1)], resp_lines[(index_second_line_break + 1): line_length]
91+
)
92+
list(metadata = resp_metadata, data = resp)
9793
}

R/process_metadata.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#'
88
#' @export
99
print.cancerprof_tbl <- function(x, ...) {
10-
cat("Metadata:", "\n")
11-
for (i in seq_along(attr(x, "metadata"))) {
12-
cat(names(attr(x, "metadata"))[i], attr(x, "metadata")[[i]], "\n")
13-
}
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+
# }
1416
NextMethod(x, ...)
1517
}
1618

@@ -35,6 +37,6 @@ process_metadata <- function(resp) {
3537
class(resp_data) <- c("cancerprof_tbl", class(resp_data))
3638
attr(resp_data, "metadata") <- resp_metadata
3739

38-
print(resp_data)
40+
#print(resp_metadata)
3941
return(resp_data)
4042
}

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.

0 commit comments

Comments
 (0)