Skip to content

Commit

Permalink
More tests, ... tidyselect, doc as_epi_df, more values
Browse files Browse the repository at this point in the history
  • Loading branch information
dsweber2 committed Jun 25, 2024
1 parent fe8867b commit 08059b0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
3 changes: 2 additions & 1 deletion R/archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ as_epi_archive <- function(
additional_metadata = NULL,
compactify = NULL,
clobberable_versions_start = NULL,
versions_end = NULL) {
versions_end = NULL, ...) {
assert_data_frame(x)
x <- rename(x, ...)
x <- guess_time_column_name(x)
x <- guess_geo_column_name(x)
x <- guess_version_column_name(x)
Expand Down
19 changes: 12 additions & 7 deletions R/epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,16 @@ as_epi_df.epi_df <- function(x, ...) {

#' @method as_epi_df tbl_df
#' @describeIn as_epi_df The input tibble `x` must contain the columns
#' `geo_value` and `time_value`. All other columns will be preserved as is,
#' and treated as measured variables. If `as_of` is missing, then the function
#' will try to guess it from an `as_of`, `issue`, or `version` column of `x`
#' (if any of these are present), or from as an `as_of` field in its metadata
#' (stored in its attributes); if this fails, then the current day-time will
#' be used.
#' `geo_value` and `time_value`, or column names that uniquely map onto these
#' (e.g. `date` or `province`). Alternatively, you can specify the conversion
#' explicitly (`time_value = someWeirdColumnName`). All other columns not
#' specified as `other_keys` will be preserved as is, and treated as measured
#' variables.
#'
#' If `as_of` is missing, then the function will try to guess it from an
#' `as_of`, `issue`, or `version` column of `x` (if any of these are present),
#' or from as an `as_of` field in its metadata (stored in its attributes); if
#' this fails, then the current day-time will be used.
#' @importFrom rlang .data
#' @importFrom tidyselect any_of
#' @importFrom cli cli_inform
Expand All @@ -263,11 +267,12 @@ as_epi_df.tbl_df <- function(x, geo_type, time_type, as_of,
additional_metadata = list(),
...) {
# possible standard substitutions for time_value
x <- rename(x, ...)
x <- guess_time_column_name(x)
x <- guess_geo_column_name(x)
if (!test_subset(c("geo_value", "time_value"), names(x))) {
cli_abort(
"Columns `geo_value` and `time_value` must be present in `x`."
"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)."

Check warning on line 275 in R/epi_df.R

View workflow job for this annotation

GitHub Actions / lint

file=R/epi_df.R,line=275,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 204 characters.
)
}

Expand Down
20 changes: 14 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ upcase_snake_case <- function(x) {
c(x, X)
}

#' given an arbitrary
#' rename potential time_value columns
#' @keywords internal
guess_time_column_name <- function(x, substitutions = NULL) {
if (!("time_value" %in% names(x))) {
Expand All @@ -473,12 +473,14 @@ guess_time_column_name <- function(x, substitutions = NULL) {
time_value = "forecast_date",
time_value = "target_date",
time_value = "week",
time_value = "day",
time_value = "epiweek",
time_value = "month",
time_value = "mon",
time_value = "year",
time_value = "yearmon",
time_value = "yearmonth",
time_value = "yearMon",
time_value = "yearMonth",
time_value = "dates",
time_value = "time_values",
time_value = "forecast_dates",
Expand All @@ -495,7 +497,9 @@ guess_time_column_name <- function(x, substitutions = NULL) {
Either `rename` some yourself or drop some.")
}
)
cli_inform("inferring `time_value` column.")
if (any(substitutions != "")) {
cli_inform("inferring `time_value` column.")
}
}
return(x)
}
Expand Down Expand Up @@ -529,7 +533,9 @@ guess_geo_column_name <- function(x, substitutions = NULL) {
Either `rename` some yourself or drop some.")
}
)
cli_inform("inferring `time_value` column.")
if (any(substitutions != "")) {
cli_inform("inferring `geo_value` column.")
}
}
return(x)
}
Expand All @@ -545,11 +551,13 @@ guess_version_column_name <- function(x, substitutions = NULL) {
}
x <- tryCatch(x %>% rename(any_of(substitutions)),
error = function(cond) {
cli_abort("There are multiple `geo_value` candidate columns.
cli_abort("There are multiple `version` candidate columns.
Either `rename` some yourself or drop some.")
}
)
cli_inform("inferring `time_value` column.")
if (any(substitutions != "")) {
cli_inform("inferring `version` column.")
}
}
return(x)
}
Expand Down
16 changes: 10 additions & 6 deletions man/as_epi_df.Rd

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

4 changes: 2 additions & 2 deletions man/guess_time_column_name.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ test_that("as_epi_df works for nonstandard input", {
geo_value = rep(c("ca", "hi"), each = 5)
)
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))
expect_no_error(tib_epi_df <- tib %>% as_epi_df(time_value = date, geo_value = geo_value))
expect_error(expect_message(
tib %>% rename(awefa = geo_value) %>% as_epi_df(),

Check warning on line 58 in tests/testthat/test-epi_df.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_df.R,line=58,col=4,[indentation_linter] Hanging indent should be 30 spaces but is 4 spaces.
regexp = "inferring `time_value` column."))

Check warning on line 59 in tests/testthat/test-epi_df.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_df.R,line=59,col=2,[indentation_linter] Hanging indent should be 30 spaces but is 2 spaces.
expect_no_error(expect_message(
tib %>% rename(awefa = geo_value) %>% as_epi_df(geo_value = awefa),

Check warning on line 61 in tests/testthat/test-epi_df.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_df.R,line=61,col=4,[indentation_linter] Hanging indent should be 33 spaces but is 4 spaces.
regexp = "inferring `time_value` column."))

Check warning on line 62 in tests/testthat/test-epi_df.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-epi_df.R,line=62,col=2,[indentation_linter] Hanging indent should be 33 spaces but is 2 spaces.

tib <- tib %>% rename(forecast_date = date)
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))
Expand Down

0 comments on commit 08059b0

Please sign in to comment.