Skip to content

Commit a0497a6

Browse files
Merge pull request #1340 from tidymodels/step_interact-non-formula-error
make step_interact give better erorr if terms ins't a formula
2 parents 023dcd4 + 8cf5eb3 commit a0497a6

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
* `step_interact()` now gives better error if `terms` isn't a formula. (#1299)
20+
1921
* `recipe()` will now show better error when columns are misspelled in formula (#1283).
2022

2123
* Fixed bug in `step_ns()` and `step_bs()` where `knots` field in `options` argument wasn't correctly used. (#1297)

R/interact.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,18 @@ prep.step_interact <- function(x, training, info = NULL, ...) {
151151

152152
# make backwards compatible with 1.0.6 (#1138)
153153
if (!is_formula(x)) {
154-
tmp_terms <- rlang::eval_tidy(x$terms[[1]])
154+
tmp_terms <- tryCatch(
155+
rlang::eval_tidy(x$terms[[1]]),
156+
error = function(cnd) {
157+
cli::cli_abort(
158+
"{.arg terms} must be supplied as a formula.",
159+
call = NULL
160+
)
161+
}
162+
)
155163
if (!is_formula(tmp_terms)) {
156164
cli::cli_abort(
157-
"{.arg term} must be a formula. Not {.obj_type_friendly {term}}."
165+
"{.arg terms} must be a formula. Not {.obj_type_friendly {term}}."
158166
)
159167
}
160168

tests/testthat/_snaps/interact.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
! Name collision occurred. The following variable names already exist:
99
* `x1ax2`
1010

11+
# gives informative error if terms isn't a formula (#1299)
12+
13+
Code
14+
recipe(mpg ~ ., data = mtcars) %>% step_interact(terms = starts_with("dis")) %>%
15+
prep()
16+
Condition
17+
Error in `step_interact()`:
18+
Caused by error:
19+
! `terms` must be supplied as a formula.
20+
1121
# bake method errors when needed non-standard role columns are missing
1222

1323
Code

tests/testthat/test-interact.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ test_that("works with long formulas (#1231)", {
307307
expect_identical(res_long, res_short)
308308
})
309309

310+
test_that("gives informative error if terms isn't a formula (#1299)", {
311+
expect_snapshot(
312+
error = TRUE,
313+
recipe(mpg ~ ., data = mtcars) %>%
314+
step_interact(terms = starts_with("dis")) %>%
315+
prep()
316+
)
317+
})
318+
319+
310320
# Infrastructure ---------------------------------------------------------------
311321

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

0 commit comments

Comments
 (0)