Skip to content

Commit

Permalink
Merge pull request #1328 from tidymodels/fix-1325
Browse files Browse the repository at this point in the history
Error earlier on non-tibble data
  • Loading branch information
EmilHvitfeldt authored Jun 1, 2024
2 parents 323d257 + 0552283 commit b28585c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/recipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ recipe.formula <- function(formula, data, ...) {
))
}

if (!is_tibble(data)) {
data <- as_tibble(data)
}

# Check for other in-line functions
args <- form2args(formula, data, ...)
obj <- recipe.data.frame(
Expand Down Expand Up @@ -234,10 +238,6 @@ form2args <- function(formula, data, ..., call = rlang::caller_env()) {
## check for in-line formulas
inline_check(formula, data, call)

if (!is_tibble(data)) {
data <- as_tibble(data)
}

## use rlang to get both sides of the formula
outcomes <- get_lhs_vars(formula, data)
predictors <- get_rhs_vars(formula, data, no_lhs = TRUE)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,19 @@
Caused by error in `prep()`:
! The following arguments were specified but do not exist: `AM` and `GEAR`.

# data argument is checked in recipe.formula() (#1325)

Code
recipe(~a, data = data)
Condition
Error in `as.data.frame.default()`:
! cannot coerce class '"function"' to a data.frame

---

Code
recipe(~., data = data)
Condition
Error in `as.data.frame.default()`:
! cannot coerce class '"function"' to a data.frame

11 changes: 11 additions & 0 deletions tests/testthat/test-basics.R
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,14 @@ test_that("steps give errors when arguments are misspelled", {
prep()
)
})

test_that("data argument is checked in recipe.formula() (#1325)", {
expect_snapshot(
error = TRUE,
recipe(~ a, data = data)
)
expect_snapshot(
error = TRUE,
recipe(~ ., data = data)
)
})

0 comments on commit b28585c

Please sign in to comment.