Skip to content

[Issue 182] Update epi_df examples, check additional_metadata type at construction #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions R/epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
if (!is.data.frame(x)) {
Abort("`x` must be a data frame.")
}


if (!is.list(additional_metadata)) {
Abort("`additional_metadata` must be a list type.")
}

# If geo type is missing, then try to guess it
if (missing(geo_type)) {
geo_type = guess_geo_type(x$geo_value)
Expand Down Expand Up @@ -230,7 +234,7 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
#'
#' ex2 <- ex2_input %>% dplyr::rename(geo_value = state, time_value = reported_date) %>%
#' as_epi_df(geo_type = "state", as_of = "2020-06-03",
#' additional_metadata = c(other_keys = "pol"))
#' additional_metadata = list(other_keys = "pol"))
#'
#' attr(ex2,"metadata")
#'
Expand All @@ -244,8 +248,10 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
#'
#' ex3 <- ex3_input %>%
#' tsibble::as_tsibble() %>% # needed to add the additional metadata
#' dplyr::mutate(state = rep("MA",6)) %>%
#' as_epi_df(additional_metadata = c(other_keys = "state"))
#' dplyr::mutate(
#' state = rep("MA",6),
#' pol = rep(c("blue", "swing", "swing"), each = 2)) %>% # 2 extra keys
#' as_epi_df(additional_metadata = list(other_keys = "state", "pol"))
#'
#' attr(ex3,"metadata")
as_epi_df = function(x, ...) {
Expand Down
12 changes: 4 additions & 8 deletions man/as_epi_archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions man/as_epi_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions man/epi_archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/epi_slide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/epix_as_of.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/epix_slide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ test_that("new_epi_df works as intended", {
expect_identical(attributes(epi_tib)$metadata$time_type, "day")
expect_true(lubridate::is.POSIXt(attributes(epi_tib)$metadata$as_of))
})

test_that("as_epi_df errors when additional_metadata is not a list", {
# This is the 3rd example from as_epi_df
ex_input <- jhu_csse_county_level_subset %>%
dplyr::filter(time_value > "2021-12-01", state_name == "Massachusetts") %>%
dplyr::slice_tail(n = 6) %>%
tsibble::as_tsibble() %>%
dplyr::mutate(
state = rep("MA",6),
pol = rep(c("blue", "swing", "swing"), each = 2))

expect_error(
as_epi_df(ex_input, additional_metadata = c(other_keys = "state", "pol")),
"`additional_metadata` must be a list type.")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-methods-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ toy_epi_df <- tibble::tibble(
), times = 2),
geo_value = rep(c("ca", "hi"), each = 5),
indicator_var = as.factor(rep(1:2, times = 5)),
) %>% as_epi_df(additional_metadata = c(other_keys = "indicator_var"))
) %>% as_epi_df(additional_metadata = list(other_keys = "indicator_var"))

att_toy = attr(toy_epi_df, "metadata")

Expand Down
4 changes: 2 additions & 2 deletions vignettes/epiprocess.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ head(ex2)

ex2 <- ex2 %>% rename(geo_value = state, time_value = reported_date) %>%
as_epi_df(geo_type = "state", as_of = "2020-06-03",
additional_metadata = c(other_keys = "pol"))
additional_metadata = list(other_keys = "pol"))

attr(ex2,"metadata")
```
Expand All @@ -206,7 +206,7 @@ Now we add state (MA) as a new column and a key to the metadata. Reminder that l
ex3 <- ex3 %>%
as_tibble() %>% # needed to add the additional metadata
mutate(state = rep(tolower("MA"),6)) %>%
as_epi_df(additional_metadata = c(other_keys = "state"))
as_epi_df(additional_metadata = list(other_keys = "state"))

attr(ex3,"metadata")
```
Expand Down