Skip to content

Commit ef674ea

Browse files
authored
Merge pull request #167 from cmu-delphi/ds/tibble
refactor: use tibbles for metadata printing
2 parents 29c784a + b4df472 commit ef674ea

File tree

4 files changed

+69
-77
lines changed

4 files changed

+69
-77
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Imports:
3030
jsonlite,
3131
magrittr,
3232
MMWRweek,
33+
purrr,
3334
readr,
3435
tibble,
3536
xml2

NAMESPACE

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

3-
S3method(as.data.frame,covidcast_data_signal_list)
4-
S3method(as.data.frame,covidcast_data_source_list)
3+
S3method(as_tibble,covidcast_data_signal_list)
4+
S3method(as_tibble,covidcast_data_source_list)
55
S3method(print,covidcast_data_signal)
66
S3method(print,covidcast_data_source)
7+
S3method(print,covidcast_epidata)
78
S3method(print,epidata_call)
89
export("%>%")
910
export(avail_endpoints)
@@ -65,6 +66,8 @@ importFrom(httr,modify_url)
6566
importFrom(httr,stop_for_status)
6667
importFrom(jsonlite,fromJSON)
6768
importFrom(magrittr,"%>%")
69+
importFrom(purrr,map_chr)
70+
importFrom(purrr,map_lgl)
6871
importFrom(readr,read_csv)
6972
importFrom(tibble,as_tibble)
7073
importFrom(tibble,tibble)

R/covidcast.R

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -59,45 +59,59 @@ parse_source <- function(source, base_url) {
5959
r
6060
}
6161

62-
#' @method as.data.frame covidcast_data_signal_list
62+
#' @method as_tibble covidcast_data_signal_list
63+
#' @importFrom tibble as_tibble
64+
#' @importFrom purrr map_chr map_lgl
6365
#' @export
64-
as.data.frame.covidcast_data_signal_list <- function(x, ...) {
65-
as.data.frame(
66-
do.call(rbind, lapply(x, function(z) {
67-
sub <- z[c(
68-
"source",
69-
"signal",
70-
"name",
71-
"active",
72-
"short_description",
73-
"description",
74-
"time_type",
75-
"time_label",
76-
"value_label",
77-
"format",
78-
"category",
79-
"high_values_are",
80-
"is_smoothed",
81-
"is_weighted",
82-
"is_cumulative",
83-
"has_stderr",
84-
"has_sample_size"
85-
)]
86-
sub$geo_types <- paste0(names(z$geo_types), collapse = ",")
87-
sub
88-
})),
89-
row.names = sapply(x, function(y) y$key),
90-
...
91-
)
66+
as_tibble.covidcast_data_signal_list <- function(x, ...) {
67+
tib <- list()
68+
tib$source <- unname(map_chr(x, "source"))
69+
tib$signal <- unname(map_chr(x, "signal"))
70+
tib$name <- unname(map_chr(x, "name"))
71+
tib$active <- unname(map_lgl(x, "active"))
72+
tib$short_description <- unname(map_chr(x, "short_description"))
73+
tib$description <- unname(map_chr(x, "description"))
74+
tib$time_type <- unname(map_chr(x, "time_type"))
75+
tib$time_label <- unname(map_chr(x, "time_label"))
76+
tib$value_label <- unname(map_chr(x, "value_label"))
77+
tib$format <- unname(map_chr(x, "format"))
78+
tib$category <- unname(map_chr(x, "category"))
79+
tib$high_values_are <- unname(map_chr(x, "high_values_are"))
80+
if ("is_smoothed" %in% names(x)) {
81+
tib$is_smoothed <- unname(map_lgl(x, "is_smoothed"))
82+
} else {
83+
tib$is_smoothed <- NA
84+
}
85+
if ("is_weighted" %in% names(x)) {
86+
tib$is_weighted <- unname(map_lgl(x, "is_weighted"))
87+
} else {
88+
tib$is_weighted <- NA
89+
}
90+
if ("is_cumulative" %in% names(x)) {
91+
tib$is_cumulative <- unname(map_lgl(x, "is_cumulative"))
92+
} else {
93+
tib$is_cumulative <- NA
94+
}
95+
if ("has_stderr" %in% names(x)) {
96+
tib$has_stderr <- unname(map_lgl(x, "has_stderr"))
97+
} else {
98+
tib$has_stderr <- NA
99+
}
100+
if ("has_sample_size" %in% names(x)) {
101+
tib$has_sample_size <- unname(map_lgl(x, "has_sample_size"))
102+
} else {
103+
tib$has_sample_size <- NA
104+
}
105+
as_tibble(tib)
92106
}
93107

94108
#' @export
95109
print.covidcast_data_source <- function(x, ...) {
96110
print(x$name, ...)
97111
print(x$source, ...)
98112
print(x$description, ...)
99-
signals <- as.data.frame(x$signals)
100-
print(signals[, c("signal", "name", "short_description")], ...)
113+
signals <- as_tibble(x$signals)
114+
print(signals[, c("signal", "short_description")], ...)
101115
}
102116

103117
#' Creates the COVIDcast Epidata autocomplete helper
@@ -152,45 +166,26 @@ covidcast_epidata <- function(base_url = global_base_url, timeout_seconds = 30)
152166
)
153167
}
154168

155-
#' @method as.data.frame covidcast_data_source_list
169+
#' @method as_tibble covidcast_data_source_list
156170
#' @export
157-
as.data.frame.covidcast_data_source_list <- function(x, ...) {
158-
as.data.frame(
159-
do.call(
160-
rbind,
161-
lapply(
162-
x,
163-
FUN = function(z) {
164-
cols <- c(
165-
"source", "name", "description", "reference_signal",
166-
"license"
167-
)
168-
sub <- z[cols]
169-
sub$signals <- paste0(
170-
sapply(z$signals, function(y) y$signal),
171-
collapse = ","
172-
)
173-
sub
174-
}
175-
)
176-
),
177-
row.names = sapply(x, function(z) z$source),
178-
...
179-
)
171+
as_tibble.covidcast_data_source_list <- function(x, ...) {
172+
tib <- list()
173+
tib$source <- unname(map_chr(x, "source"))
174+
tib$name <- unname(map_chr(x, "name"))
175+
tib$description <- unname(map_chr(x, "description"))
176+
tib$reference_signal <- unname(map_chr(x, "reference_signal"))
177+
tib$license <- unname(map_chr(x, "license"))
178+
as_tibble(tib)
180179
}
181180

181+
#' @export
182182
print.covidcast_epidata <- function(x, ...) {
183183
print("COVIDcast Epidata Fetcher")
184184
print("Sources:")
185-
sources <- as.data.frame(x$sources)
186-
print(sources[1:5, c("source", "name")], ...)
187-
if (nrow(sources) > 5) {
188-
print(paste0((nrow(sources) - 5), " more..."))
189-
}
185+
sources <- as_tibble(x$sources)
186+
print(sources[, c("source", "name")], ...)
187+
190188
print("Signals")
191-
signals <- as.data.frame(x$signals)
192-
print(signals[1:5, c("source", "signal", "name")], ...)
193-
if (nrow(signals) > 5) {
194-
print(paste0((nrow(signals) - 5), " more..."))
195-
}
189+
signals <- as_tibble(x$signals)
190+
print(signals[, c("source", "signal", "name")], ...)
196191
}

tests/testthat/test-covidcast.R

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test_that("covidcast", {
2-
covidcast_api <- epidatr:::covidcast_epidata()
2+
covidcast_api <- epidatr::covidcast_epidata()
33
expect_identical(
44
covidcast_api$sources$`fb-survey`$signals$smoothed_cli$call(
55
"nation",
@@ -19,24 +19,17 @@ test_that("covidcast", {
1919
)
2020
})
2121

22-
# quite minimal, could probably use some checks that the fields are as desired
23-
test_that("dataframe converters", {
24-
res <- epidatr:::covidcast_epidata()$sources %>% as.data.frame()
25-
expect_identical(class(res), "data.frame")
26-
res <- epidatr:::covidcast_epidata()$signals %>% as.data.frame()
27-
expect_identical(class(res), "data.frame")
28-
})
2922

3023
test_that("http errors", {
3124
# see generate_test_data.R
3225
local_mocked_bindings(
3326
do_request = function(...) readRDS(testthat::test_path("data/test-do_request-httpbin.rds"))
3427
)
35-
expect_error(epidatr:::covidcast_epidata(), class = "http_400")
28+
expect_error(epidatr::covidcast_epidata(), class = "http_400")
3629
})
3730

3831

3932
test_that("name completion", {
40-
all_names <- names(epidatr:::covidcast_epidata()$signals)
33+
all_names <- names(epidatr::covidcast_epidata()$signals)
4134
expect_identical(all_names, all_names)
4235
})

0 commit comments

Comments
 (0)