Skip to content

Commit ca02bc9

Browse files
committed
update examples, add tests
1 parent becfee9 commit ca02bc9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

R/epi_df.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
117117
if (!is.data.frame(x)) {
118118
Abort("`x` must be a data frame.")
119119
}
120-
120+
121+
if (!is.list(additional_metadata)) {
122+
Abort("`additional_metadata` must be a list type.")
123+
}
124+
121125
# If geo type is missing, then try to guess it
122126
if (missing(geo_type)) {
123127
geo_type = guess_geo_type(x$geo_value)
@@ -230,7 +234,7 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
230234
#'
231235
#' ex2 <- ex2_input %>% dplyr::rename(geo_value = state, time_value = reported_date) %>%
232236
#' as_epi_df(geo_type = "state", as_of = "2020-06-03",
233-
#' additional_metadata = c(other_keys = "pol"))
237+
#' additional_metadata = list(other_keys = "pol"))
234238
#'
235239
#' attr(ex2,"metadata")
236240
#'
@@ -244,8 +248,10 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
244248
#'
245249
#' ex3 <- ex3_input %>%
246250
#' tsibble::as_tsibble() %>% # needed to add the additional metadata
247-
#' dplyr::mutate(state = rep("MA",6)) %>%
248-
#' as_epi_df(additional_metadata = c(other_keys = "state"))
251+
#' dplyr::mutate(
252+
#' state = rep("MA",6),
253+
#' pol = rep(c("blue", "swing", "swing"), each = 2)) %>% # 2 extra keys
254+
#' as_epi_df(additional_metadata = list(other_keys = "state", "pol")) %>%
249255
#'
250256
#' attr(ex3,"metadata")
251257
as_epi_df = function(x, ...) {

tests/testthat/test-epi_df.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,18 @@ test_that("new_epi_df works as intended", {
2424
expect_identical(attributes(epi_tib)$metadata$time_type, "day")
2525
expect_true(lubridate::is.POSIXt(attributes(epi_tib)$metadata$as_of))
2626
})
27+
28+
test_that("as_epi_df errors when additional_metadata is not a list", {
29+
# This is the 3rd example from as_epi_df
30+
ex_input <- jhu_csse_county_level_subset %>%
31+
dplyr::filter(time_value > "2021-12-01", state_name == "Massachusetts") %>%
32+
dplyr::slice_tail(n = 6) %>%
33+
tsibble::as_tsibble() %>%
34+
dplyr::mutate(
35+
state = rep("MA",6),
36+
pol = rep(c("blue", "swing", "swing"), each = 2))
37+
38+
expect_error(
39+
as_epi_df(ex_input, additional_metadata = c(other_keys = "state", "pol")),
40+
"`additional_metadata` must be a list type.")
41+
})

0 commit comments

Comments
 (0)