Skip to content

Commit

Permalink
basic auto-naming
Browse files Browse the repository at this point in the history
  • Loading branch information
dsweber2 committed Jun 6, 2024
1 parent 0ce39c5 commit 38f3608
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ importFrom(checkmate,test_subset)
importFrom(checkmate,vname)
importFrom(cli,cat_line)
importFrom(cli,cli_abort)
importFrom(cli,cli_inform)
importFrom(cli,cli_vec)
importFrom(cli,cli_warn)
importFrom(cli,format_message)
Expand Down
27 changes: 26 additions & 1 deletion R/epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,34 @@ as_epi_df.epi_df <- function(x, ...) {
#' (stored in its attributes); if this fails, then the current day-time will
#' be used.
#' @importFrom rlang .data
#' @importFrom cli cli_inform
#' @export
as_epi_df.tbl_df <- function(x, geo_type, time_type, as_of,
additional_metadata = list(), ...) {
additional_metadata = list(),
substitutions = NULL, ...) {
# possible standard substitutions
if (!("time_value" %in% names(x))) {
if (("forecast_date" %in% names(x)) && ("target_date" %in% names(x))) {
cli_abort("both `forecast_date` and `target_date` are present without a `time_value`
column, so it is ambiguous which to choose as `time_value`.")
}
name_substitutions <- substitutions %||% c(
time_value = "date",
time_value = "forecast_date",
time_value = "target_date",
time_value = "dates",
time_value = "time_values",
time_value = "forecast_dates",
time_value = "target_dates"
)
x <- tryCatch(x %>% rename(any_of(name_substitutions)),

Check warning on line 279 in R/epi_df.R

View workflow job for this annotation

GitHub Actions / lint

file=R/epi_df.R,line=279,col=32,[object_usage_linter] no visible global function definition for 'any_of'
error = function(cond) {
cli_abort("There are multiple `time_value` candidate columns.
Either `rename` on yourself or drop some.")
}
)
cli_inform("inferring `time_value` column.")
}
if (!test_subset(c("geo_value", "time_value"), names(x))) {
cli_abort(
"Columns `geo_value` and `time_value` must be present in `x`."
Expand Down
10 changes: 9 additions & 1 deletion man/as_epi_df.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ test_that("as_epi_df errors when additional_metadata is not a list", {
)
})

test_that("as_epi_df works for nonstandard input", {
tib <- tibble::tibble(
x = 1:10, y = 1:10,
date = rep(seq(as.Date("2020-01-01"), by = 1, length.out = 5), times = 2),
geo_value = rep(c("ca", "hi"), each = 5)
)
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))

tib <- tib %>% rename(forecast_date = date)
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))
tib %>% rename(any_of(name_substitutions))

tib <- tib %>% mutate(target_date = 20 + forecast_date)
expect_error(tib_epi_df <- tib %>% as_epi_df())
})

# select fixes

tib <- tibble::tibble(
Expand Down

0 comments on commit 38f3608

Please sign in to comment.