Skip to content

Commit

Permalink
update inline_check()
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed May 24, 2024
1 parent 4bb8d76 commit cd51ae9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
16 changes: 8 additions & 8 deletions R/recipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ recipe.data.frame <-
#' @rdname recipe
#' @export
recipe.formula <- function(formula, data, ...) {

if (rlang::is_missing(data)) {
cli::cli_abort("Argument {.var data} is missing, with no default.")
}

# check for minus:
f_funcs <- fun_calls(formula, data)
if (any(f_funcs == "-")) {
Expand Down Expand Up @@ -232,7 +232,7 @@ form2args <- function(formula, data, ..., call = rlang::caller_env()) {
}

## check for in-line formulas
inline_check(formula, data)
inline_check(formula, data, call)

if (!is_tibble(data)) {
data <- as_tibble(data)
Expand Down Expand Up @@ -273,20 +273,20 @@ form2args <- function(formula, data, ..., call = rlang::caller_env()) {
list(x = data, vars = vars, roles = roles)
}

inline_check <- function(x, data) {
inline_check <- function(x, data, call) {
funs <- fun_calls(x, data)
funs <- funs[!(funs %in% c("~", "+", "-", "."))]

if (length(funs) > 0) {
cli::cli_abort(c(
x = "No in-line functions should be used here.",
i = "{cli::qty(length(funs))}The following function{?s} {?was/were} \\
found: {.and {.code {funs}}}.",
x = "misspelled variable name or in-line functions detected.",
i = "{cli::qty(length(funs))}The following function/misspellings{?s} \\
{?was/were} found: {.and {.code {funs}}}.",
i = "Use steps to do transformations instead.",
i = "If your modeling engine uses special terms in formulas, pass \\
that formula to workflows as a \\
{.help [model formula](parsnip::model_formula)}."
))
), call = call)
}

invisible(x)
Expand Down
46 changes: 34 additions & 12 deletions tests/testthat/_snaps/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Code
recipe(HHV ~ log(nitrogen), data = biomass)
Condition
Error in `inline_check()`:
x No in-line functions should be used here.
i The following function was found: `log`.
Error in `recipe()`:
x misspelled variable name or in-line functions detected.
i The following function/misspellings was found: `log`.
i Use steps to do transformations instead.
i If your modeling engine uses special terms in formulas, pass that formula to workflows as a model formula (`?parsnip::model_formula()`).

Expand All @@ -14,9 +14,9 @@
Code
recipe(HHV ~ (.)^2, data = biomass)
Condition
Error in `inline_check()`:
x No in-line functions should be used here.
i The following functions were found: `^` and `(`.
Error in `recipe()`:
x misspelled variable name or in-line functions detected.
i The following function/misspellingss were found: `^` and `(`.
i Use steps to do transformations instead.
i If your modeling engine uses special terms in formulas, pass that formula to workflows as a model formula (`?parsnip::model_formula()`).

Expand All @@ -25,9 +25,9 @@
Code
recipe(HHV ~ nitrogen + sulfur + nitrogen:sulfur, data = biomass)
Condition
Error in `inline_check()`:
x No in-line functions should be used here.
i The following function was found: `:`.
Error in `recipe()`:
x misspelled variable name or in-line functions detected.
i The following function/misspellings was found: `:`.
i Use steps to do transformations instead.
i If your modeling engine uses special terms in formulas, pass that formula to workflows as a model formula (`?parsnip::model_formula()`).

Expand All @@ -36,9 +36,31 @@
Code
recipe(HHV ~ nitrogen^2, data = biomass)
Condition
Error in `inline_check()`:
x No in-line functions should be used here.
i The following function was found: `^`.
Error in `recipe()`:
x misspelled variable name or in-line functions detected.
i The following function/misspellings was found: `^`.
i Use steps to do transformations instead.
i If your modeling engine uses special terms in formulas, pass that formula to workflows as a model formula (`?parsnip::model_formula()`).

# Recipe on missspelled variables in formulas

Code
recipe(HHV ~ not_nitrogen, data = biomass)
Condition
Error in `recipe()`:
x misspelled variable name or in-line functions detected.
i The following function/misspellings was found: `not_nitrogen`.
i Use steps to do transformations instead.
i If your modeling engine uses special terms in formulas, pass that formula to workflows as a model formula (`?parsnip::model_formula()`).

---

Code
recipe(not_HHV ~ nitrogen, data = biomass)
Condition
Error in `recipe()`:
x misspelled variable name or in-line functions detected.
i The following function/misspellings was found: `not_HHV`.
i Use steps to do transformations instead.
i If your modeling engine uses special terms in formulas, pass that formula to workflows as a model formula (`?parsnip::model_formula()`).

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-basics.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ test_that("Recipe fails on in-line functions", {
)
})

test_that("Recipe on missspelled variables in formulas", {
expect_snapshot(
error = TRUE,
recipe(HHV ~ not_nitrogen, data = biomass)
)

expect_snapshot(
error = TRUE,
recipe(not_HHV ~ nitrogen, data = biomass)
)
})

test_that("return character or factor values", {
raw_recipe <- recipe(HHV ~ ., data = biomass)
centered <- raw_recipe %>%
Expand Down

0 comments on commit cd51ae9

Please sign in to comment.