From cd51ae9eb7004e58b515f60a626101ded29589a6 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 23 May 2024 17:17:24 -0700 Subject: [PATCH] update inline_check() --- R/recipe.R | 16 ++++++------ tests/testthat/_snaps/basics.md | 46 ++++++++++++++++++++++++--------- tests/testthat/test-basics.R | 12 +++++++++ 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/R/recipe.R b/R/recipe.R index 291ada502..19b1f7124 100644 --- a/R/recipe.R +++ b/R/recipe.R @@ -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 == "-")) { @@ -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) @@ -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) diff --git a/tests/testthat/_snaps/basics.md b/tests/testthat/_snaps/basics.md index 9f7f9b2e1..912c49ae6 100644 --- a/tests/testthat/_snaps/basics.md +++ b/tests/testthat/_snaps/basics.md @@ -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()`). @@ -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()`). @@ -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()`). @@ -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()`). diff --git a/tests/testthat/test-basics.R b/tests/testthat/test-basics.R index fc25699a1..38793b082 100644 --- a/tests/testthat/test-basics.R +++ b/tests/testthat/test-basics.R @@ -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 %>%