diff --git a/R/BoxCox.R b/R/BoxCox.R index 858a0360b..243d5d287 100644 --- a/R/BoxCox.R +++ b/R/BoxCox.R @@ -160,7 +160,7 @@ bc_trans <- function(x, lambda, eps = .001) { if (any(x <= 0)) { cli::cli_warn( "Applying Box-Cox transformation to non-positive data in column \\ - {names(lambda)}" + {.field {names(lambda)}}." ) } diff --git a/R/case_weights.R b/R/case_weights.R index 1fbfe3584..9d94cbf65 100644 --- a/R/case_weights.R +++ b/R/case_weights.R @@ -88,10 +88,10 @@ get_case_weights <- function(info, .data, call = rlang::caller_env()) { if (!is.numeric(res)) { cli::cli_abort( c( - "x" = "{.arg {wt_col}} has a {.code case_weights} role,\\ - but is not numeric.", - "i" = "{.arg {wt_col}} is {.obj_type_friendly {wt_col}}." - ), + x = "{.field {wt_col}} has a {.code case_weights} role and should be + numeric, but is {.obj_type_friendly {wt_col}}.", + i = "Only numeric case weights are supported in recipes." + ), call = call ) } diff --git a/R/colcheck.R b/R/colcheck.R index 3c802f221..579513812 100644 --- a/R/colcheck.R +++ b/R/colcheck.R @@ -81,6 +81,9 @@ bake.check_cols <- function(object, new_data, ...) { new_cols <- names(new_data) missing <- setdiff(original_cols, new_cols) if (length(missing) > 0) { + # This is functionally not reachable after we added ptype checking in + # https://github.com/tidymodels/recipes/pull/1330 + # but it feels too harsh to deprecate this check function. cli::cli_abort(c( x = "{cli::qty(length(missing))}The following column{?s} {?is/are} \\ missing from {.arg new_data}:", diff --git a/R/count.R b/R/count.R index 3686ec208..36f10fe61 100644 --- a/R/count.R +++ b/R/count.R @@ -129,14 +129,6 @@ prep.step_count <- function(x, training, info = NULL, ...) { col_name <- recipes_eval_select(x$terms, training, info) check_type(training[, col_name], types = c("string", "factor", "ordered")) - if (length(col_name) > 1) { - cli::cli_abort(c( - x = "The selector should select at most a single variable.", - i = "The following {length(col_name)} were selected: \\ - {.and {.var {col_name}}}." - )) - } - step_count_new( terms = x$terms, role = x$role, diff --git a/R/cut.R b/R/cut.R index cfd1131a2..39924b5a5 100644 --- a/R/cut.R +++ b/R/cut.R @@ -113,6 +113,13 @@ prep.step_cut <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + if (!is.numeric(x$breaks)) { + cli::cli_abort( + "{.arg breaks} must be a numeric vector, \\ + not {.obj_type_friendly {breaks}}." + ) + } + all_breaks <- vector("list", length(col_names)) names(all_breaks) <- col_names for (col_name in col_names) { @@ -133,19 +140,6 @@ prep.step_cut <- function(x, training, info = NULL, ...) { } create_full_breaks <- function(var, breaks) { - if (!is.numeric(var)) { - cli::cli_abort( - "{.arg var} must be a numeric vector, not {.obj_type_friendly {var}}." - ) - } - - if (!is.numeric(breaks)) { - cli::cli_abort( - "{.arg breaks} must be a numeric vector, \\ - not {.obj_type_friendly {breaks}}." - ) - } - if (min(var) < min(breaks)) { breaks <- c(min(var), breaks) } @@ -204,12 +198,6 @@ cut_var <- function(var, breaks, include_outside_range) { # the levels when bake.recipe itself is called. Moreover, # it is cleaner to show it in this way. adjust_levels_min_max <- function(x) { - if (!is.factor(x)) { - cli::cli_abort( - "{.arg x} must be a factor, not {.obj_type_friendly {x}}.", - .internal = TRUE - ) - } levs <- levels(x) if (length(levs) == 1) { return(factor(rep("[min,max]", length(x)))) diff --git a/R/date.R b/R/date.R index 5dbdeabe2..5bdb45fd5 100644 --- a/R/date.R +++ b/R/date.R @@ -111,8 +111,7 @@ step_date <- offenders <- features[!features %in% feat] cli::cli_abort(c( - x = "Possible values of {.arg features} should include:", - "*" = "{.or {.val {feat}}}.", + x = "Possible values of {.arg features} are {.or {.val {feat}}}.", i = "Invalid values were: {.val {offenders}}." )) } diff --git a/R/dummy.R b/R/dummy.R index 1924f0f7f..8395d698b 100644 --- a/R/dummy.R +++ b/R/dummy.R @@ -271,13 +271,9 @@ bake.step_dummy <- function(object, new_data, ...) { # the original (see the note above) is_ordered <- attr(levels, "dataClasses") == "ordered" - if (is.null(levels_values)) { - cli::cli_abort("Factor level values not recorded in {.var col_name}.") - } - if (length(levels_values) == 1) { cli::cli_abort( - "Only one factor level in {.var col_name}: {levels_values}." + "Only one factor level in {.var {col_name}}: {.val {levels_values}}." ) } diff --git a/R/dummy_multi_choice.R b/R/dummy_multi_choice.R index aa434d848..1a7a9414f 100644 --- a/R/dummy_multi_choice.R +++ b/R/dummy_multi_choice.R @@ -142,8 +142,6 @@ prep.step_dummy_multi_choice <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("nominal", "logical")) - multi_dummy_check_type(training[, col_names]) - levels <- purrr::map(training[, col_names], levels) levels <- vctrs::list_unchop(levels, ptype = character(), name_spec = rlang::zap()) levels <- levels[!is.na(levels)] @@ -165,23 +163,6 @@ prep.step_dummy_multi_choice <- function(x, training, info = NULL, ...) { ) } -multi_dummy_check_type <- function(dat, call = rlang::caller_env()) { - is_good <- function(x) { - is.factor(x) | is.character(x) | all(is.na(x)) - } - - all_good <- vapply(dat, is_good, logical(1)) - if (!all(all_good)) { - offenders <- names(dat)[!all_good] - cli::cli_abort(c( - "x" = "All columns selected for the step should be \\ - factor, character, or NA. The following were not:", - "*" = "{.var {offenders}}." - ), call = call) - } - invisible(all_good) -} - #' @export bake.step_dummy_multi_choice <- function(object, new_data, ...) { col_names <- object$input diff --git a/R/impute_knn.R b/R/impute_knn.R index e06eb9cfb..b47f5324d 100644 --- a/R/impute_knn.R +++ b/R/impute_knn.R @@ -118,7 +118,7 @@ step_impute_knn <- if (length(options) > 0) { if (any(!(opt_nms %in% c("eps", "nthread")))) { cli::cli_abort( - "Valid values for {.arg options} are {.val eps}, and {.val nthread}." + "Valid values for {.arg options} are {.val eps} and {.val nthread}." ) } if (all(opt_nms != "nthread")) { diff --git a/R/impute_mean.R b/R/impute_mean.R index 06f7608cc..dd8c105e3 100644 --- a/R/impute_mean.R +++ b/R/impute_mean.R @@ -113,14 +113,8 @@ trim <- function(x, trim) { # Adapted from mean.default x <- sort(x, na.last = TRUE) na_ind <- is.na(x) - if (!is.numeric(trim) || length(trim) != 1L) { - cli::cli_abort("{.arg trim} must be numeric of length one.") - } n <- length(x[!na_ind]) if (trim > 0 && n) { - if (is.complex(x)) { - cli::cli_abort("Trimmed means are not defined for complex data.") - } if (trim >= 0.5) return(stats::median(x[!na_ind], na.rm = FALSE)) lo <- floor(n * trim) + 1 diff --git a/R/interact.R b/R/interact.R index f0d947044..0ed8a8bd8 100644 --- a/R/interact.R +++ b/R/interact.R @@ -162,7 +162,8 @@ prep.step_interact <- function(x, training, info = NULL, ...) { ) if (!is_formula(tmp_terms)) { cli::cli_abort( - "{.arg terms} must be a formula. Not {.obj_type_friendly {term}}." + "{.arg terms} must be a formula, not {.obj_type_friendly {term}}.", + .internal = TRUE ) } @@ -209,7 +210,7 @@ prep.step_interact <- function(x, training, info = NULL, ...) { vars <- unique(unlist(lapply(make_new_formula(int_terms), all.vars))) var_check <- info[info$variable %in% vars, ] - if (any(var_check$type == "nominal")) { + if (any(vapply(var_check$type, function(x) "nominal" %in% x, logical(1)))) { cli::cli_warn( "Categorical variables used in {.fn step_interact} should probably be \\ avoided; This can lead to differences in dummy variable values that \\ @@ -401,7 +402,10 @@ find_selectors <- function(f) { list() } else { # User supplied incorrect input - cli::cli_abort("Don't know how to handle type {typeof(f)}.") + cli::cli_abort( + "Don't know how to handle type {.code {typeof(f)}}.", + .internal = TRUE + ) } } @@ -418,7 +422,10 @@ replace_selectors <- function(x, elem, value) { map_pairlist(x, replace_selectors, elem, value) } else { # User supplied incorrect input - cli::cli_abort("Don't know how to handle type {typeof(f)}.") + cli::cli_abort( + "Don't know how to handle type {.code {typeof(f)}}.", + .internal = TRUE + ) } } diff --git a/R/kpca_rbf.R b/R/kpca_rbf.R index 677b71e0e..d96afec52 100644 --- a/R/kpca_rbf.R +++ b/R/kpca_rbf.R @@ -134,7 +134,7 @@ prep.step_kpca_rbf <- function(x, training, info = NULL, ...) { cli::cli_abort(c( x = "Failed with error:", i = as.character(kprc) - )) + ), .internal = TRUE) } } else { kprc <- NULL diff --git a/R/nnmf_sparse.R b/R/nnmf_sparse.R index c9c293836..5cc2ed745 100644 --- a/R/nnmf_sparse.R +++ b/R/nnmf_sparse.R @@ -170,9 +170,10 @@ prep.step_nnmf_sparse <- function(x, training, info = NULL, ...) { } else { na_w <- sum(is.na(nnm$w)) if (na_w > 0) { - cli::cli_abort( - "The NNMF loadings are missing. The penalty may have been to high." - ) + cli::cli_abort(c( + x = "The NNMF loadings are missing.", + i = "The penalty may have been too high or missing values are present in data." + )) } else { nnm <- list(x_vars = col_names, w = nnm$w) rownames(nnm$w) <- col_names diff --git a/R/pls.R b/R/pls.R index 41923154e..55cb656fc 100644 --- a/R/pls.R +++ b/R/pls.R @@ -344,7 +344,7 @@ prep.step_pls <- function(x, training, info = NULL, ...) { res <- try(rlang::eval_tidy(cl), silent = TRUE) if (inherits(res, "try-error")) { cli::cli_warn( - "{.fn step_pls failed with error: {as.character(res))}.", + "{.fn step_pls} failed with error: {as.character(res)}.", ) res <- list(x_vars = x_names, y_vars = y_names) } else { diff --git a/R/profile.R b/R/profile.R index f7b09e3a0..228a0c124 100644 --- a/R/profile.R +++ b/R/profile.R @@ -122,10 +122,10 @@ step_profile <- function(recipe, i = "See {.help [?step_profile](recipes::step_profile)} for information." )) } - if (all(sort(names(grid)) == c("len", "ptcl"))) { + if (!identical(sort(names(grid)), c("len", "pctl"))) { cli::cli_abort(c( x = "`grid` should have two named elements {.field len} and \\ - {.field ptcl}, not {sort(names(grid))}.", + {.field pctl}, not {.val {sort(names(grid))}}.", i = "See {.help [?step_profile](recipes::step_profile)} for information." )) } diff --git a/R/relevel.R b/R/relevel.R index 93b823147..3d0a0a6d1 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -84,19 +84,12 @@ step_relevel_new <- #' @export prep.step_relevel <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) - check_type(training[, col_names], types = c("string", "factor", "ordered")) + check_type(training[, col_names], types = c("string", "factor")) # Get existing levels and their factor type (i.e. ordered) objects <- lapply(training[, col_names], get_existing_values) # Check to make sure that no ordered levels are provided order_check <- map_lgl(objects, attr, "is_ordered") - if (any(order_check)) { - offenders <- names(order_check)[order_check] - cli::cli_abort( - "Columns contain ordered factors (which cannot be releveled) \\ - {.val {x$ref_level}}: {offenders}." - ) - } # Check to make sure that the reference level exists in the factor ref_check <- map_lgl(objects, function(x, y) !y %in% x, diff --git a/tests/testthat/_snaps/BoxCox.md b/tests/testthat/_snaps/BoxCox.md index 37c59254d..b7aa67253 100644 --- a/tests/testthat/_snaps/BoxCox.md +++ b/tests/testthat/_snaps/BoxCox.md @@ -33,6 +33,22 @@ -- Operations * Box-Cox transformation on: | Trained +--- + + Code + recipe(~ mpg + disp, data = mtcars) %>% step_BoxCox(mpg, disp) %>% prep() %>% + bake(new_data = tibble(mpg = -1, disp = -1)) + Condition + Warning: + Applying Box-Cox transformation to non-positive data in column mpg. + Warning: + Applying Box-Cox transformation to non-positive data in column disp. + Output + # A tibble: 1 x 2 + mpg disp + + 1 NaN NaN + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/case-weight-functions.md b/tests/testthat/_snaps/case-weight-functions.md index 91340d24c..70b0e25ad 100644 --- a/tests/testthat/_snaps/case-weight-functions.md +++ b/tests/testthat/_snaps/case-weight-functions.md @@ -15,3 +15,13 @@ ! There should only be a single column with the role `case_weights`. i In these data, there are 2 columns: `var1` and `var2`. +# get_case_weights() catches non-numeric case weights + + Code + recipe(~., data = mtcars) %>% step_normalize(all_predictors()) %>% prep() + Condition + Error in `step_normalize()`: + Caused by error in `prep()`: + x vs has a `case_weights` role and should be numeric, but is a string. + i Only numeric case weights are supported in recipes. + diff --git a/tests/testthat/_snaps/corr.md b/tests/testthat/_snaps/corr.md index 0a94908f6..25541ab76 100644 --- a/tests/testthat/_snaps/corr.md +++ b/tests/testthat/_snaps/corr.md @@ -52,6 +52,14 @@ -- Operations * Correlation filter on: V6, V1, V3 | Trained, ignored weights +# corr_filter() warns on many NA values + + Code + tmp <- recipe(~., data = mtcars) %>% step_corr(all_predictors()) %>% prep() + Condition + Warning: + Too many correlations are `NA`; skipping correlation filter. + # empty printing Code diff --git a/tests/testthat/_snaps/count.md b/tests/testthat/_snaps/count.md index a00d44f2c..1637a214d 100644 --- a/tests/testthat/_snaps/count.md +++ b/tests/testthat/_snaps/count.md @@ -27,6 +27,16 @@ ! Name collision occurred. The following variable names already exist: * `Sepal.Width` +# checks for grepl arguments + + Code + recipe(~., data = mtcars) %>% step_count(options = list(not_real_option = TRUE)) + Condition + Error in `step_count()`: + x The following elements of `options` are not allowed: + * "not_real_option". + i Valid options are: "ignore.case", "perl", "fixed", and "useBytes". + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/cut.md b/tests/testthat/_snaps/cut.md index c187c37bc..95254dd48 100644 --- a/tests/testthat/_snaps/cut.md +++ b/tests/testthat/_snaps/cut.md @@ -34,6 +34,29 @@ Warning: This will create a factor with one value only. +# breaks argument are type checked + + Code + recipe(~., data = mtcars) %>% step_cut(disp, hp, breaks = TRUE) %>% prep() + Condition + Error in `step_cut()`: + Caused by error: + ! Could not evaluate cli `{}` expression: `breaks`. + Caused by error: + ! object 'breaks' not found + +--- + + Code + recipe(~., data = mtcars) %>% step_cut(disp, hp, breaks = c("100", "200")) %>% + prep() + Condition + Error in `step_cut()`: + Caused by error: + ! Could not evaluate cli `{}` expression: `breaks`. + Caused by error: + ! object 'breaks' not found + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/date.md b/tests/testthat/_snaps/date.md index 693109ece..db44e6cd4 100644 --- a/tests/testthat/_snaps/date.md +++ b/tests/testthat/_snaps/date.md @@ -8,6 +8,36 @@ ! Name collision occurred. The following variable names already exist: * `Dan_year` +# errors on wrong values of features + + Code + recipe(~ Dan + Stefan, examples) %>% step_date(all_predictors(), features = "yearly") %>% + prep() + Condition + Error in `step_date()`: + x Possible values of `features` are "year", "doy", "mday", "week", "decimal", "semester", "quarter", "dow", or "month". + i Invalid values were: "yearly". + +--- + + Code + recipe(~ Dan + Stefan, examples) %>% step_date(all_predictors(), features = c( + "daily", "monthly", "yearly")) %>% prep() + Condition + Error in `step_date()`: + x Possible values of `features` are "year", "doy", "mday", "week", "decimal", "semester", "quarter", "dow", or "month". + i Invalid values were: "daily", "monthly", and "yearly". + +--- + + Code + recipe(~ Dan + Stefan, examples) %>% step_date(all_predictors(), features = c( + "daily", "month", "yearly")) %>% prep() + Condition + Error in `step_date()`: + x Possible values of `features` are "year", "doy", "mday", "week", "decimal", "semester", "quarter", "dow", or "month". + i Invalid values were: "daily" and "yearly". + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/discretize.md b/tests/testthat/_snaps/discretize.md index 2b694b0df..9a6ef4145 100644 --- a/tests/testthat/_snaps/discretize.md +++ b/tests/testthat/_snaps/discretize.md @@ -119,6 +119,14 @@ -- Operations * Discretize numeric variables from: x1 | Trained +# war when less breaks are generated + + Code + tmp <- discretize(c(rep(1, 50), 1:50), cuts = 5, min_unique = 1) + Condition + Warning: + Not enough data for 5 breaks. Only 4 breaks were used. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/dummy.md b/tests/testthat/_snaps/dummy.md index 595caca65..18c6da906 100644 --- a/tests/testthat/_snaps/dummy.md +++ b/tests/testthat/_snaps/dummy.md @@ -145,6 +145,15 @@ Caused by error: ! `x` contains too many levels (123456), which would result in a data.frame too large to fit in memory. +# throws an informative error for single level + + Code + recipe(~., data = data.frame(x = "only-level")) %>% step_dummy(x) %>% prep() + Condition + Error in `step_dummy()`: + Caused by error in `bake()`: + ! Only one factor level in `x`: "only-level". + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/holiday.md b/tests/testthat/_snaps/holiday.md index fad6d60a7..7d87ba249 100644 --- a/tests/testthat/_snaps/holiday.md +++ b/tests/testthat/_snaps/holiday.md @@ -8,6 +8,15 @@ ! Name collision occurred. The following variable names already exist: * `day_Easter` +# error on incorrect holidays argument + + Code + recipe(~., mtcars) %>% step_holiday(holidays = c("Invalid Holiday", + "NewYearsDay")) + Condition + Error in `step_holiday()`: + ! Invalid `holidays` value. See `timeDate::listHolidays()` for possible values. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/ica.md b/tests/testthat/_snaps/ica.md index 35366f492..49cc4d787 100644 --- a/tests/testthat/_snaps/ica.md +++ b/tests/testthat/_snaps/ica.md @@ -28,6 +28,16 @@ ! Name collision occurred. The following variable names already exist: * `IC1` +# rethrows error correctly from implementation + + Code + recipe(~., data = mtcars) %>% step_ica(all_predictors()) %>% prep() + Condition + Error in `step_ica()`: + Caused by error in `prep()`: + x Failed with error: + i Error in fastICA::fastICA(n.comp = 5, X = as.matrix(training[, col_names]), : mocked error + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/impute_bag.md b/tests/testthat/_snaps/impute_bag.md index 8ec9ed30f..b27599d9a 100644 --- a/tests/testthat/_snaps/impute_bag.md +++ b/tests/testthat/_snaps/impute_bag.md @@ -1,3 +1,24 @@ +# impute_with errors with nothing selected + + Code + recipe(~., data = mtcars) %>% step_impute_bag(all_predictors(), impute_with = NULL) %>% + prep() + Condition + Error in `step_impute_bag()`: + ! `impute_with` must not be empty. + +--- + + Code + tmp <- recipe(~., data = mtcars) %>% step_impute_bag(mpg, disp, vs) %>% prep() + Condition + Warning: + All predictors are missing; cannot impute. + Warning: + All predictors are missing; cannot impute. + Warning: + All predictors are missing; cannot impute. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/impute_knn.md b/tests/testthat/_snaps/impute_knn.md index 6759c29f5..b545960e6 100644 --- a/tests/testthat/_snaps/impute_knn.md +++ b/tests/testthat/_snaps/impute_knn.md @@ -6,6 +6,45 @@ Warning in `gower_work()`: skipping variable with zero or non-finite range. +# impute_with errors with nothing selected + + Code + recipe(~., data = mtcars) %>% step_impute_knn(all_predictors(), impute_with = NULL) %>% + prep() + Condition + Error in `step_impute_knn()`: + ! `impute_with` must not be empty. + +# warn if all values of predictor are missing + + Code + tmp <- recipe(~., data = mtcars) %>% step_impute_knn(mpg, disp, vs) %>% prep() + Condition + Warning: + All predictors are missing; cannot impute. + Warning: + All predictors are missing; cannot impute. + Warning: + All predictors are missing; cannot impute. + +# error on wrong options argument + + Code + recipe(~., data = mtcars) %>% step_impute_knn(all_predictors(), options = list( + wrong = "wrong")) %>% prep() + Condition + Error in `step_impute_knn()`: + ! Valid values for `options` are "eps" and "nthread". + +--- + + Code + recipe(~., data = mtcars) %>% step_impute_knn(all_predictors(), options = c( + wrong = "wrong")) %>% prep() + Condition + Error in `step_impute_knn()`: + ! `options` should be a named list. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/impute_linear.md b/tests/testthat/_snaps/impute_linear.md index 4152d1dde..fee317b2d 100644 --- a/tests/testthat/_snaps/impute_linear.md +++ b/tests/testthat/_snaps/impute_linear.md @@ -56,6 +56,33 @@ -- Operations * Linear regression imputation for: Lot_Frontage | Trained, ignored weights +# impute_with errors with nothing selected + + Code + recipe(~., data = mtcars) %>% step_impute_linear(all_predictors(), impute_with = NULL) %>% + prep() + Condition + Error in `step_impute_linear()`: + ! `impute_with` must not be empty. + +# warns if impute_with columns contains missing values + + Code + tmp <- recipe(~., data = mtcars) %>% step_impute_linear(mpg, impute_with = imp_vars( + disp)) %>% prep() + Condition + Warning: + There were missing values in the predictor(s) used to impute; imputation did not occur. + +# errors if there are no rows without missing values + + Code + recipe(~., data = mtcars) %>% step_impute_linear(all_predictors()) %>% prep() + Condition + Error in `step_impute_linear()`: + Caused by error in `prep()`: + ! The data did not have any rows where the imputation values were all complete. Is is thus unable to fit the linear regression model. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/interact.md b/tests/testthat/_snaps/interact.md index 45b1256e6..5f44474ad 100644 --- a/tests/testthat/_snaps/interact.md +++ b/tests/testthat/_snaps/interact.md @@ -18,6 +18,15 @@ Caused by error: ! `terms` must be supplied as a formula. +--- + + Code + tmp <- recipe(mpg ~ ., data = mtcars) %>% step_interact(~ disp:am) %>% prep( + strings_as_factors = FALSE) + Condition + Warning: + Categorical variables used in `step_interact()` should probably be avoided; This can lead to differences in dummy variable values that are produced by ?step_dummy (`?recipes::step_dummy()`). Please convert all involved variables to dummy variables first. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/kpca.md b/tests/testthat/_snaps/kpca.md index 33177778b..431c967fa 100644 --- a/tests/testthat/_snaps/kpca.md +++ b/tests/testthat/_snaps/kpca.md @@ -33,6 +33,16 @@ -- Operations * Kernel PCA extraction with: X2, X3, X4, X5, X6 | Trained +# rethrows error correctly from implementation + + Code + recipe(~., data = mtcars) %>% step_kpca(all_predictors()) %>% prep() + Condition + Error in `step_kpca()`: + Caused by error in `prep()`: + x Failed with error: + i Error in kernlab::kpca(x = as.matrix(training[, col_names]), features = 5, : mocked error + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/kpca_poly.md b/tests/testthat/_snaps/kpca_poly.md index 9148d0d94..b5bf211a3 100644 --- a/tests/testthat/_snaps/kpca_poly.md +++ b/tests/testthat/_snaps/kpca_poly.md @@ -27,6 +27,16 @@ ! Name collision occurred. The following variable names already exist: * `kPC1` +# rethrows error correctly from implementation + + Code + recipe(~., data = mtcars) %>% step_kpca_poly(all_predictors()) %>% prep() + Condition + Error in `step_kpca_poly()`: + Caused by error in `prep()`: + x Failed with error: + i Error in kernlab::kpca(x = as.matrix(training[, col_names]), features = 5, : mocked error + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/nnmf_sparse.md b/tests/testthat/_snaps/nnmf_sparse.md index 569ac3297..507aa247c 100644 --- a/tests/testthat/_snaps/nnmf_sparse.md +++ b/tests/testthat/_snaps/nnmf_sparse.md @@ -8,6 +8,26 @@ ! Name collision occurred. The following variable names already exist: * `NNMF1` +# rethrows error correctly from implementation + + Code + recipe(~., data = mtcars) %>% step_nnmf_sparse(all_predictors()) %>% prep() + Condition + Error in `step_nnmf_sparse()`: + Caused by error in `prep()`: + x Failed with error: + i Error in RcppML::nmf(A = dat, k = 2, L1 = c(0.001, 0.001), verbose = FALSE, : mocked error + +# errors for missing data + + Code + recipe(~., data = mtcars) %>% step_nnmf_sparse(all_predictors()) %>% prep() + Condition + Error in `step_nnmf_sparse()`: + Caused by error in `prep()`: + x The NNMF loadings are missing. + i The penalty may have been too high or missing values are present in data. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/pls.md b/tests/testthat/_snaps/pls.md index e758fc111..7657e6c31 100644 --- a/tests/testthat/_snaps/pls.md +++ b/tests/testthat/_snaps/pls.md @@ -35,6 +35,23 @@ ! The `preserve` argument of `step_pls()` was deprecated in recipes 0.1.16 and is now defunct. i Please use the `keep_original_cols` argument instead. +# rethrows error correctly from implementation + + Code + tmp <- recipe(~., data = mtcars) %>% step_pls(all_predictors(), outcome = "mpg") %>% + prep() + Condition + Warning: + `step_pls()` failed with error: Error in mixOmics::pls(X = x, Y = y, ncomp = 2, scale = TRUE) : mocked error . + +# error on no outcome + + Code + recipe(~., data = mtcars) %>% step_pls(all_predictors()) %>% prep() + Condition + Error in `step_pls()`: + ! `outcome` should select at least one column. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/profile.md b/tests/testthat/_snaps/profile.md index 6561f30e5..a03f11f65 100644 --- a/tests/testthat/_snaps/profile.md +++ b/tests/testthat/_snaps/profile.md @@ -54,6 +54,15 @@ Error in `fixed()`: ! No method for determining a value to fix for objects of class: . +# error on wrong grid names + + Code + recipe(~., data = mtcars) %>% step_profile(grid = list(pctl = TRUE, not_len = 100)) + Condition + Error in `step_profile()`: + x `grid` should have two named elements len and pctl, not "not_len" and "pctl". + i See ?step_profile (`?recipes::step_profile()`) for information. + # empty printing Code diff --git a/tests/testthat/_snaps/ratio.md b/tests/testthat/_snaps/ratio.md index 207da82c3..5b2d3843c 100644 --- a/tests/testthat/_snaps/ratio.md +++ b/tests/testthat/_snaps/ratio.md @@ -38,6 +38,15 @@ ! Name collision occurred. The following variable names already exist: * `mpg_o_disp` +--- + + Code + recipe(~., data = mtcars) %>% step_ratio(mpg, denom = NULL) + Condition + Error in `step_ratio()`: + ! `denom` must select at least one variable. + i See ?selections (`?recipes::selections()`) for more information. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/regex.md b/tests/testthat/_snaps/regex.md index 71074de5d..c015301b0 100644 --- a/tests/testthat/_snaps/regex.md +++ b/tests/testthat/_snaps/regex.md @@ -27,6 +27,25 @@ ! Name collision occurred. The following variable names already exist: * `Sepal.Width` +# error on multiple selections + + Code + recipe(~., data = mtcars) %>% step_regex(vs, am) + Condition + Error in `step_regex()`: + x For this step, only a single selector can be used. + i The following 2 selectors were used: `~vs` and `~am`. + +# checks for grepl arguments + + Code + recipe(~., data = mtcars) %>% step_regex(options = list(not_real_option = TRUE)) + Condition + Error in `step_regex()`: + x The following elements of `options` are not allowed: + * "not_real_option". + i Valid options are: "ignore.case", "perl", "fixed", and "useBytes". + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/relevel.md b/tests/testthat/_snaps/relevel.md index a081d0a06..8ed1a4246 100644 --- a/tests/testthat/_snaps/relevel.md +++ b/tests/testthat/_snaps/relevel.md @@ -5,7 +5,7 @@ Condition Error in `step_relevel()`: Caused by error in `prep()`: - x All columns selected for the step should be string, factor, or ordered. + x All columns selected for the step should be string or factor. * 1 integer variable found: `sqft` --- diff --git a/tests/testthat/_snaps/sample.md b/tests/testthat/_snaps/sample.md index 7d7822ffb..7f8c86b3c 100644 --- a/tests/testthat/_snaps/sample.md +++ b/tests/testthat/_snaps/sample.md @@ -60,6 +60,14 @@ -- Operations * Row sampling: | Trained, weighted +# warn when selectors are provided + + Code + tmp <- recipe(~., data = mtcars) %>% step_sample(all_predictors()) + Condition + Warning: + Selectors are not used for this step. + # empty printing Code diff --git a/tests/testthat/_snaps/selections.md b/tests/testthat/_snaps/selections.md index a0660963a..802f50272 100644 --- a/tests/testthat/_snaps/selections.md +++ b/tests/testthat/_snaps/selections.md @@ -16,3 +16,12 @@ Error in `recipes_eval_select()`: ! Argument `quos` is missing, with no default. +# error when selecting case weights + + Code + recipe(~., data = mtcars) %>% step_normalize(hp) %>% prep() + Condition + Error in `step_normalize()`: + Caused by error in `prep()`: + ! Cannot select case weights variable. + diff --git a/tests/testthat/_snaps/time.md b/tests/testthat/_snaps/time.md index 1166fe986..8f4cf54fb 100644 --- a/tests/testthat/_snaps/time.md +++ b/tests/testthat/_snaps/time.md @@ -8,6 +8,16 @@ ! Name collision occurred. The following variable names already exist: * `time_hour` +# errors on wrong values of features + + Code + recipe(~times, examples) %>% step_time(all_predictors(), features = "hourly") %>% + prep() + Condition + Error in `step_time()`: + x Possible values of `features` are: "am", "hour", "hour12", "minute", "second", or "decimal_day". + i Invalid values were: "hourly". + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/window.md b/tests/testthat/_snaps/window.md index 1eb0b6d37..cd55e8764 100644 --- a/tests/testthat/_snaps/window.md +++ b/tests/testthat/_snaps/window.md @@ -109,6 +109,15 @@ ! Name collision occurred. The following variable names already exist: * `new_value` +# error on too large window size + + Code + recipe(~., data = mtcars) %>% step_window(mpg, size = 999) %>% prep() + Condition + Error in `step_window()`: + Caused by error in `roller()`: + ! The window is too large. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/test-BoxCox.R b/tests/testthat/test-BoxCox.R index eb32cd3ad..972d06274 100644 --- a/tests/testthat/test-BoxCox.R +++ b/tests/testthat/test-BoxCox.R @@ -85,8 +85,16 @@ test_that("warnings", { step_BoxCox(x1) %>% prep() ) + + expect_snapshot( + recipe(~ mpg + disp, data = mtcars) %>% + step_BoxCox(mpg, disp) %>% + prep() %>% + bake(new_data = tibble(mpg = -1, disp = -1)) + ) }) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-case-weight-functions.R b/tests/testthat/test-case-weight-functions.R index 10d5f9eb6..c5eed8eab 100644 --- a/tests/testthat/test-case-weight-functions.R +++ b/tests/testthat/test-case-weight-functions.R @@ -160,3 +160,15 @@ test_that("is_unsupervised_weights works", { too_many_case_weights(c("var1", "var2")) ) }) + +test_that("get_case_weights() catches non-numeric case weights", { + mtcars$vs <- as.character(mtcars$vs) + class(mtcars$vs) <- c("hardhat_case_weights", "non_numeric_weights") + + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_normalize(all_predictors()) %>% + prep() + ) +}) diff --git a/tests/testthat/test-corr.R b/tests/testthat/test-corr.R index c45c1e6e4..ab891cb4b 100644 --- a/tests/testthat/test-corr.R +++ b/tests/testthat/test-corr.R @@ -132,6 +132,16 @@ test_that("case weights", { expect_snapshot(filtering_trained) }) +test_that("corr_filter() warns on many NA values", { + mtcars[, 1:10] <- NA_real_ + + expect_snapshot( + tmp <- recipe(~., data = mtcars) %>% + step_corr(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-count.R b/tests/testthat/test-count.R index a70675b8e..3433498bb 100644 --- a/tests/testthat/test-count.R +++ b/tests/testthat/test-count.R @@ -67,6 +67,14 @@ test_that("check_name() is used", { ) }) +test_that("checks for grepl arguments", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_count(options = list(not_real_option = TRUE)) + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-cut.R b/tests/testthat/test-cut.R index 22981787d..5d95c7a4b 100644 --- a/tests/testthat/test-cut.R +++ b/tests/testthat/test-cut.R @@ -167,6 +167,21 @@ test_that("tidy method works", { ) }) +test_that("breaks argument are type checked", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_cut(disp, hp, breaks = TRUE) %>% + prep() + ) + + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_cut(disp, hp, breaks = c("100", "200")) %>% + prep() + ) +}) # Infrastructure --------------------------------------------------------------- diff --git a/tests/testthat/test-date.R b/tests/testthat/test-date.R index 6a36d8ff2..791bdca74 100644 --- a/tests/testthat/test-date.R +++ b/tests/testthat/test-date.R @@ -186,6 +186,27 @@ test_that("can bake and recipes with no locale", { ) }) +test_that("errors on wrong values of features", { + expect_snapshot( + error = TRUE, + recipe(~ Dan + Stefan, examples) %>% + step_date(all_predictors(), features = "yearly") %>% + prep() + ) + expect_snapshot( + error = TRUE, + recipe(~ Dan + Stefan, examples) %>% + step_date(all_predictors(), features = c("daily", "monthly", "yearly")) %>% + prep() + ) + expect_snapshot( + error = TRUE, + recipe(~ Dan + Stefan, examples) %>% + step_date(all_predictors(), features = c("daily", "month", "yearly")) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-discretize.R b/tests/testthat/test-discretize.R index 0d5312de6..db5ea1ce5 100644 --- a/tests/testthat/test-discretize.R +++ b/tests/testthat/test-discretize.R @@ -171,6 +171,12 @@ test_that("tunable", { ) }) +test_that("war when less breaks are generated", { + expect_snapshot( + tmp <- discretize(c(rep(1, 50), 1:50), cuts = 5, min_unique = 1) + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-dummy.R b/tests/testthat/test-dummy.R index a531ceadc..caf62dfc1 100644 --- a/tests/testthat/test-dummy.R +++ b/tests/testthat/test-dummy.R @@ -345,6 +345,15 @@ test_that("throws a informative error for too many levels (#828)", { ) }) +test_that("throws an informative error for single level", { + expect_snapshot( + error = TRUE, + recipe(~ ., data = data.frame(x = "only-level")) %>% + step_dummy(x) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-holiday.R b/tests/testthat/test-holiday.R index aea68ea6f..1e469eaa0 100644 --- a/tests/testthat/test-holiday.R +++ b/tests/testthat/test-holiday.R @@ -182,6 +182,14 @@ test_that("check_name() is used", { ) }) +test_that("error on incorrect holidays argument", { + expect_snapshot( + error = TRUE, + recipe(~., mtcars) %>% + step_holiday(holidays = c("Invalid Holiday", "NewYearsDay")) + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-ica.R b/tests/testthat/test-ica.R index 437844e70..96c773d17 100644 --- a/tests/testthat/test-ica.R +++ b/tests/testthat/test-ica.R @@ -137,6 +137,25 @@ test_that("Do nothing for num_comps = 0 and keep_original_cols = FALSE (#1152)", expect_identical(res, tibble::as_tibble(mtcars)) }) +test_that("rethrows error correctly from implementation", { + skip_if_not_installed("dimRed") + skip_if_not_installed("fastICA") + skip_if_not_installed("RSpectra") + + local_mocked_bindings( + .package = "fastICA", + fastICA = function(...) { + cli::cli_abort("mocked error") + } + ) + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_ica(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-impute_bag.R b/tests/testthat/test-impute_bag.R index fa35adfee..3260f88be 100644 --- a/tests/testthat/test-impute_bag.R +++ b/tests/testthat/test-impute_bag.R @@ -112,6 +112,24 @@ test_that("non-factor imputation", { expect_true(is.character(bake(rec, NULL, Location)[[1]])) }) +test_that("impute_with errors with nothing selected", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_impute_bag(all_predictors(), impute_with = NULL) %>% + prep() + ) +}) + +test_that("impute_with errors with nothing selected", { + mtcars[, 1:11] <- NA_real_ + expect_snapshot( + tmp <- recipe(~., data = mtcars) %>% + step_impute_bag(mpg, disp, vs) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-impute_knn.R b/tests/testthat/test-impute_knn.R index d748d85c9..d65e27f4a 100644 --- a/tests/testthat/test-impute_knn.R +++ b/tests/testthat/test-impute_knn.R @@ -172,6 +172,39 @@ test_that("tunable", { ) }) +test_that("impute_with errors with nothing selected", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_impute_knn(all_predictors(), impute_with = NULL) %>% + prep() + ) +}) + +test_that("warn if all values of predictor are missing", { + mtcars[, 1:11] <- NA_real_ + expect_snapshot( + tmp <- recipe(~., data = mtcars) %>% + step_impute_knn(mpg, disp, vs) %>% + prep() + ) +}) + +test_that("error on wrong options argument", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_impute_knn(all_predictors(), options = list(wrong = "wrong")) %>% + prep() + ) + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_impute_knn(all_predictors(), options = c(wrong = "wrong")) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-impute_linear.R b/tests/testthat/test-impute_linear.R index 956308fbd..6ad354d11 100644 --- a/tests/testthat/test-impute_linear.R +++ b/tests/testthat/test-impute_linear.R @@ -134,6 +134,35 @@ test_that("case weights", { expect_snapshot(rec_prepped) }) +test_that("impute_with errors with nothing selected", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_impute_linear(all_predictors(), impute_with = NULL) %>% + prep() + ) +}) + +test_that("warns if impute_with columns contains missing values", { + mtcars$mpg[3] <- NA + mtcars$disp[3] <- NA + expect_snapshot( + tmp <- recipe(~., data = mtcars) %>% + step_impute_linear(mpg, impute_with = imp_vars(disp)) %>% + prep() + ) +}) + +test_that("errors if there are no rows without missing values", { + mtcars$am <- NA + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_impute_linear(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-interact.R b/tests/testthat/test-interact.R index 2a71ccabe..5ab8521f0 100644 --- a/tests/testthat/test-interact.R +++ b/tests/testthat/test-interact.R @@ -236,7 +236,10 @@ test_that("replacing selectors in formulas", { test_that('with factors', { int_rec <- recipe(Sepal.Width ~ ., data = iris) %>% step_interact(~ (. - Sepal.Width)^2, sep = ":") - int_rec_trained <- prep(int_rec, iris) + + suppressWarnings( + int_rec_trained <- prep(int_rec, iris) + ) te_new <- bake(int_rec_trained, new_data = iris, all_predictors(), - Species) te_new <- te_new[, sort(names(te_new))] @@ -316,6 +319,15 @@ test_that("gives informative error if terms isn't a formula (#1299)", { ) }) +test_that("gives informative error if terms isn't a formula (#1299)", { + mtcars$am <- as.character(mtcars$am) + + expect_snapshot( + tmp <- recipe(mpg ~ ., data = mtcars) %>% + step_interact(~disp:am) %>% + prep(strings_as_factors = FALSE) + ) +}) # Infrastructure --------------------------------------------------------------- @@ -421,3 +433,4 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + diff --git a/tests/testthat/test-kpca.R b/tests/testthat/test-kpca.R index 4668afb8e..d8718d982 100644 --- a/tests/testthat/test-kpca.R +++ b/tests/testthat/test-kpca.R @@ -89,6 +89,23 @@ test_that("Do nothing for num_comps = 0 and keep_original_cols = FALSE (#1152)", expect_identical(res, tibble::as_tibble(mtcars)) }) +test_that("rethrows error correctly from implementation", { + skip_if_not_installed("kernlab") + + local_mocked_bindings( + .package = "kernlab", + kpca = function(...) { + cli::cli_abort("mocked error") + } + ) + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_kpca(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-kpca_poly.R b/tests/testthat/test-kpca_poly.R index b71728ef2..b24a24ff1 100644 --- a/tests/testthat/test-kpca_poly.R +++ b/tests/testthat/test-kpca_poly.R @@ -96,6 +96,23 @@ test_that("Do nothing for num_comps = 0 and keep_original_cols = FALSE (#1152)", expect_identical(res, tibble::as_tibble(mtcars)) }) +test_that("rethrows error correctly from implementation", { + skip_if_not_installed("kernlab") + + local_mocked_bindings( + .package = "kernlab", + kpca = function(...) { + cli::cli_abort("mocked error") + } + ) + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_kpca_poly(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-nnmf_sparse.R b/tests/testthat/test-nnmf_sparse.R index e956d20c0..2f922eda2 100644 --- a/tests/testthat/test-nnmf_sparse.R +++ b/tests/testthat/test-nnmf_sparse.R @@ -27,6 +27,37 @@ test_that("Do nothing for num_comps = 0 and keep_original_cols = FALSE (#1152)", expect_identical(res, tibble::as_tibble(mtcars)) }) +test_that("rethrows error correctly from implementation", { + skip_if_not_installed("RcppML") + library(Matrix) + + local_mocked_bindings( + .package = "RcppML", + nmf = function(...) { + cli::cli_abort("mocked error") + } + ) + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_nnmf_sparse(all_predictors()) %>% + prep() + ) +}) + +test_that("errors for missing data", { + skip_if_not_installed("RcppML") + library(Matrix) + mtcars$mpg[1] <- NA + + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_nnmf_sparse(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-pls.R b/tests/testthat/test-pls.R index b16e29243..fcbaa46e1 100644 --- a/tests/testthat/test-pls.R +++ b/tests/testthat/test-pls.R @@ -271,6 +271,31 @@ test_that("Do nothing for num_comps = 0 and keep_original_cols = FALSE (#1152)", expect_identical(res, tibble::as_tibble(mtcars)) }) +test_that("rethrows error correctly from implementation", { + skip_if_not_installed("mixOmics") + local_mocked_bindings( + .package = "mixOmics", + pls = function(...) { + cli::cli_abort("mocked error") + } + ) + expect_snapshot( + tmp <- recipe(~ ., data = mtcars) %>% + step_pls(all_predictors(), outcome = "mpg") %>% + prep() + ) +}) + +test_that("error on no outcome", { + skip_if_not_installed("mixOmics") + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_pls(all_predictors()) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-profile.R b/tests/testthat/test-profile.R index e956df474..0f8aa522c 100644 --- a/tests/testthat/test-profile.R +++ b/tests/testthat/test-profile.R @@ -123,6 +123,14 @@ test_that("tidy", { expect_equal(tidy_4, exp_4) }) +test_that("error on wrong grid names", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_profile(grid = list(pctl = TRUE, not_len = 100)) + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-ratio.R b/tests/testthat/test-ratio.R index ab46bc80e..5f1191f81 100644 --- a/tests/testthat/test-ratio.R +++ b/tests/testthat/test-ratio.R @@ -145,6 +145,14 @@ test_that("check_name() is used", { ) }) +test_that("check_name() is used", { + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_ratio(mpg, denom = NULL) + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-regex.R b/tests/testthat/test-regex.R index e503f4932..5d553a6eb 100644 --- a/tests/testthat/test-regex.R +++ b/tests/testthat/test-regex.R @@ -59,6 +59,26 @@ test_that("check_name() is used", { ) }) +test_that("error on multiple selections", { + mtcars$vs <- as.character(mtcars$vs) + mtcars$am <- as.character(mtcars$am) + + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_regex(vs, am) + ) +}) + +test_that("checks for grepl arguments", { + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_regex(options = list(not_real_option = TRUE)) + ) +}) + + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-sample.R b/tests/testthat/test-sample.R index ee45124ab..8168543c1 100644 --- a/tests/testthat/test-sample.R +++ b/tests/testthat/test-sample.R @@ -136,6 +136,13 @@ test_that("sample with case weights", { expect_snapshot(rec) }) +test_that("warn when selectors are provided", { + expect_snapshot( + tmp <- recipe(~., data = mtcars) %>% + step_sample(all_predictors()) + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-selections.R b/tests/testthat/test-selections.R index feabfbb7f..7195524aa 100644 --- a/tests/testthat/test-selections.R +++ b/tests/testthat/test-selections.R @@ -324,3 +324,14 @@ test_that("old recipes from 1.0.1 work with new get_types", { bake(new_data = Sacramento) ) }) + +test_that("error when selecting case weights", { + mtcars$hp <- hardhat::importance_weights(mtcars$hp) + + expect_snapshot( + error = TRUE, + recipe(~., data = mtcars) %>% + step_normalize(hp) %>% + prep() + ) +}) diff --git a/tests/testthat/test-time.R b/tests/testthat/test-time.R index 93cd399f2..99c197bc1 100644 --- a/tests/testthat/test-time.R +++ b/tests/testthat/test-time.R @@ -106,6 +106,21 @@ test_that("check_name() is used", { prep(rec, training = dat) ) }) + +test_that("errors on wrong values of features", { + examples <- data.frame( + times = lubridate::ymd_hms("2022-05-06 10:01:07") + + lubridate::hours(1:5) + lubridate::minutes(1:5) + lubridate::seconds(1:5) + ) + + expect_snapshot( + error = TRUE, + recipe(~ times, examples) %>% + step_time(all_predictors(), features = "hourly") %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", { diff --git a/tests/testthat/test-window.R b/tests/testthat/test-window.R index 2bd253b5f..59b76929c 100644 --- a/tests/testthat/test-window.R +++ b/tests/testthat/test-window.R @@ -143,6 +143,17 @@ test_that("check_name() is used", { ) }) +test_that("error on too large window size", { + skip_if_not_installed("RcppRoll") + + expect_snapshot( + error = TRUE, + recipe(~ ., data = mtcars) %>% + step_window(mpg, size = 999) %>% + prep() + ) +}) + # Infrastructure --------------------------------------------------------------- test_that("bake method errors when needed non-standard role columns are missing", {