Skip to content

Commit 7d99e6d

Browse files
committed
recs from Nat, local checks passing
1 parent c59af46 commit 7d99e6d

File tree

5 files changed

+72
-36
lines changed

5 files changed

+72
-36
lines changed

R/archive.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,10 @@ as_epi_archive <- function(
468468
x <- guess_column_name(x, "version", version_column_names())
469469
if (!test_subset(c("geo_value", "time_value", "version"), names(x))) {
470470
cli_abort(
471-
"Either columns `geo_value`, `time_value`, and `version` must be present in `x`,
472-
or related columns (see the internal functions `guess_time_column_name()`,
473-
`guess_geo_column_name()` and/or `guess_geo_version_name()` for complete list)."
471+
"Either columns `geo_value`, `time_value`, and `version`, or related columns
472+
(see the internal functions `guess_time_column_name()`,
473+
`guess_geo_column_name()` and/or `guess_geo_version_name()` for complete
474+
list) must be present in `x`."
474475
)
475476
}
476477
if (anyMissing(x$version)) {

R/epi_df.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ as_epi_df.tbl_df <- function(x, geo_type, time_type, as_of,
273273
x <- guess_column_name(x, "geo_value", geo_column_names())
274274
if (!test_subset(c("geo_value", "time_value"), names(x))) {
275275
cli_abort(
276-
"Either columns `geo_value` and `time_value` must be present in `x`, or related columns (see the internal
277-
functions `guess_time_column_name()` and/or `guess_geo_column_name()` for a complete list)."
276+
"Either columns `geo_value` and `time_value` or related columns
277+
(see the internal functions `guess_time_column_name()` and/or
278+
`guess_geo_column_name()` for a complete list)
279+
must be present in `x`."
278280
)
279281
}
280282

R/utils.R

+26-10
Original file line numberDiff line numberDiff line change
@@ -512,21 +512,37 @@ version_column_names <- function() {
512512
#' @param x the tibble to potentially rename
513513
#' @param substitutions a named vector. the potential substitions, with every name `time_value`
514514
#' @keywords internal
515+
#' @importFrom cli cli_inform cli_abort
516+
#' @importFrom dplyr rename
515517
guess_column_name <- function(x, column_name, substitutions) {
516518
if (!(column_name %in% names(x))) {
517-
x <- tryCatch(x %>% rename(any_of(substitutions)),
518-
error = function(cond) {
519-
cli_abort("{names(x)[names(x) %in% substitutions]} are both/all valid substitutions.
520-
Either `rename` some yourself or drop some.")
521-
}
522-
)
523519
# if none of the names are in substitutions, and `column_name` isn't a column, we're missing a relevant column
524520
if (!any(names(x) %in% substitutions)) {
525-
cli_abort("There is no {column_name} column or similar name. See e.g. [`time_column_name()`] for a complete list")
526-
}
527-
if (any(substitutions != "")) {
528-
cli_inform("inferring {column_name} column.")
521+
cli_abort(
522+
"There is no {column_name} column or similar name.
523+
See e.g. [`time_column_name()`] for a complete list",
524+
class = "epiprocess__guess_column__multiple_substitution_error"
525+
)
529526
}
527+
528+
tryCatch(
529+
{
530+
x <- x %>% rename(any_of(substitutions))
531+
cli_inform(
532+
"inferring {column_name} column.",
533+
class = "epiprocess__guess_column_inferring_inform"
534+
)
535+
return(x)
536+
},
537+
error = function(cond) {
538+
cli_abort(
539+
"{intersect(names(x), substitutions)}
540+
are both/all valid substitutions for {column_name}.
541+
Either `rename` some yourself or drop some.",
542+
class = "epiprocess__guess_column__multiple_substitution_error"
543+
)
544+
}
545+
)
530546
}
531547
return(x)
532548
}

tests/testthat/test-archive.R

+19-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test_that("data.frame must contain geo_value, time_value and version columns", {
1010
expect_error(as_epi_archive(select(dt, -geo_value), compactify = FALSE),
1111
regexp = "There is no geo_value column or similar name"
1212
)
13-
expect_error(expect_message(as_epi_archive(select(dt, -time_value), compactify = FALSE)),
13+
expect_error(as_epi_archive(select(dt, -time_value), compactify = FALSE),
1414
regexp = "There is no time_value column or similar name"
1515
)
1616
expect_error(as_epi_archive(select(dt, -version), compactify = FALSE),
@@ -20,18 +20,24 @@ test_that("data.frame must contain geo_value, time_value and version columns", {
2020

2121
test_that("as_epi_archive custom name mapping works correctly", {
2222
# custom name works correctly
23-
suppressWarnings(expect_equal(
24-
as_epi_archive(rename(dt, weirdName = version), version = weirdName),
25-
as_epi_archive(dt)
26-
))
27-
suppressWarnings(expect_equal(
28-
as_epi_archive(rename(dt, weirdName = geo_value), geo_value = weirdName),
29-
as_epi_archive(dt)
30-
))
31-
suppressWarnings(expect_equal(
32-
as_epi_archive(rename(dt, weirdName = time_value), time_value = weirdName),
33-
as_epi_archive(dt)
34-
))
23+
expect_equal(
24+
as_epi_archive(rename(dt, weirdName = version),
25+
version = weirdName, compactify = TRUE
26+
),
27+
as_epi_archive(dt, compactify = TRUE)
28+
)
29+
expect_equal(
30+
as_epi_archive(rename(dt, weirdName = geo_value),
31+
geo_value = weirdName, compactify = TRUE
32+
),
33+
as_epi_archive(dt, compactify = TRUE)
34+
)
35+
expect_equal(
36+
as_epi_archive(rename(dt, weirdName = time_value),
37+
time_value = weirdName, compactify = TRUE
38+
),
39+
as_epi_archive(dt, compactify = TRUE)
40+
)
3541

3642
expect_error(
3743
as_epi_archive(

tests/testthat/test-epi_df.R

+19-8
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,33 @@ test_that("as_epi_df works for nonstandard input", {
5252
date = rep(seq(as.Date("2020-01-01"), by = 1, length.out = 5), times = 2),
5353
geo_value = rep(c("ca", "hi"), each = 5)
5454
)
55-
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))
55+
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()),
56+
class = "epiprocess__guess_column_inferring_inform"
57+
)
5658
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 "
60-
))
59+
expect_error(
60+
expect_message(
61+
tib %>%
62+
rename(awefa = geo_value) %>%
63+
as_epi_df(),
64+
class = "epiprocess__guess_column_inferring_inform"
65+
),
66+
class = "epiprocess__guess_column__multiple_substitution_error"
67+
)
6168
expect_no_error(expect_message(
6269
tib %>% rename(awefa = geo_value) %>% as_epi_df(geo_value = awefa),
63-
regexp = "inferring"
70+
class = "epiprocess__guess_column_inferring_inform"
6471
))
6572

6673
tib <- tib %>% rename(target_date = date)
67-
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()))
74+
expect_message(expect_no_error(tib_epi_df <- tib %>% as_epi_df()),
75+
class = "epiprocess__guess_column_inferring_inform"
76+
)
6877

6978
tib <- tib %>% mutate(Time = 20 + target_date)
70-
expect_error(tib_epi_df <- tib %>% as_epi_df())
79+
expect_error(tib_epi_df <- tib %>% as_epi_df(),
80+
class = "epiprocess__guess_column__multiple_substitution_error"
81+
)
7182
})
7283

7384
# select fixes

0 commit comments

Comments
 (0)