Skip to content

Commit

Permalink
Merge pull request #1340 from tidymodels/step_interact-non-formula-error
Browse files Browse the repository at this point in the history
make step_interact give better erorr if terms ins't a formula
  • Loading branch information
EmilHvitfeldt authored Jun 8, 2024
2 parents 023dcd4 + 8cf5eb3 commit a0497a6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

* `recipe()` no longer crashes when given long formula expression (#1283).

* `step_interact()` now gives better error if `terms` isn't a formula. (#1299)

* `recipe()` will now show better error when columns are misspelled in formula (#1283).

* Fixed bug in `step_ns()` and `step_bs()` where `knots` field in `options` argument wasn't correctly used. (#1297)
Expand Down
12 changes: 10 additions & 2 deletions R/interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ prep.step_interact <- function(x, training, info = NULL, ...) {

# make backwards compatible with 1.0.6 (#1138)
if (!is_formula(x)) {
tmp_terms <- rlang::eval_tidy(x$terms[[1]])
tmp_terms <- tryCatch(
rlang::eval_tidy(x$terms[[1]]),
error = function(cnd) {
cli::cli_abort(
"{.arg terms} must be supplied as a formula.",
call = NULL
)
}
)
if (!is_formula(tmp_terms)) {
cli::cli_abort(
"{.arg term} must be a formula. Not {.obj_type_friendly {term}}."
"{.arg terms} must be a formula. Not {.obj_type_friendly {term}}."
)
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/interact.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
! Name collision occurred. The following variable names already exist:
* `x1ax2`

# gives informative error if terms isn't a formula (#1299)

Code
recipe(mpg ~ ., data = mtcars) %>% step_interact(terms = starts_with("dis")) %>%
prep()
Condition
Error in `step_interact()`:
Caused by error:
! `terms` must be supplied as a formula.

# bake method errors when needed non-standard role columns are missing

Code
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ test_that("works with long formulas (#1231)", {
expect_identical(res_long, res_short)
})

test_that("gives informative error if terms isn't a formula (#1299)", {
expect_snapshot(
error = TRUE,
recipe(mpg ~ ., data = mtcars) %>%
step_interact(terms = starts_with("dis")) %>%
prep()
)
})


# Infrastructure ---------------------------------------------------------------

test_that("bake method errors when needed non-standard role columns are missing", {
Expand Down

0 comments on commit a0497a6

Please sign in to comment.