Skip to content

Commit e07ec94

Browse files
committed
More tests, ... tidyselect, doc as_epi_df, more values
1 parent fe8867b commit e07ec94

6 files changed

Lines changed: 49 additions & 22 deletions

File tree

R/archive.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,9 @@ as_epi_archive <- function(
453453
additional_metadata = NULL,
454454
compactify = NULL,
455455
clobberable_versions_start = NULL,
456-
versions_end = NULL) {
456+
versions_end = NULL, ...) {
457457
assert_data_frame(x)
458+
x <- rename(x, ...)
458459
x <- guess_time_column_name(x)
459460
x <- guess_geo_column_name(x)
460461
x <- guess_version_column_name(x)

R/epi_df.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,16 @@ as_epi_df.epi_df <- function(x, ...) {
249249

250250
#' @method as_epi_df tbl_df
251251
#' @describeIn as_epi_df The input tibble `x` must contain the columns
252-
#' `geo_value` and `time_value`. All other columns will be preserved as is,
253-
#' and treated as measured variables. If `as_of` is missing, then the function
254-
#' will try to guess it from an `as_of`, `issue`, or `version` column of `x`
255-
#' (if any of these are present), or from as an `as_of` field in its metadata
256-
#' (stored in its attributes); if this fails, then the current day-time will
257-
#' be used.
252+
#' `geo_value` and `time_value`, or column names that uniquely map onto these
253+
#' (e.g. `date` or `province`). Alternatively, you can specify the conversion
254+
#' explicitly (`time_value = someWeirdColumnName`). All other columns not
255+
#' specified as `other_keys` will be preserved as is, and treated as measured
256+
#' variables.
257+
#'
258+
#' If `as_of` is missing, then the function will try to guess it from an
259+
#' `as_of`, `issue`, or `version` column of `x` (if any of these are present),
260+
#' or from as an `as_of` field in its metadata (stored in its attributes); if
261+
#' this fails, then the current day-time will be used.
258262
#' @importFrom rlang .data
259263
#' @importFrom tidyselect any_of
260264
#' @importFrom cli cli_inform
@@ -263,11 +267,12 @@ as_epi_df.tbl_df <- function(x, geo_type, time_type, as_of,
263267
additional_metadata = list(),
264268
...) {
265269
# possible standard substitutions for time_value
270+
x <- rename(x, ...)
266271
x <- guess_time_column_name(x)
267272
x <- guess_geo_column_name(x)
268273
if (!test_subset(c("geo_value", "time_value"), names(x))) {
269274
cli_abort(
270-
"Columns `geo_value` and `time_value` must be present in `x`."
275+
"Either columns `geo_value` and `time_value` must be present in `x`, or related columns (see the internal functions `guess_time_column_name()` and/or `guess_geo_column_name()` for a complete list)."
271276
)
272277
}
273278

R/utils.R

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ upcase_snake_case <- function(x) {
459459
c(x, X)
460460
}
461461

462-
#' given an arbitrary
462+
#' rename potential time_value columns
463463
#' @keywords internal
464464
guess_time_column_name <- function(x, substitutions = NULL) {
465465
if (!("time_value" %in% names(x))) {
@@ -473,12 +473,14 @@ guess_time_column_name <- function(x, substitutions = NULL) {
473473
time_value = "forecast_date",
474474
time_value = "target_date",
475475
time_value = "week",
476-
time_value = "day",
477476
time_value = "epiweek",
478477
time_value = "month",
478+
time_value = "mon",
479479
time_value = "year",
480480
time_value = "yearmon",
481+
time_value = "yearmonth",
481482
time_value = "yearMon",
483+
time_value = "yearMonth",
482484
time_value = "dates",
483485
time_value = "time_values",
484486
time_value = "forecast_dates",
@@ -495,7 +497,9 @@ guess_time_column_name <- function(x, substitutions = NULL) {
495497
Either `rename` some yourself or drop some.")
496498
}
497499
)
498-
cli_inform("inferring `time_value` column.")
500+
if (any(substitutions != "")) {
501+
cli_inform("inferring `time_value` column.")
502+
}
499503
}
500504
return(x)
501505
}
@@ -529,7 +533,9 @@ guess_geo_column_name <- function(x, substitutions = NULL) {
529533
Either `rename` some yourself or drop some.")
530534
}
531535
)
532-
cli_inform("inferring `time_value` column.")
536+
if (any(substitutions != "")) {
537+
cli_inform("inferring `geo_value` column.")
538+
}
533539
}
534540
return(x)
535541
}
@@ -545,11 +551,13 @@ guess_version_column_name <- function(x, substitutions = NULL) {
545551
}
546552
x <- tryCatch(x %>% rename(any_of(substitutions)),
547553
error = function(cond) {
548-
cli_abort("There are multiple `geo_value` candidate columns.
554+
cli_abort("There are multiple `version` candidate columns.
549555
Either `rename` some yourself or drop some.")
550556
}
551557
)
552-
cli_inform("inferring `time_value` column.")
558+
if (any(substitutions != "")) {
559+
cli_inform("inferring `version` column.")
560+
}
553561
}
554562
return(x)
555563
}

man/as_epi_df.Rd

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/guess_time_column_name.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-epi_df.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ test_that("as_epi_df works for nonstandard input", {
5353
geo_value = rep(c("ca", "hi"), each = 5)
5454
)
5555
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))
56+
expect_no_error(tib_epi_df <- tib %>% as_epi_df(time_value = date, geo_value = geo_value))
57+
expect_error(expect_message(
58+
tib %>% rename(awefa = geo_value) %>% as_epi_df(),
59+
regexp = "inferring `time_value` column."
60+
))
61+
expect_no_error(expect_message(
62+
tib %>% rename(awefa = geo_value) %>% as_epi_df(geo_value = awefa),
63+
regexp = "inferring `time_value` column."
64+
))
5665

5766
tib <- tib %>% rename(forecast_date = date)
5867
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))

0 commit comments

Comments
 (0)