Skip to content

Commit 6455828

Browse files
authored
Merge pull request #350 from cmu-delphi/ds/epi-recipe
refactor: epi_recipe only accepts epi_df
2 parents 14e3708 + 0f000a4 commit 6455828

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: epipredict
22
Title: Basic epidemiology forecasting methods
3-
Version: 0.0.15
3+
Version: 0.0.16
44
Authors@R: c(
55
person("Daniel", "McDonald", , "[email protected]", role = c("aut", "cre")),
66
person("Ryan", "Tibshirani", , "[email protected]", role = "aut"),

NEWS.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
4343
- `arx_fcast_epi_workflow()` and `arx_class_epi_workflow()` now default to
4444
`trainer = parsnip::logistic_reg()` to match their more canned versions.
4545
- add a `forecast()` method simplify generating forecasts
46-
- refactor `bake.epi_recipe()` and remove `epi_juice()`.
47-
- Revise `compat-purrr` to use the r-lang `standalone-*` version (via
46+
- refactor `bake.epi_recipe()` and remove `epi_juice()`.
47+
- Revise `compat-purrr` to use the r-lang `standalone-*` version (via
4848
`{usethis}`)
49+
- `epi_recipe()` will now warn when given non-`epi_df` data

R/epi_recipe.R

+11-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ epi_recipe.default <- function(x, ...) {
2020
if (is.matrix(x) || is.data.frame(x) || tibble::is_tibble(x)) {
2121
x <- x[1, , drop = FALSE]
2222
}
23+
cli_warn(
24+
"epi_recipe has been called with a non-epi_df object, returning a regular recipe. Various
25+
step_epi_* functions will not work."
26+
)
2327
recipes::recipe(x, ...)
2428
}
2529

@@ -147,6 +151,10 @@ epi_recipe.formula <- function(formula, data, ...) {
147151
data <- data[1, ]
148152
# check for minus:
149153
if (!epiprocess::is_epi_df(data)) {
154+
cli_warn(
155+
"epi_recipe has been called with a non-epi_df object, returning a regular recipe. Various
156+
step_epi_* functions will not work."
157+
)
150158
return(recipes::recipe(formula, data, ...))
151159
}
152160

@@ -333,15 +341,11 @@ update_epi_recipe <- function(x, recipe, ..., blueprint = default_epi_recipe_blu
333341
#' illustrations of the different types of updates.
334342
#'
335343
#' @param x A `epi_workflow` or `epi_recipe` object
336-
#'
337344
#' @param which_step the number or name of the step to adjust
338-
#'
339345
#' @param ... Used to input a parameter adjustment
340-
#'
341346
#' @param blueprint A hardhat blueprint used for fine tuning the preprocessing.
342347
#'
343-
#' @return
344-
#' `x`, updated with the adjustment to the specified `epi_recipe` step.
348+
#' @return `x`, updated with the adjustment to the specified `epi_recipe` step.
345349
#'
346350
#' @export
347351
#' @examples
@@ -383,17 +387,15 @@ adjust_epi_recipe <- function(x, which_step, ..., blueprint = default_epi_recipe
383387

384388
#' @rdname adjust_epi_recipe
385389
#' @export
386-
adjust_epi_recipe.epi_workflow <- function(
387-
x, which_step, ..., blueprint = default_epi_recipe_blueprint()) {
390+
adjust_epi_recipe.epi_workflow <- function(x, which_step, ..., blueprint = default_epi_recipe_blueprint()) {
388391
recipe <- adjust_epi_recipe(workflows::extract_preprocessor(x), which_step, ...)
389392

390393
update_epi_recipe(x, recipe, blueprint = blueprint)
391394
}
392395

393396
#' @rdname adjust_epi_recipe
394397
#' @export
395-
adjust_epi_recipe.epi_recipe <- function(
396-
x, which_step, ..., blueprint = default_epi_recipe_blueprint()) {
398+
adjust_epi_recipe.epi_recipe <- function(x, which_step, ..., blueprint = default_epi_recipe_blueprint()) {
397399
if (!(is.numeric(which_step) || is.character(which_step))) {
398400
cli::cli_abort(
399401
c("`which_step` must be a number or a character.",

tests/testthat/test-epi_recipe.R

+12-9
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ test_that("epi_recipe produces default recipe", {
44
x = 1:5, y = 1:5,
55
time_value = seq(as.Date("2020-01-01"), by = 1, length.out = 5)
66
)
7-
rec <- recipes::recipe(tib)
8-
rec$template <- rec$template[1, ]
9-
expect_identical(rec, epi_recipe(tib))
7+
expected_rec <- recipes::recipe(tib)
8+
expected_rec$template <- expected_rec$template[1, ]
9+
expect_warning(rec <- epi_recipe(tib), regexp = "epi_recipe has been called with a non-epi_df object")
10+
expect_identical(expected_rec, rec)
1011
expect_equal(nrow(rec$template), 1L)
1112

12-
rec <- recipes::recipe(y ~ x, tib)
13-
rec$template <- rec$template[1, ]
14-
expect_identical(rec, epi_recipe(y ~ x, tib))
13+
expected_rec <- recipes::recipe(y ~ x, tib)
14+
expected_rec$template <- expected_rec$template[1, ]
15+
expect_warning(rec <- epi_recipe(y ~ x, tib), regexp = "epi_recipe has been called with a non-epi_df object")
16+
expect_identical(expected_rec, rec)
1517
expect_equal(nrow(rec$template), 1L)
1618

1719
m <- as.matrix(tib)
18-
rec <- recipes::recipe(m)
19-
rec$template <- rec$template[1, ]
20-
expect_identical(rec, epi_recipe(m))
20+
expected_rec <- recipes::recipe(m)
21+
expected_rec$template <- expected_rec$template[1, ]
22+
expect_warning(rec <- epi_recipe(m), regexp = "epi_recipe has been called with a non-epi_df object")
23+
expect_identical(expected_rec, rec)
2124
expect_equal(nrow(rec$template), 1L)
2225
})
2326

0 commit comments

Comments
 (0)