diff --git a/R/YeoJohnson.R b/R/YeoJohnson.R index 63afa48fc..68c1293d2 100644 --- a/R/YeoJohnson.R +++ b/R/YeoJohnson.R @@ -109,6 +109,14 @@ step_YeoJohnson_new <- prep.step_YeoJohnson <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_whole(x$num_unique, args = "num_unique") + check_bool(x$na_rm, arg = "na_rm") + if (!is.numeric(x$limits) || any(is.na(x$limits)) || length(x$limits) != 2) { + cli::cli_abort("{.arg limits} should be a numeric vector with two values, + not {.obj_type_friendly {x$limits}}") + } + + x$limits <- sort(x$limits) values <- vapply( training[, col_names], diff --git a/R/lag.R b/R/lag.R index 0c710dc08..28a8a2d41 100644 --- a/R/lag.R +++ b/R/lag.R @@ -103,6 +103,7 @@ prep.step_lag <- function(x, training, info = NULL, ...) { not {.obj_type_friendly {lag}}." ) } + check_string(x$prefix, arg = "prefix") step_lag_new( terms = x$terms, diff --git a/R/lincomb.R b/R/lincomb.R index 08517505d..9e1aecbf8 100644 --- a/R/lincomb.R +++ b/R/lincomb.R @@ -101,6 +101,7 @@ step_lincomb_new <- prep.step_lincomb <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_whole(x$max_steps, arg = "max_steps", min = 1) filter <- iter_lc_rm( x = training[, col_names], diff --git a/R/log.R b/R/log.R index 122ab2cea..167c0e3b6 100644 --- a/R/log.R +++ b/R/log.R @@ -108,6 +108,9 @@ step_log_new <- prep.step_log <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_decimal(x$offset, arg = "offset") + check_bool(x$signed, arg = "signed") + check_number_decimal(x$base, arg = "base", min = 0) step_log_new( terms = x$terms, diff --git a/R/logit.R b/R/logit.R index ebbdf122c..c93473c5d 100644 --- a/R/logit.R +++ b/R/logit.R @@ -84,6 +84,7 @@ step_logit_new <- prep.step_logit <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_decimal(x$offset, arg = "offset") step_logit_new( terms = x$terms, diff --git a/R/newvalues.R b/R/newvalues.R index 8f43e1e25..a06404666 100644 --- a/R/newvalues.R +++ b/R/newvalues.R @@ -116,6 +116,7 @@ new_values_func <- function(x, #' @export prep.check_new_values <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) + check_bool(x$ignore_NA, arg = "ignore_NA") values <- lapply(training[, col_names], unique) diff --git a/R/nnmf_sparse.R b/R/nnmf_sparse.R index 5cc2ed745..f13d8e1c8 100644 --- a/R/nnmf_sparse.R +++ b/R/nnmf_sparse.R @@ -154,6 +154,9 @@ nnmf_pen_call <- function(x) { prep.step_nnmf_sparse <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_whole(x$num_comp, arg = "num_comp", min = 0) + check_number_decimal(x$penalty, arg = "penalty", min = .Machine$double.eps) + check_string(x$prefix, arg = "prefix") if (x$num_comp > 0 && length(col_names) > 0) { x$num_comp <- min(x$num_comp, length(col_names)) diff --git a/R/normalize.R b/R/normalize.R index 6502bfb87..5210c6d35 100644 --- a/R/normalize.R +++ b/R/normalize.R @@ -139,6 +139,7 @@ sd_check <- function(x) { prep.step_normalize <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$na_rm, arg = "na_rm") wts <- get_case_weights(info, training) were_weights_used <- are_weights_used(wts, unsupervised = TRUE) diff --git a/R/novel.R b/R/novel.R index 310372aaf..0e572279a 100644 --- a/R/novel.R +++ b/R/novel.R @@ -121,6 +121,7 @@ get_existing_values <- function(x) { prep.step_novel <- 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_string(x$new_level, arg = "new_level") # Get existing levels and their factor type (i.e. ordered) objects <- lapply(training[, col_names], get_existing_values) diff --git a/R/num2factor.R b/R/num2factor.R index 87b47b63f..3e6ea6161 100644 --- a/R/num2factor.R +++ b/R/num2factor.R @@ -146,6 +146,7 @@ get_ord_lvls_num <- function(x, foo) { prep.step_num2factor <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_function(x$transform, arg = "transform") res <- lapply(training[, col_names], get_ord_lvls_num, foo = x$transform) res <- c(res, ..levels = list(x$levels)) diff --git a/R/nzv.R b/R/nzv.R index c2fcbdf55..e1c43d190 100644 --- a/R/nzv.R +++ b/R/nzv.R @@ -93,7 +93,7 @@ step_nzv <- skip = FALSE, id = rand_id("nzv")) { exp_list <- list(freq_cut = 95 / 5, unique_cut = 10) - + if (!isTRUE(all.equal(exp_list, options))) { lifecycle::deprecate_stop( "0.1.7", @@ -140,6 +140,8 @@ step_nzv_new <- #' @export prep.step_nzv <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) + check_number_decimal(x$unique_cut, arg = "unique_cut", min = 0, max = 100) + check_number_decimal(x$freq_cut, arg = "freq_cut", min = 0) wts <- get_case_weights(info, training) were_weights_used <- are_weights_used(wts, unsupervised = TRUE) diff --git a/R/ordinalscore.R b/R/ordinalscore.R index 6c2ba5808..d8f20cde6 100644 --- a/R/ordinalscore.R +++ b/R/ordinalscore.R @@ -109,6 +109,7 @@ step_ordinalscore_new <- prep.step_ordinalscore <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = "ordered") + check_function(x$convert, arg = "convert") step_ordinalscore_new( terms = x$terms, diff --git a/R/other.R b/R/other.R index 8380643f7..90fe0f152 100644 --- a/R/other.R +++ b/R/other.R @@ -107,13 +107,7 @@ step_other <- objects = NULL, skip = FALSE, id = rand_id("other")) { - if (!is_tune(threshold)) { - if (threshold >= 1) { - check_number_whole(threshold) - } else { - check_number_decimal(threshold, min = 0) - } - } + add_step( recipe, step_other_new( @@ -152,6 +146,18 @@ prep.step_other <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("string", "factor", "ordered")) + if (!is.numeric(x$threshold)) { + cli::cli_abort("{.arg threshold} should be a single numeric value + {.obj_type_friendly {x$threshold}}") + } + + if (x$threshold >= 1) { + check_number_whole(x$threshold, arg = "threshold", min = 1) + } else { + check_number_decimal(x$threshold, arg = "threshold", min = 0) + } + + wts <- get_case_weights(info, training) were_weights_used <- are_weights_used(wts, unsupervised = TRUE) if (isFALSE(were_weights_used)) { diff --git a/R/pca.R b/R/pca.R index 52d622a65..f9d8237c7 100644 --- a/R/pca.R +++ b/R/pca.R @@ -127,9 +127,6 @@ step_pca <- function(recipe, keep_original_cols = FALSE, skip = FALSE, id = rand_id("pca")) { - if (!is_tune(threshold)) { - check_number_decimal(threshold, min = 0, max = 1, allow_na = TRUE) - } add_step( recipe, @@ -176,6 +173,10 @@ step_pca_new <- prep.step_pca <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_decimal(x$threshold, arg = "threshold", min = 0, max = 1, + allow_na = TRUE) + check_string(x$prefix, arg = "prefix") + check_number_whole(x$num_comp, arg = "num_comp", min = 0) wts <- get_case_weights(info, training) were_weights_used <- are_weights_used(wts, unsupervised = TRUE) @@ -352,8 +353,8 @@ tidy.step_pca <- function(x, type = "coef", ...) { ) } else { type <- rlang::arg_match( - type, - c("coef", "variance"), + type, + c("coef", "variance"), error_call = rlang::caller_env() ) if (type == "coef") { diff --git a/R/pls.R b/R/pls.R index 55cb656fc..72a873031 100644 --- a/R/pls.R +++ b/R/pls.R @@ -326,6 +326,9 @@ prep.step_pls <- function(x, training, info = NULL, ...) { y_names <- recipes_eval_select(x$outcome, training, info) check_type(training[, x_names], types = c("double", "integer")) + check_number_decimal(x$predictor_prop, arg = "predictor_prop", min = 0, max = 1) + check_string(x$prefix, arg = "prefix") + check_number_whole(x$num_comp, arg = "num_comp", min = 0) if (length(y_names) > 1 && any(!map_lgl(training[y_names], is.numeric))) { cli::cli_abort( diff --git a/R/poly.R b/R/poly.R index 1e83d2c7a..e0c9aff0a 100644 --- a/R/poly.R +++ b/R/poly.R @@ -73,14 +73,11 @@ step_poly <- role = "predictor", trained = FALSE, objects = NULL, - degree = 2, + degree = 2L, options = list(), keep_original_cols = FALSE, skip = FALSE, id = rand_id("poly")) { - if (!is_tune(degree)) { - degree <- as.integer(degree) - } if (any(names(options) == "degree")) { degree <- options$degree @@ -142,6 +139,8 @@ poly_wrapper <- function(x, args) { prep.step_poly <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_whole(x$degree, arg = "degree", min = 1) + x$degree <- as.integer(x$degree) opts <- x$options opts$degree <- x$degree diff --git a/R/poly_bernstein.R b/R/poly_bernstein.R index 5e3e02143..5eaee058c 100644 --- a/R/poly_bernstein.R +++ b/R/poly_bernstein.R @@ -116,6 +116,8 @@ step_poly_bernstein_new <- prep.step_poly_bernstein <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$complete_set, arg = "complete_set") + check_number_whole(x$degree, arg = "degree", min = 0) x$options <- c(x$options, degree = x$degree) diff --git a/R/range.R b/R/range.R index 08d9d282d..0de4d781a 100644 --- a/R/range.R +++ b/R/range.R @@ -4,10 +4,8 @@ #' numeric data to be within a pre-defined range of values. #' #' @inheritParams step_center -#' @param min A single numeric value for the smallest value in the -#' range. -#' @param max A single numeric value for the largest value in the -#' range. +#' @param min,max Single numeric values for the smallest (or largest) value in +#' the transformed data. #' @param clipping A single logical value for determining whether #' application of transformation onto new data should be forced #' to be inside `min` and `max`. Defaults to TRUE. @@ -106,6 +104,9 @@ step_range_new <- prep.step_range <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_number_decimal(x$min, arg = "min") + check_number_decimal(x$max, arg = "max") + check_bool(x$clipping, arg = "clipping") mins <- vapply(training[, col_names], min, c(min = 0), na.rm = TRUE) diff --git a/R/ratio.R b/R/ratio.R index e023587ad..963c6527a 100644 --- a/R/ratio.R +++ b/R/ratio.R @@ -134,6 +134,7 @@ prep.step_ratio <- function(x, training, info = NULL, ...) { training[, unique(c(col_names$top, col_names$bottom))], types = c("double", "integer") ) + check_function(x$naming, arg = "naming") step_ratio_new( terms = x$terms, diff --git a/R/regex.R b/R/regex.R index b89f2903f..e731a10a9 100644 --- a/R/regex.R +++ b/R/regex.R @@ -63,9 +63,7 @@ step_regex <- function(recipe, keep_original_cols = TRUE, skip = FALSE, id = rand_id("regex")) { - if (!is_tune(pattern)) { - check_string(pattern) - } + valid_args <- names(formals(grepl))[-(1:2)] if (any(!(names(options) %in% valid_args))) { cli::cli_abort(c( @@ -123,6 +121,7 @@ step_regex_new <- prep.step_regex <- function(x, training, info = NULL, ...) { col_name <- recipes_eval_select(x$terms, training, info) check_type(training[, col_name], types = c("string", "factor", "ordered")) + check_string(x$pattern, arg = "pattern", allow_empty = FALSE) step_regex_new( terms = x$terms, diff --git a/R/relevel.R b/R/relevel.R index 3d0a0a6d1..0b8c78114 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -85,6 +85,7 @@ step_relevel_new <- 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")) + check_string(x$ref_level, arg = "ref_level", allow_empty = FALSE) # Get existing levels and their factor type (i.e. ordered) objects <- lapply(training[, col_names], get_existing_values) diff --git a/R/relu.R b/R/relu.R index 61eeebd44..0ff1fdd36 100644 --- a/R/relu.R +++ b/R/relu.R @@ -82,18 +82,6 @@ step_relu <- columns = NULL, skip = FALSE, id = rand_id("relu")) { - if (!is_tune(shift)) { - check_number_decimal(shift) - } - if (!is_tune(reverse)) { - check_bool(reverse) - } - if (!is_tune(smooth)) { - check_bool(smooth) - } - if (reverse & prefix == "right_relu_") { - prefix <- "left_relu_" - } add_step( recipe, step_relu_new( @@ -132,6 +120,12 @@ step_relu_new <- prep.step_relu <- function(x, training, info = NULL, ...) { columns <- recipes_eval_select(x$terms, training, info) check_type(training[, columns], types = c("double", "integer")) + check_number_decimal(x$shift, arg = "shift") + check_bool(x$reverse, arg = "reverse") + check_bool(x$smooth, arg = "smooth") + if (x$reverse & x$prefix == "right_relu_") { + x$prefix <- "left_relu_" + } step_relu_new( terms = x$terms, diff --git a/R/sample.R b/R/sample.R index 5d80c7364..50402cd38 100644 --- a/R/sample.R +++ b/R/sample.R @@ -69,18 +69,6 @@ step_sample <- function(recipe, cli::cli_warn("Selectors are not used for this step.") } - if (!is_tune(size)) { - check_number_decimal(size, min = 0, allow_null = TRUE) - } - if (!is_tune(replace)) { - if (!is.logical(replace)) { - cli::cli_abort( - "{.arg replace} should be a single logical, \\ - not {.obj_type_friendly {replace}}." - ) - } - } - add_step( recipe, step_sample_new( @@ -113,6 +101,9 @@ step_sample_new <- #' @export prep.step_sample <- function(x, training, info = NULL, ...) { + + check_number_decimal(x$size, min = 0, allow_null = TRUE, arg = "size") + check_bool(x$replace, arg = "replace") if (is.null(x$size)) { x$size <- nrow(training) } diff --git a/R/scale.R b/R/scale.R index 28c8eef8a..0c0d43e86 100644 --- a/R/scale.R +++ b/R/scale.R @@ -107,6 +107,13 @@ step_scale_new <- prep.step_scale <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$na_rm, arg = "na_rm") + if (x$factor != 1 & x$factor != 2) { + cli::cli_warn( + "Scaling {.arg factor} should take either a value of 1 or 2, not + {.obj_type_friendly {x$factor}}." + ) + } wts <- get_case_weights(info, training) were_weights_used <- are_weights_used(wts, unsupervised = TRUE) @@ -114,12 +121,6 @@ prep.step_scale <- function(x, training, info = NULL, ...) { wts <- NULL } - if (x$factor != 1 & x$factor != 2) { - cli::cli_warn( - "Scaling {.arg factor} should take either a value of 1 or 2, \\ - not {x$factor}." - ) - } vars <- variances(training[, col_names], wts, na_rm = x$na_rm) sds <- sqrt(vars) diff --git a/R/spatialsign.R b/R/spatialsign.R index 0a70cbeee..a7bb1dc33 100644 --- a/R/spatialsign.R +++ b/R/spatialsign.R @@ -116,6 +116,7 @@ step_spatialsign_new <- prep.step_spatialsign <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$na_rm, arg = "na_rm") wts <- get_case_weights(info, training) were_weights_used <- are_weights_used(wts, unsupervised = TRUE) diff --git a/R/spline_b.R b/R/spline_b.R index a77a6a8d7..eb62f1f5b 100644 --- a/R/spline_b.R +++ b/R/spline_b.R @@ -136,7 +136,10 @@ step_spline_b_new <- prep.step_spline_b <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) - + check_bool(x$complete_set, arg = "complete_set") + check_number_whole(x$degree, arg = "degree", min = 0) + check_number_whole(x$deg_free, arg = "deg_free", min = 0) + res <- list() for (col_name in col_names) { diff --git a/R/spline_convex.R b/R/spline_convex.R index d962c1b44..5ed3626b3 100644 --- a/R/spline_convex.R +++ b/R/spline_convex.R @@ -124,6 +124,8 @@ step_spline_convex_new <- prep.step_spline_convex <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$complete_set, arg = "complete_set") + check_number_whole(x$degree, arg = "degree", min = 0) res <- list() diff --git a/R/spline_monotone.R b/R/spline_monotone.R index 798dab07a..1bfb706b4 100644 --- a/R/spline_monotone.R +++ b/R/spline_monotone.R @@ -125,6 +125,9 @@ step_spline_monotone_new <- prep.step_spline_monotone <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$complete_set, arg = "complete_set") + check_number_whole(x$degree, arg = "degree", min = 0) + check_number_whole(x$deg_free, arg = "deg_free", min = 0) res <- list() diff --git a/R/spline_natural.R b/R/spline_natural.R index 7c37c2951..0d3e3ecb7 100644 --- a/R/spline_natural.R +++ b/R/spline_natural.R @@ -119,6 +119,8 @@ step_spline_natural_new <- prep.step_spline_natural <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$complete_set, arg = "complete_set") + check_number_whole(x$deg_free, arg = "deg_free", min = 2) res <- list() diff --git a/R/spline_nonnegative.R b/R/spline_nonnegative.R index 46f8d64da..5adf0f4ce 100644 --- a/R/spline_nonnegative.R +++ b/R/spline_nonnegative.R @@ -136,6 +136,9 @@ step_spline_nonnegative_new <- prep.step_spline_nonnegative <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + check_bool(x$complete_set, arg = "complete_set") + check_number_whole(x$degree, arg = "degree", min = 0) + check_number_whole(x$deg_free, arg = "deg_free", min = 0) res <- list() diff --git a/R/string2factor.R b/R/string2factor.R index fbdda614e..65fd11b74 100644 --- a/R/string2factor.R +++ b/R/string2factor.R @@ -8,7 +8,7 @@ #' strings to factors before using any tidymodels functions. #' #' @inheritParams step_center -#' @param levels An options specification of the levels to be used +#' @param levels An optional specification of the levels to be used #' for the new factor. If left `NULL`, the sorted unique #' values present when `bake` is called will be used. #' @param ordered A single logical value; should the factor(s) be @@ -89,9 +89,6 @@ step_string2factor <- skip = FALSE, id = rand_id("string2factor")) { - if (!is_tune(ordered)) { - check_bool(ordered) - } check_character(levels, allow_null = TRUE) add_step( @@ -130,6 +127,7 @@ get_ord_lvls <- function(x) { prep.step_string2factor <- 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_bool(x$ordered, arg = "ordered") if (is.null(x$levels)) { res <- lapply(training[, col_names], get_ord_lvls) diff --git a/R/tidy.R b/R/tidy.R index d87e12301..50300a0f4 100644 --- a/R/tidy.R +++ b/R/tidy.R @@ -56,17 +56,8 @@ tidy.recipe <- function(x, number = NA, id = NA, ...) { num_oper <- length(x$steps) pattern <- "(^step_)|(^check_)" - if (length(id) != 1L) { - cli::cli_abort( - "If {.arg id} is provided, it must be a length 1 character vector." - ) - } - - if (length(number) != 1L) { - cli::cli_abort( - "If {.arg number} is provided, it must be a length 1 integer vector." - ) - } + check_string(id, allow_na = TRUE, allow_empty = FALSE) + check_number_whole(number, allow_na = TRUE) if (!is.na(id)) { if (!is.na(number)) { @@ -75,11 +66,7 @@ tidy.recipe <- function(x, number = NA, id = NA, ...) { ) } step_ids <- vapply(x$steps, function(x) x$id, character(1)) - if (!(id %in% step_ids)) { - cli::cli_abort( - "Supplied {.arg id} ({.val {id}}) not found in the recipe." - ) - } + id <- rlang::arg_match(id, step_ids) number <- which(id == step_ids) } if (is.na(number)) { @@ -109,8 +96,8 @@ tidy.recipe <- function(x, number = NA, id = NA, ...) { } else { if (number > num_oper || length(number) > 1) { cli::cli_abort( - "{.arg number} should be a single value between 1 and {num_oper}, \\ - not {number}." + "{.arg number} should be a single value between 1 and {num_oper}, + not {.obj_type_friendly {number}}." ) } res <- tidy(x$steps[[number]], ...) diff --git a/R/time.R b/R/time.R index 9f392b812..2b46aaa64 100644 --- a/R/time.R +++ b/R/time.R @@ -67,25 +67,7 @@ step_time <- keep_original_cols = TRUE, skip = FALSE, id = rand_id("time")) { - feat <- - c( - "am", - "hour", - "hour12", - "minute", - "second", - "decimal_day" - ) - if (!is_tune(features)) { - if (!all(features %in% feat)) { - offenders <- features[!features %in% feat] - - cli::cli_abort(c( - x = "Possible values of {.arg features} are: {.or {.val {feat}}}.", - i = "Invalid values were: {.val {offenders}}." - )) - } - } + add_step( recipe, step_time_new( @@ -117,12 +99,27 @@ step_time_new <- ) } +feat_list <- + c( + "am", + "hour", + "hour12", + "minute", + "second", + "decimal_day" + ) #' @export prep.step_time <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = "datetime") + + features <- x$features + check_character(features, allow_na = FALSE) + x$features <- rlang::arg_match(features, feat_list, multiple = TRUE, + error_arg = "features") + step_time_new( terms = x$terms, role = x$role, diff --git a/R/unknown.R b/R/unknown.R index d60f7d3b7..b3959b203 100644 --- a/R/unknown.R +++ b/R/unknown.R @@ -93,6 +93,7 @@ step_unknown_new <- prep.step_unknown <- 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_string(x$new_level, arg = "new_level", allow_empty = FALSE) # Get existing levels and their factor type (i.e. ordered) objects <- lapply(training[, col_names], get_existing_values) diff --git a/R/window.R b/R/window.R index 2f086f16d..121a677dc 100644 --- a/R/window.R +++ b/R/window.R @@ -123,34 +123,10 @@ step_window <- keep_original_cols = TRUE, skip = FALSE, id = rand_id("window")) { - if (!is_call(statistic) && - (!(statistic %in% roll_funs) | length(statistic) != 1)) { - cli::cli_abort( - "{.arg statistic} should be one of: {.or {.val {roll_funs}}}." - ) + if (!is_call(statistic)) { + statistic <- rlang::arg_match(statistic, roll_funs) } - ## ensure size is odd, integer, and not too small - if (!is_tune(size)) { - check_number_decimal(size, min = 0) - - if (!is.integer(size)) { - tmp <- size - size <- as.integer(size) - if (!isTRUE(all.equal(tmp, size))) { - cli::cli_warn( - "{.arg size} was not an integer ({tmp}) and was converted to \\ - {size}." - ) - } - } - if (size %% 2 == 0) { - cli::cli_abort("{.arg size} should be odd, not {size}.") - } - if (size < 3) { - cli::cli_abort("{.arg size} should be at least 3, not {size}.") - } - } add_step( recipe, step_window_new( @@ -195,11 +171,30 @@ prep.step_window <- function(x, training, info = NULL, ...) { col_names <- recipes_eval_select(x$terms, training, info) check_type(training[, col_names], types = c("double", "integer")) + ## ensure size is odd, integer, and not too small + check_number_whole(x$size, arg = "size", min = 3) + + if (!is.integer(x$size)) { + tmp <- x$size + x$size <- as.integer(x$size) + if (!isTRUE(all.equal(tmp, x$size))) { + cli::cli_warn( + "{.arg size} was not an integer ({tmp}) and was converted to {x$size}." + ) + } + } + if (x$size %% 2 == 0) { + cli::cli_abort("{.arg size} should be odd, not {x$size}.") + } + if (x$size < 3) { + cli::cli_abort("{.arg size} should be at least 3, not {x$size}.") + } + if (!is.null(x$names)) { if (length(x$names) != length(col_names)) { cli::cli_abort( - "There were {length(col_names)} term{?s} selected but \\ - {length(x$names)} value{?s} for the new features {?was/were} passed \\ + "There were {length(col_names)} term{?s} selected but + {length(x$names)} value{?s} for the new features {?was/were} passed to {.arg names}." ) } diff --git a/man/step_poly.Rd b/man/step_poly.Rd index 5d358ccf7..809a72689 100644 --- a/man/step_poly.Rd +++ b/man/step_poly.Rd @@ -10,7 +10,7 @@ step_poly( role = "predictor", trained = FALSE, objects = NULL, - degree = 2, + degree = 2L, options = list(), keep_original_cols = FALSE, skip = FALSE, diff --git a/man/step_range.Rd b/man/step_range.Rd index 5e41a7bc1..bf4177b42 100644 --- a/man/step_range.Rd +++ b/man/step_range.Rd @@ -30,11 +30,8 @@ created.} \item{trained}{A logical to indicate if the quantities for preprocessing have been estimated.} -\item{min}{A single numeric value for the smallest value in the -range.} - -\item{max}{A single numeric value for the largest value in the -range.} +\item{min, max}{Single numeric values for the smallest (or largest) value in +the transformed data.} \item{clipping}{A single logical value for determining whether application of transformation onto new data should be forced diff --git a/man/step_string2factor.Rd b/man/step_string2factor.Rd index 4de7c73c6..a04075588 100644 --- a/man/step_string2factor.Rd +++ b/man/step_string2factor.Rd @@ -28,7 +28,7 @@ created.} \item{trained}{A logical to indicate if the quantities for preprocessing have been estimated.} -\item{levels}{An options specification of the levels to be used +\item{levels}{An optional specification of the levels to be used for the new factor. If left \code{NULL}, the sorted unique values present when \code{bake} is called will be used.} diff --git a/tests/testthat/_snaps/YeoJohnson.md b/tests/testthat/_snaps/YeoJohnson.md index 556276ff5..cdb605381 100644 --- a/tests/testthat/_snaps/YeoJohnson.md +++ b/tests/testthat/_snaps/YeoJohnson.md @@ -84,3 +84,33 @@ -- Operations * Yeo-Johnson transformation on: x1, x2, x4 | Trained +# bad args + + Code + recipe(~., data = ex_dat) %>% step_YeoJohnson(x1, x2, x3, x4, na_rm = "yes") %>% + prep() + Condition + Error in `step_YeoJohnson()`: + Caused by error in `prep()`: + ! `na_rm` must be `TRUE` or `FALSE`, not the string "yes". + +--- + + Code + recipe(~., data = ex_dat) %>% step_YeoJohnson(x1, x2, x3, x4, num_unique = "yes") %>% + prep() + Condition + Error in `step_YeoJohnson()`: + Caused by error in `prep()`: + ! `x$num_unique` must be a whole number, not the string "yes". + +--- + + Code + recipe(~., data = ex_dat) %>% step_YeoJohnson(x1, x2, x3, x4, limits = NA_real_) %>% + prep() + Condition + Error in `step_YeoJohnson()`: + Caused by error in `prep()`: + ! `limits` should be a numeric vector with two values, not a double vector + diff --git a/tests/testthat/_snaps/lag.md b/tests/testthat/_snaps/lag.md index cbffae302..063ef55ac 100644 --- a/tests/testthat/_snaps/lag.md +++ b/tests/testthat/_snaps/lag.md @@ -7,6 +7,15 @@ Caused by error in `prep()`: ! `lag` argument must be integer-valued, not a function. +--- + + Code + recipe(~., data = df) %>% step_lag(x, prefix = 2) %>% prep() + Condition + Error in `step_lag()`: + Caused by error in `prep()`: + ! `prefix` must be a single string, not the number 2. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/lincomb.md b/tests/testthat/_snaps/lincomb.md index fe0c8a1d7..0cfa584aa 100644 --- a/tests/testthat/_snaps/lincomb.md +++ b/tests/testthat/_snaps/lincomb.md @@ -68,3 +68,12 @@ -- Operations * Linear combination filter removed: N1, P1, K1 | Trained +# bad args + + Code + dum_rec %>% step_lincomb(all_predictors(), max_steps = 0) %>% prep() + Condition + Error in `step_lincomb()`: + Caused by error in `prep()`: + ! `max_steps` must be a whole number larger than or equal to 1, not the number 0. + diff --git a/tests/testthat/_snaps/log.md b/tests/testthat/_snaps/log.md index 4efa7ed4f..781985886 100644 --- a/tests/testthat/_snaps/log.md +++ b/tests/testthat/_snaps/log.md @@ -95,3 +95,30 @@ -- Operations * Log transformation on: x1, x2, x3, x4 | Trained +# bad args + + Code + recipe(~., data = ex_dat) %>% step_log(x1, base = -1) %>% prep() + Condition + Error in `step_log()`: + Caused by error in `prep()`: + ! `base` must be a number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(~., data = ex_dat) %>% step_log(x1, offset = "none") %>% prep() + Condition + Error in `step_log()`: + Caused by error in `prep()`: + ! `offset` must be a number, not the string "none". + +--- + + Code + recipe(~., data = ex_dat) %>% step_log(x1, signed = "yes") %>% prep() + Condition + Error in `step_log()`: + Caused by error in `prep()`: + ! `signed` must be `TRUE` or `FALSE`, not the string "yes". + diff --git a/tests/testthat/_snaps/logit.md b/tests/testthat/_snaps/logit.md index 1031eee97..ed36b1b97 100644 --- a/tests/testthat/_snaps/logit.md +++ b/tests/testthat/_snaps/logit.md @@ -83,3 +83,12 @@ -- Operations * Logit transformation on: x1 | Trained +# bad args + + Code + recipe(~., data = ex_dat) %>% step_logit(x1, offset = "sure") %>% prep() + Condition + Error in `step_logit()`: + Caused by error in `prep()`: + ! `offset` must be a number, not the string "sure". + diff --git a/tests/testthat/_snaps/newvalues.md b/tests/testthat/_snaps/newvalues.md index 02a65e9ac..af7fd4574 100644 --- a/tests/testthat/_snaps/newvalues.md +++ b/tests/testthat/_snaps/newvalues.md @@ -197,3 +197,12 @@ -- Operations * Checking no new_values for: disp | Trained +# bad args + + Code + recipe(mpg ~ ., mtcars) %>% check_new_values(disp, ignore_NA = 2) %>% prep() + Condition + Error in `check_new_values()`: + Caused by error in `prep()`: + ! `ignore_NA` must be `TRUE` or `FALSE`, not the number 2. + diff --git a/tests/testthat/_snaps/nnmf_sparse.md b/tests/testthat/_snaps/nnmf_sparse.md index 507aa247c..13a56b71d 100644 --- a/tests/testthat/_snaps/nnmf_sparse.md +++ b/tests/testthat/_snaps/nnmf_sparse.md @@ -115,3 +115,31 @@ -- Operations * Non-negative matrix factorization for: disp and drat | Trained +# bad args + + Code + recipe(mpg ~ ., mtcars) %>% step_nnmf_sparse(disp, drat, num_comp = -1) %>% + prep() + Condition + Error in `step_nnmf_sparse()`: + Caused by error in `prep()`: + ! `num_comp` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., mtcars) %>% step_nnmf_sparse(disp, drat, penalty = -1) %>% prep() + Condition + Error in `step_nnmf_sparse()`: + Caused by error in `prep()`: + ! `penalty` must be a number larger than or equal to 2.22044604925031e-16, not the number -1. + +--- + + Code + recipe(mpg ~ ., mtcars) %>% step_nnmf_sparse(disp, drat, prefix = 1) %>% prep() + Condition + Error in `step_nnmf_sparse()`: + Caused by error in `prep()`: + ! `prefix` must be a single string, not the number 1. + diff --git a/tests/testthat/_snaps/normalize.md b/tests/testthat/_snaps/normalize.md index ab1abea71..4a12fdcf2 100644 --- a/tests/testthat/_snaps/normalize.md +++ b/tests/testthat/_snaps/normalize.md @@ -7,6 +7,16 @@ Warning: Columns `mpg`, `cyl`, `disp`, and `hp` returned NaN, because variance cannot be calculated and scaling cannot be used. Consider avoiding `Inf` or `-Inf` values and/or setting `na_rm = TRUE` before normalizing. +--- + + Code + recipe(~., data = mtcars_na) %>% step_normalize(all_predictors(), na_rm = 2) %>% + prep() + Condition + Error in `step_normalize()`: + Caused by error in `prep()`: + ! `na_rm` must be `TRUE` or `FALSE`, not the number 2. + # warns on zv Code diff --git a/tests/testthat/_snaps/novel.md b/tests/testthat/_snaps/novel.md index 4df85f8f1..e0bdfd18e 100644 --- a/tests/testthat/_snaps/novel.md +++ b/tests/testthat/_snaps/novel.md @@ -17,6 +17,15 @@ Caused by error in `prep()`: ! Columns already contain the new level: x. +--- + + Code + rec %>% step_novel(all_predictors(), new_level = letters) %>% prep() + Condition + Error in `step_novel()`: + Caused by error in `prep()`: + ! `new_level` must be a single string, not a character vector. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/num2factor.md b/tests/testthat/_snaps/num2factor.md index fbbd456b8..874f8da87 100644 --- a/tests/testthat/_snaps/num2factor.md +++ b/tests/testthat/_snaps/num2factor.md @@ -16,6 +16,15 @@ Error in `step_num2factor()`: ! Please provide a character vector of appropriate length for `levels`. +--- + + Code + rec %>% step_num2factor(z, levels = rev(LETTERS[1:10]), transform = 2) %>% prep() + Condition + Error in `step_num2factor()`: + Caused by error in `prep()`: + ! `transform` must be a function, not the number 2. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/nzv.md b/tests/testthat/_snaps/nzv.md index 21010f4ec..f8d5bec40 100644 --- a/tests/testthat/_snaps/nzv.md +++ b/tests/testthat/_snaps/nzv.md @@ -124,3 +124,21 @@ -- Operations * Sparse, unbalanced variable filter removed: x3 and x4 | Trained +# bad args + + Code + recipe(y ~ ., data = dat) %>% step_nzv(x1, freq_cut = -1) %>% prep() + Condition + Error in `step_nzv()`: + Caused by error in `prep()`: + ! `freq_cut` must be a number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(y ~ ., data = dat) %>% step_nzv(x1, unique_cut = 101) %>% prep() + Condition + Error in `step_nzv()`: + Caused by error in `prep()`: + ! `unique_cut` must be a number between 0 and 100, not the number 101. + diff --git a/tests/testthat/_snaps/ordinalscore.md b/tests/testthat/_snaps/ordinalscore.md index 616336e5c..38905255f 100644 --- a/tests/testthat/_snaps/ordinalscore.md +++ b/tests/testthat/_snaps/ordinalscore.md @@ -85,3 +85,13 @@ -- Operations * Scoring for: ord1, ord2, ord3 | Trained +# bad args + + Code + recipe(~., data = ex_dat) %>% step_ordinalscore(starts_with("ord"), convert = NULL) %>% + prep() + Condition + Error in `step_ordinalscore()`: + Caused by error in `prep()`: + ! `convert` must be a function, not `NULL`. + diff --git a/tests/testthat/_snaps/other.md b/tests/testthat/_snaps/other.md index 157fd6268..6b00b50b1 100644 --- a/tests/testthat/_snaps/other.md +++ b/tests/testthat/_snaps/other.md @@ -10,11 +10,21 @@ # if the threshold argument is greather than one then it should be an integer(ish) Code - rec %>% step_other(city, zip, threshold = 3.14) + rec %>% step_other(city, zip, threshold = 3.14) %>% prep() Condition Error in `step_other()`: + Caused by error in `prep()`: ! `threshold` must be a whole number, not the number 3.14. +# bad values of threshold are treated correctly + + Code + rec %>% step_other(city, zip, threshold = letters) %>% prep() + Condition + Error in `step_other()`: + Caused by error in `prep()`: + ! `threshold` should be a single numeric value a character vector + # othering with case weights Code diff --git a/tests/testthat/_snaps/pca.md b/tests/testthat/_snaps/pca.md index fac5da890..c90a21506 100644 --- a/tests/testthat/_snaps/pca.md +++ b/tests/testthat/_snaps/pca.md @@ -184,3 +184,33 @@ -- Operations * PCA extraction with: carbon, hydrogen, oxygen, nitrogen, sulfur | Trained +# bad args + + Code + recipe(~., data = mtcars) %>% step_pca(all_numeric_predictors(), num_comp = -1) %>% + prep() + Condition + Error in `step_pca()`: + Caused by error in `prep()`: + ! `num_comp` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(~., data = mtcars) %>% step_pca(all_numeric_predictors(), prefix = 1) %>% + prep() + Condition + Error in `step_pca()`: + Caused by error in `prep()`: + ! `prefix` must be a single string, not the number 1. + +--- + + Code + recipe(~., data = mtcars) %>% step_pca(all_numeric_predictors(), threshold = -1) %>% + prep() + Condition + Error in `step_pca()`: + Caused by error in `prep()`: + ! `threshold` must be a number between 0 and 1 or `NA`, not the number -1. + diff --git a/tests/testthat/_snaps/pls.md b/tests/testthat/_snaps/pls.md index 7657e6c31..61fbd80c5 100644 --- a/tests/testthat/_snaps/pls.md +++ b/tests/testthat/_snaps/pls.md @@ -139,3 +139,33 @@ -- Operations * PLS feature extraction with: carbon, hydrogen, oxygen, ... | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_pls(-mpg, outcome = "mpg", num_comp = - + 1) %>% prep() + Condition + Error in `step_pls()`: + Caused by error in `prep()`: + ! `num_comp` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_pls(-mpg, outcome = "mpg", prefix = 1) %>% + prep() + Condition + Error in `step_pls()`: + Caused by error in `prep()`: + ! `prefix` must be a single string, not the number 1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_pls(-mpg, outcome = "mpg", + predictor_prop = -1) %>% prep() + Condition + Error in `step_pls()`: + Caused by error in `prep()`: + ! `predictor_prop` must be a number between 0 and 1, not the number -1. + diff --git a/tests/testthat/_snaps/poly.md b/tests/testthat/_snaps/poly.md index 89d83e426..e39b9b9e1 100644 --- a/tests/testthat/_snaps/poly.md +++ b/tests/testthat/_snaps/poly.md @@ -103,3 +103,12 @@ -- Operations * Orthogonal polynomials on: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_poly(disp, degree = 0) %>% prep() + Condition + Error in `step_poly()`: + Caused by error in `prep()`: + ! `degree` must be a whole number larger than or equal to 1, not the number 0. + diff --git a/tests/testthat/_snaps/poly_bernstein.md b/tests/testthat/_snaps/poly_bernstein.md index caf5e436e..ec06f3661 100644 --- a/tests/testthat/_snaps/poly_bernstein.md +++ b/tests/testthat/_snaps/poly_bernstein.md @@ -86,3 +86,23 @@ -- Operations * Bernstein polynomial expansion: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_poly_bernstein(disp, degree = -1) %>% + prep() + Condition + Error in `step_poly_bernstein()`: + Caused by error in `prep()`: + ! `degree` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_poly_bernstein(disp, complete_set = 1) %>% + prep() + Condition + Error in `step_poly_bernstein()`: + Caused by error in `prep()`: + ! `complete_set` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/_snaps/range.md b/tests/testthat/_snaps/range.md index ec1188b1e..057df8675 100644 --- a/tests/testthat/_snaps/range.md +++ b/tests/testthat/_snaps/range.md @@ -139,3 +139,31 @@ -- Operations * Range scaling to [0,1] for: disp and wt | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_range(disp, wt, max = "max") %>% prep() + Condition + Error in `step_range()`: + Caused by error in `prep()`: + ! `max` must be a number, not the string "max". + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_range(disp, wt, min = "min") %>% prep() + Condition + Error in `step_range()`: + Caused by error in `prep()`: + ! `min` must be a number, not the string "min". + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_range(disp, wt, clipping = "never") %>% + prep() + Condition + Error in `step_range()`: + Caused by error in `prep()`: + ! `clipping` must be `TRUE` or `FALSE`, not the string "never". + diff --git a/tests/testthat/_snaps/ratio.md b/tests/testthat/_snaps/ratio.md index 5b2d3843c..af5e2a0f2 100644 --- a/tests/testthat/_snaps/ratio.md +++ b/tests/testthat/_snaps/ratio.md @@ -132,3 +132,13 @@ -- Operations * Ratios from: x2, x3, x4, x1, x1, x2, x3, x4 | Trained +# bad args + + Code + recipe(~ mpg + disp, mtcars) %>% step_ratio(mpg, denom = denom_vars(disp), + naming = NULL) %>% prep() + Condition + Error in `step_ratio()`: + Caused by error in `prep()`: + ! `naming` must be a function, not `NULL`. + diff --git a/tests/testthat/_snaps/regex.md b/tests/testthat/_snaps/regex.md index c015301b0..9c33e960c 100644 --- a/tests/testthat/_snaps/regex.md +++ b/tests/testthat/_snaps/regex.md @@ -131,3 +131,12 @@ -- Operations * Regular expression dummy variable using: "(rock|stony)" | Trained +# bad args + + Code + rec %>% step_regex(description, pattern = character(0)) %>% prep() + Condition + Error in `step_regex()`: + Caused by error in `prep()`: + ! `pattern` must be a single string, not an empty character vector. + diff --git a/tests/testthat/_snaps/relevel.md b/tests/testthat/_snaps/relevel.md index 8ed1a4246..8023a4501 100644 --- a/tests/testthat/_snaps/relevel.md +++ b/tests/testthat/_snaps/relevel.md @@ -17,6 +17,15 @@ Caused by error in `prep()`: ! The following column doesn't include required reference level "missing_level": `city`. +--- + + Code + rec %>% step_relevel(city, ref_level = character(0)) %>% prep() + Condition + Error in `step_relevel()`: + Caused by error in `prep()`: + ! `ref_level` must be a single string, not an empty character vector. + # bake method errors when needed non-standard role columns are missing Code diff --git a/tests/testthat/_snaps/relu.md b/tests/testthat/_snaps/relu.md index e30d75c3f..56e218687 100644 --- a/tests/testthat/_snaps/relu.md +++ b/tests/testthat/_snaps/relu.md @@ -4,6 +4,7 @@ recipe(~., data = df) %>% step_relu(val1, shift = TRUE) %>% prep(df, verbose = FALSE) Condition Error in `step_relu()`: + Caused by error in `prep()`: ! `shift` must be a number, not `TRUE`. --- @@ -12,6 +13,7 @@ recipe(~., data = df) %>% step_relu(val1, reverse = 3) %>% prep(df, verbose = FALSE) Condition Error in `step_relu()`: + Caused by error in `prep()`: ! `reverse` must be `TRUE` or `FALSE`, not the number 3. --- @@ -20,6 +22,7 @@ recipe(~., data = df) %>% step_relu(val1, smooth = "cat") %>% prep(df, verbose = FALSE) Condition Error in `step_relu()`: + Caused by error in `prep()`: ! `smooth` must be `TRUE` or `FALSE`, not the string "cat". --- diff --git a/tests/testthat/_snaps/rename_at.md b/tests/testthat/_snaps/rename_at.md index 8895858f1..8642742e9 100644 --- a/tests/testthat/_snaps/rename_at.md +++ b/tests/testthat/_snaps/rename_at.md @@ -16,6 +16,16 @@ Error in `step_rename_at()`: ! Argument `fn` must be specified. +--- + + Code + iris_rec %>% step_rename_at(fn = ":=O") %>% prep(training = iris) %>% bake( + new_data = NULL, composition = "data.frame") + Condition + Error in `step_rename_at()`: + Caused by error in `get()`: + ! object ':=O' of mode 'function' was not found + # empty printing Code diff --git a/tests/testthat/_snaps/sample.md b/tests/testthat/_snaps/sample.md index 7f8c86b3c..48e13890c 100644 --- a/tests/testthat/_snaps/sample.md +++ b/tests/testthat/_snaps/sample.md @@ -1,26 +1,29 @@ # bad input Code - iris_rec %>% step_sample(size = -1) + iris_rec %>% step_sample(size = -1) %>% prep() Condition Error in `step_sample()`: + Caused by error in `prep()`: ! `size` must be a number larger than or equal to 0 or `NULL`, not the number -1. --- Code - iris_rec %>% step_sample(size = "a") + iris_rec %>% step_sample(size = "a") %>% prep() Condition Error in `step_sample()`: + Caused by error in `prep()`: ! `size` must be a number or `NULL`, not the string "a". --- Code - iris_rec %>% step_sample(replace = "a") + iris_rec %>% step_sample(replace = "a") %>% prep() Condition Error in `step_sample()`: - ! `replace` should be a single logical, not a string. + Caused by error in `prep()`: + ! `replace` must be `TRUE` or `FALSE`, not the string "a". # sample with case weights diff --git a/tests/testthat/_snaps/scale.md b/tests/testthat/_snaps/scale.md index 90c128b5d..79030e177 100644 --- a/tests/testthat/_snaps/scale.md +++ b/tests/testthat/_snaps/scale.md @@ -5,7 +5,7 @@ factor = 3) %>% prep(training = biomass) Condition Warning: - Scaling `factor` should take either a value of 1 or 2, not 3. + Scaling `factor` should take either a value of 1 or 2, not a number. # na_rm argument works for step_scale @@ -16,6 +16,16 @@ Warning: Columns `mpg`, `cyl`, `disp`, and `hp` returned NaN, because variance cannot be calculated and scaling cannot be used. Consider avoiding `Inf` or `-Inf` values and/or setting `na_rm = TRUE` before normalizing. +--- + + Code + rec_no_na_rm <- recipe(~., data = mtcars_na) %>% step_scale(all_predictors(), + na_rm = "FALSE") %>% prep() + Condition + Error in `step_scale()`: + Caused by error in `prep()`: + ! `na_rm` must be `TRUE` or `FALSE`, not the string "FALSE". + # warns on zv Code diff --git a/tests/testthat/_snaps/spatialsign.md b/tests/testthat/_snaps/spatialsign.md index 340a96693..1ea68873a 100644 --- a/tests/testthat/_snaps/spatialsign.md +++ b/tests/testthat/_snaps/spatialsign.md @@ -1,3 +1,12 @@ +# spatial sign + + Code + rec %>% step_spatialsign(carbon, hydrogen, na_rm = 12) %>% prep() + Condition + Error in `step_spatialsign()`: + Caused by error in `prep()`: + ! `na_rm` must be `TRUE` or `FALSE`, not the number 12. + # centering with case weights Code diff --git a/tests/testthat/_snaps/spline_b.md b/tests/testthat/_snaps/spline_b.md index d4e198800..a5b556536 100644 --- a/tests/testthat/_snaps/spline_b.md +++ b/tests/testthat/_snaps/spline_b.md @@ -106,3 +106,31 @@ -- Operations * Basis spline expansion: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_b(disp, degree = -1) %>% prep() + Condition + Error in `step_spline_b()`: + Caused by error in `prep()`: + ! `degree` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_b(disp, deg_free = "a") %>% prep() + Condition + Error in `step_spline_b()`: + Caused by error in `prep()`: + ! `deg_free` must be a whole number, not the string "a". + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_b(disp, complete_set = 1) %>% + prep() + Condition + Error in `step_spline_b()`: + Caused by error in `prep()`: + ! `complete_set` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/_snaps/spline_convex.md b/tests/testthat/_snaps/spline_convex.md index 0750fa863..925121b0e 100644 --- a/tests/testthat/_snaps/spline_convex.md +++ b/tests/testthat/_snaps/spline_convex.md @@ -106,3 +106,23 @@ -- Operations * Convex spline expansion: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_convex(disp, degree = -1) %>% + prep() + Condition + Error in `step_spline_convex()`: + Caused by error in `prep()`: + ! `degree` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_convex(disp, complete_set = 1) %>% + prep() + Condition + Error in `step_spline_convex()`: + Caused by error in `prep()`: + ! `complete_set` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/_snaps/spline_monotone.md b/tests/testthat/_snaps/spline_monotone.md index 1b2bcfffa..98fe7fa19 100644 --- a/tests/testthat/_snaps/spline_monotone.md +++ b/tests/testthat/_snaps/spline_monotone.md @@ -106,3 +106,33 @@ -- Operations * Monotone spline expansion: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_monotone(disp, degree = -1) %>% + prep() + Condition + Error in `step_spline_monotone()`: + Caused by error in `prep()`: + ! `degree` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_monotone(disp, deg_free = "a") %>% + prep() + Condition + Error in `step_spline_monotone()`: + Caused by error in `prep()`: + ! `deg_free` must be a whole number, not the string "a". + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_monotone(disp, complete_set = 1) %>% + prep() + Condition + Error in `step_spline_monotone()`: + Caused by error in `prep()`: + ! `complete_set` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/_snaps/spline_natural.md b/tests/testthat/_snaps/spline_natural.md index b46dd4f06..b0a111873 100644 --- a/tests/testthat/_snaps/spline_natural.md +++ b/tests/testthat/_snaps/spline_natural.md @@ -86,3 +86,23 @@ -- Operations * Natural spline expansion: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_natural(disp, deg_free = "a") %>% + prep() + Condition + Error in `step_spline_natural()`: + Caused by error in `prep()`: + ! `deg_free` must be a whole number, not the string "a". + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_natural(disp, complete_set = 1) %>% + prep() + Condition + Error in `step_spline_natural()`: + Caused by error in `prep()`: + ! `complete_set` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/_snaps/spline_nonnegative.md b/tests/testthat/_snaps/spline_nonnegative.md index 4d5c27252..1ae094562 100644 --- a/tests/testthat/_snaps/spline_nonnegative.md +++ b/tests/testthat/_snaps/spline_nonnegative.md @@ -106,3 +106,33 @@ -- Operations * Non-negative spline expansion: carbon and hydrogen | Trained +# bad args + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_nonnegative(disp, degree = -1) %>% + prep() + Condition + Error in `step_spline_nonnegative()`: + Caused by error in `prep()`: + ! `degree` must be a whole number larger than or equal to 0, not the number -1. + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_nonnegative(disp, deg_free = "a") %>% + prep() + Condition + Error in `step_spline_nonnegative()`: + Caused by error in `prep()`: + ! `deg_free` must be a whole number, not the string "a". + +--- + + Code + recipe(mpg ~ ., data = mtcars) %>% step_spline_nonnegative(disp, complete_set = 1) %>% + prep() + Condition + Error in `step_spline_nonnegative()`: + Caused by error in `prep()`: + ! `complete_set` must be `TRUE` or `FALSE`, not the number 1. + diff --git a/tests/testthat/_snaps/string2factor.md b/tests/testthat/_snaps/string2factor.md index 8bff4742a..d05df26a3 100644 --- a/tests/testthat/_snaps/string2factor.md +++ b/tests/testthat/_snaps/string2factor.md @@ -11,9 +11,10 @@ --- Code - rec %>% step_string2factor(n, ordered = "yes") %>% prep(ex_dat) + rec %>% step_string2factor(w, x, ordered = "yes") %>% prep(ex_dat) Condition Error in `step_string2factor()`: + Caused by error in `prep()`: ! `ordered` must be `TRUE` or `FALSE`, not the string "yes". # bake method errors when needed non-standard role columns are missing diff --git a/tests/testthat/_snaps/tidy.md b/tests/testthat/_snaps/tidy.md index 990bfa9c3..4326f212f 100644 --- a/tests/testthat/_snaps/tidy.md +++ b/tests/testthat/_snaps/tidy.md @@ -9,7 +9,7 @@ tidy(trained, number = NULL) Condition Error in `tidy()`: - ! If `number` is provided, it must be a length 1 integer vector. + ! `number` must be a whole number or `NA`, not `NULL`. --- @@ -17,7 +17,7 @@ tidy(trained, number = 100) Condition Error in `tidy()`: - ! `number` should be a single value between 1 and 4, not 100. + ! `number` should be a single value between 1 and 4, not a number. --- @@ -33,7 +33,7 @@ tidy(trained, id = "id") Condition Error in `tidy()`: - ! Supplied `id` ("id") not found in the recipe. + ! `id` must be one of "other_MvKZG", "center_p66OR", "dummy_5eKj5", or "cols_z8tZQ", not "id". --- @@ -41,7 +41,7 @@ tidy(trained, id = c("id", "id2")) Condition Error in `tidy()`: - ! If `id` is provided, it must be a length 1 character vector. + ! `id` must be a single string or `NA`, not a character vector. # bag args diff --git a/tests/testthat/_snaps/time.md b/tests/testthat/_snaps/time.md index 8f4cf54fb..0202bdcaa 100644 --- a/tests/testthat/_snaps/time.md +++ b/tests/testthat/_snaps/time.md @@ -15,8 +15,9 @@ 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". + Caused by error in `prep()`: + ! `features` must be one of "am", "hour", "hour12", "minute", "second", or "decimal_day", not "hourly". + i Did you mean "hour12"? # bake method errors when needed non-standard role columns are missing diff --git a/tests/testthat/_snaps/unknown.md b/tests/testthat/_snaps/unknown.md index 05ffc32b1..833293c90 100644 --- a/tests/testthat/_snaps/unknown.md +++ b/tests/testthat/_snaps/unknown.md @@ -32,6 +32,15 @@ Caused by error in `prep()`: ! Columns already contain the level "FAIR_OAKS": city. +--- + + Code + recipe(~., data = sacr_tr) %>% step_unknown(city, new_level = 2) %>% prep() + Condition + Error in `step_unknown()`: + Caused by error in `prep()`: + ! `new_level` must be a single string, not the number 2. + # 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 cd55e8764..dbe72aaea 100644 --- a/tests/testthat/_snaps/window.md +++ b/tests/testthat/_snaps/window.md @@ -1,26 +1,29 @@ # error checks Code - rec %>% step_window(y1, size = 6) + rec %>% step_window(y1, size = 6) %>% prep() Condition Error in `step_window()`: + Caused by error in `prep()`: ! `size` should be odd, not 6. --- Code - rec %>% step_window(y1, size = NA) + rec %>% step_window(y1, size = NA) %>% prep() Condition Error in `step_window()`: - ! `size` must be a number, not `NA`. + Caused by error in `prep()`: + ! `size` must be a whole number, not `NA`. --- Code - rec %>% step_window(y1, size = NULL) + rec %>% step_window(y1, size = NULL) %>% prep() Condition Error in `step_window()`: - ! `size` must be a number, not `NULL`. + Caused by error in `prep()`: + ! `size` must be a whole number, not `NULL`. --- @@ -28,39 +31,39 @@ rec %>% step_window(y1, statistic = "average") Condition Error in `step_window()`: - ! `statistic` should be one of: "mean", "median", "sd", "var", "sum", "prod", "min", or "max". + ! `statistic` must be one of "mean", "median", "sd", "var", "sum", "prod", "min", or "max", not "average". --- Code - rec %>% step_window(y1, size = 1) + rec %>% step_window(y1, size = 1) %>% prep() Condition Error in `step_window()`: - ! `size` should be at least 3, not 1. + Caused by error in `prep()`: + ! `size` must be a whole number larger than or equal to 3, not the number 1. --- Code - rec %>% step_window(y1, size = 2) + rec %>% step_window(y1, size = 2) %>% prep() Condition Error in `step_window()`: - ! `size` should be odd, not 2. + Caused by error in `prep()`: + ! `size` must be a whole number larger than or equal to 3, not the number 2. --- Code - rec %>% step_window(y1, size = -1) + rec %>% step_window(y1, size = -1) %>% prep() Condition Error in `step_window()`: - ! `size` must be a number larger than or equal to 0, not the number -1. + Caused by error in `prep()`: + ! `size` must be a whole number larger than or equal to 3, not the number -1. --- Code - rec %>% step_window(y1, size = pi) - Condition - Warning: - `size` was not an integer (3.14159265358979) and was converted to 3. + rec %>% step_window(y1, size = 3 + .Machine$double.eps) %>% prep() Message -- Recipe ---------------------------------------------------------------------- @@ -69,8 +72,20 @@ Number of variables by role predictor: 6 + -- Training information + Training data contained 81 data points and no incomplete rows. + -- Operations - * Moving 3-point mean on: y1 + * Moving 3-point mean on: y1 | Trained + +--- + + Code + rec %>% step_window(y1, size = 3 + 2 * .Machine$double.eps) %>% prep() + Condition + Error in `step_window()`: + Caused by error in `prep()`: + ! `size` must be a whole number, not the number 3. --- @@ -85,9 +100,10 @@ --- Code - prep(rec %>% step_window(y1, size = 1000L), training = sim_dat) + prep(rec %>% step_window(y1, size = 1000L), training = sim_dat) %>% prep() Condition Error in `step_window()`: + Caused by error in `prep()`: ! `size` should be odd, not 1000. --- diff --git a/tests/testthat/test-YeoJohnson.R b/tests/testthat/test-YeoJohnson.R index 19f28496f..e9e84727a 100644 --- a/tests/testthat/test-YeoJohnson.R +++ b/tests/testthat/test-YeoJohnson.R @@ -148,3 +148,28 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + + +test_that("bad args", { + + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_YeoJohnson(x1, x2, x3, x4, na_rm = "yes") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_YeoJohnson(x1, x2, x3, x4, num_unique = "yes") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_YeoJohnson(x1, x2, x3, x4, limits = NA_real_) %>% + prep(), + error = TRUE + ) +}) + + diff --git a/tests/testthat/test-lag.R b/tests/testthat/test-lag.R index ceee9d05d..9da5b4af7 100644 --- a/tests/testthat/test-lag.R +++ b/tests/testthat/test-lag.R @@ -42,6 +42,13 @@ test_that("default lag works on a single feature", { step_lag(x, lag = 0.5) %>% prep(df) ) + + expect_snapshot( + recipe(~., data = df) %>% + step_lag(x, prefix = 2) %>% + prep(), + error = TRUE + ) }) test_that("specification of multiple lags in a vector", { diff --git a/tests/testthat/test-lincomb.R b/tests/testthat/test-lincomb.R index ac11be70d..88001379d 100644 --- a/tests/testthat/test-lincomb.R +++ b/tests/testthat/test-lincomb.R @@ -118,3 +118,13 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + + +test_that("bad args", { + expect_snapshot( + dum_rec %>% + step_lincomb(all_predictors(), max_steps = 0) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-log.R b/tests/testthat/test-log.R index c76aa9b76..5862cf165 100644 --- a/tests/testthat/test-log.R +++ b/tests/testthat/test-log.R @@ -115,3 +115,25 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + +test_that("bad args", { + + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_log(x1, base = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_log(x1, offset = "none") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_log(x1, signed = "yes") %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-logit.R b/tests/testthat/test-logit.R index 3d316c889..5cc140d79 100644 --- a/tests/testthat/test-logit.R +++ b/tests/testthat/test-logit.R @@ -104,3 +104,12 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + +test_that("bad args", { + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_logit(x1, offset = "sure") %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-newvalues.R b/tests/testthat/test-newvalues.R index a123ddb8d..774851fc7 100644 --- a/tests/testthat/test-newvalues.R +++ b/tests/testthat/test-newvalues.R @@ -209,3 +209,13 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + + +test_that("bad args", { + expect_snapshot( + recipe(mpg ~ ., mtcars) %>% + check_new_values(disp, ignore_NA = 2) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-nnmf_sparse.R b/tests/testthat/test-nnmf_sparse.R index 2f922eda2..611206903 100644 --- a/tests/testthat/test-nnmf_sparse.R +++ b/tests/testthat/test-nnmf_sparse.R @@ -176,3 +176,27 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + + +test_that("bad args", { + skip_if_not_installed("RcppML") + + expect_snapshot( + recipe(mpg ~ ., mtcars) %>% + step_nnmf_sparse(disp, drat, num_comp = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., mtcars) %>% + step_nnmf_sparse(disp, drat, penalty = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., mtcars) %>% + step_nnmf_sparse(disp, drat, prefix = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-normalize.R b/tests/testthat/test-normalize.R index 612db165d..178530d3c 100644 --- a/tests/testthat/test-normalize.R +++ b/tests/testthat/test-normalize.R @@ -71,6 +71,13 @@ test_that("na_rm argument works for step_normalize", { prep() ) + expect_snapshot( + recipe(~., data = mtcars_na) %>% + step_normalize(all_predictors(), na_rm = 2) %>% + prep(), + error = TRUE + ) + rec_na_rm <- recipe(~., data = mtcars_na) %>% step_normalize(all_predictors(), na_rm = TRUE) %>% prep() diff --git a/tests/testthat/test-novel.R b/tests/testthat/test-novel.R index e424c6913..04de2e2b7 100644 --- a/tests/testthat/test-novel.R +++ b/tests/testthat/test-novel.R @@ -86,6 +86,12 @@ test_that("bad args", { step_novel(all_predictors()) %>% prep(tr_bad) ) + expect_snapshot( + rec %>% + step_novel(all_predictors(), new_level = letters) %>% + prep(), + error = TRUE + ) }) test_that("missing values", { diff --git a/tests/testthat/test-num2factor.R b/tests/testthat/test-num2factor.R index c279765ba..3748b0abd 100644 --- a/tests/testthat/test-num2factor.R +++ b/tests/testthat/test-num2factor.R @@ -45,6 +45,12 @@ test_that("bad args", { step_num2factor(w, x) %>% prep(ex_dat) ) + expect_snapshot( + rec %>% + step_num2factor(z, levels = rev(LETTERS[1:10]), transform = 2) %>% + prep(), + error = TRUE + ) }) # Infrastructure --------------------------------------------------------------- diff --git a/tests/testthat/test-nzv.R b/tests/testthat/test-nzv.R index 3392363b9..c5458cc4b 100644 --- a/tests/testthat/test-nzv.R +++ b/tests/testthat/test-nzv.R @@ -246,3 +246,19 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + +test_that("bad args", { + + expect_snapshot( + recipe(y ~ ., data = dat) %>% + step_nzv(x1, freq_cut = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(y ~ ., data = dat) %>% + step_nzv(x1, unique_cut = 101) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-ordinalscore.R b/tests/testthat/test-ordinalscore.R index a88e1dfb2..d16a496b1 100644 --- a/tests/testthat/test-ordinalscore.R +++ b/tests/testthat/test-ordinalscore.R @@ -130,3 +130,14 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + + +test_that("bad args", { + + expect_snapshot( + recipe(~., data = ex_dat) %>% + step_ordinalscore(starts_with("ord"), convert = NULL) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-other.R b/tests/testthat/test-other.R index c84989bc6..cab736231 100644 --- a/tests/testthat/test-other.R +++ b/tests/testthat/test-other.R @@ -268,7 +268,16 @@ test_that( desc = "if the threshold argument is greather than one then it should be an integer(ish)", code = { expect_snapshot(error = TRUE, - rec %>% step_other(city, zip, threshold = 3.14) + rec %>% step_other(city, zip, threshold = 3.14) %>% prep() + ) + } +) + +test_that( + desc = "bad values of threshold are treated correctly", + code = { + expect_snapshot(error = TRUE, + rec %>% step_other(city, zip, threshold = letters) %>% prep() ) } ) diff --git a/tests/testthat/test-pca.R b/tests/testthat/test-pca.R index 0a2f78fdc..c2d732668 100644 --- a/tests/testthat/test-pca.R +++ b/tests/testthat/test-pca.R @@ -264,7 +264,7 @@ test_that("bake method errors when needed non-standard role columns are missing" pca_extract_trained <- prep(pca_extract, training = biomass_tr, verbose = FALSE) expect_snapshot( - error = TRUE, + error = TRUE, bake(pca_extract_trained, new_data = biomass_te[, c(-3)]) ) }) @@ -374,3 +374,25 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + +test_that("bad args", { + + expect_snapshot( + recipe(~ ., data = mtcars) %>% + step_pca(all_numeric_predictors(), num_comp = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(~ ., data = mtcars) %>% + step_pca(all_numeric_predictors(), prefix = 1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(~ ., data = mtcars) %>% + step_pca(all_numeric_predictors(), threshold = -1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-pls.R b/tests/testthat/test-pls.R index fcbaa46e1..507fa8a75 100644 --- a/tests/testthat/test-pls.R +++ b/tests/testthat/test-pls.R @@ -418,3 +418,27 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + + +test_that("bad args", { + skip_if_not_installed("mixOmics") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_pls(-mpg, outcome = "mpg", num_comp = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_pls(-mpg, outcome = "mpg", prefix = 1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_pls(-mpg, outcome = "mpg", predictor_prop = -1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-poly.R b/tests/testthat/test-poly.R index acbfec541..51b1d165e 100644 --- a/tests/testthat/test-poly.R +++ b/tests/testthat/test-poly.R @@ -226,3 +226,14 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 1L) }) + +test_that("bad args", { + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_poly(disp, degree = 0) %>% + prep(), + error = TRUE + ) + +}) diff --git a/tests/testthat/test-poly_bernstein.R b/tests/testthat/test-poly_bernstein.R index 6dad4defd..9dd294ac9 100644 --- a/tests/testthat/test-poly_bernstein.R +++ b/tests/testthat/test-poly_bernstein.R @@ -212,3 +212,20 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 1L) }) + +test_that("bad args", { + skip_if_not_installed("splines2") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_poly_bernstein(disp, degree = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_poly_bernstein(disp, complete_set = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-range.R b/tests/testthat/test-range.R index 4a303e88c..a2d4cb742 100644 --- a/tests/testthat/test-range.R +++ b/tests/testthat/test-range.R @@ -203,7 +203,7 @@ test_that("bake method errors when needed non-standard role columns are missing" standardized_trained <- prep(standardized, training = biomass_tr, verbose = FALSE) expect_snapshot( - error = TRUE, + error = TRUE, bake(standardized_trained, new_data = biomass_te[, 1:3]) ) }) @@ -252,3 +252,26 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + +test_that("bad args", { + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_range(disp, wt, max = "max") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_range(disp, wt, min = "min") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_range(disp, wt, clipping = "never") %>% + prep(), + error = TRUE + ) + +}) diff --git a/tests/testthat/test-ratio.R b/tests/testthat/test-ratio.R index 5f1191f81..0c6138812 100644 --- a/tests/testthat/test-ratio.R +++ b/tests/testthat/test-ratio.R @@ -251,3 +251,14 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + +test_that("bad args", { + + expect_snapshot( + recipe(~ mpg + disp, mtcars) %>% + step_ratio(mpg, denom = denom_vars(disp), naming = NULL) %>% + prep(), + error = TRUE + ) + +}) diff --git a/tests/testthat/test-regex.R b/tests/testthat/test-regex.R index 5d553a6eb..fdbb8167b 100644 --- a/tests/testthat/test-regex.R +++ b/tests/testthat/test-regex.R @@ -182,3 +182,15 @@ test_that("printing", { expect_snapshot(print(rec)) expect_snapshot(prep(rec)) }) + +test_that("bad args", { + + expect_snapshot( + rec %>% + step_regex(description, pattern = character(0)) %>% + prep(), + error = TRUE + ) + +}) + diff --git a/tests/testthat/test-relevel.R b/tests/testthat/test-relevel.R index 6026661f6..40b1810c7 100644 --- a/tests/testthat/test-relevel.R +++ b/tests/testthat/test-relevel.R @@ -32,6 +32,11 @@ test_that("bad args", { step_relevel(city, ref_level = "missing_level") %>% prep() ) + expect_snapshot( error = TRUE, + rec %>% + step_relevel(city, ref_level = character(0)) %>% + prep() + ) }) test_that("tidy methods", { diff --git a/tests/testthat/test-rename_at.R b/tests/testthat/test-rename_at.R index 63c39125f..1561bf289 100644 --- a/tests/testthat/test-rename_at.R +++ b/tests/testthat/test-rename_at.R @@ -46,6 +46,13 @@ test_that("no input", { prep(training = iris) %>% bake(new_data = NULL, composition = "data.frame") ) + expect_snapshot( + error = TRUE, + iris_rec %>% + step_rename_at(fn = ":=O") %>% + prep(training = iris) %>% + bake(new_data = NULL, composition = "data.frame") + ) }) # Infrastructure --------------------------------------------------------------- diff --git a/tests/testthat/test-sample.R b/tests/testthat/test-sample.R index 8168543c1..e8a35e865 100644 --- a/tests/testthat/test-sample.R +++ b/tests/testthat/test-sample.R @@ -70,13 +70,13 @@ test_that("basic usage", { test_that("bad input", { expect_snapshot(error = TRUE, - iris_rec %>% step_sample(size = -1) + iris_rec %>% step_sample(size = -1) %>% prep() ) expect_snapshot(error = TRUE, - iris_rec %>% step_sample(size = "a") + iris_rec %>% step_sample(size = "a") %>% prep() ) expect_snapshot(error = TRUE, - iris_rec %>% step_sample(replace = "a") + iris_rec %>% step_sample(replace = "a") %>% prep() ) }) @@ -138,7 +138,7 @@ test_that("sample with case weights", { test_that("warn when selectors are provided", { expect_snapshot( - tmp <- recipe(~., data = mtcars) %>% + tmp <- recipe(~., data = mtcars) %>% step_sample(all_predictors()) ) }) diff --git a/tests/testthat/test-scale.R b/tests/testthat/test-scale.R index 599829f29..18d13d571 100644 --- a/tests/testthat/test-scale.R +++ b/tests/testthat/test-scale.R @@ -110,6 +110,11 @@ test_that("na_rm argument works for step_scale", { tidy(rec_na_rm, 1)$value, unname(exp_na_rm) ) + expect_snapshot( + rec_no_na_rm <- recipe(~., data = mtcars_na) %>% + step_scale(all_predictors(), na_rm = "FALSE") %>% + prep(), + error = TRUE) }) test_that("warns on zv",{ diff --git a/tests/testthat/test-spatialsign.R b/tests/testthat/test-spatialsign.R index 4cd3444e7..ba8415c47 100644 --- a/tests/testthat/test-spatialsign.R +++ b/tests/testthat/test-spatialsign.R @@ -22,6 +22,13 @@ test_that("spatial sign", { x <- t(apply(x, 1, function(x) x / sqrt(sum(x^2)))) expect_equal(sp_sign_pred, x) + + expect_snapshot( + rec %>% + step_spatialsign(carbon, hydrogen, na_rm = 12) %>% + prep(), + error = TRUE + ) }) test_that("Missing values", { @@ -101,7 +108,7 @@ test_that("bake method errors when needed non-standard role columns are missing" sp_sign_trained <- prep(sp_sign, training = biomass, verbose = FALSE) expect_snapshot( - error = TRUE, + error = TRUE, bake(sp_sign_trained, new_data = biomass[,c(-3)]) ) }) diff --git a/tests/testthat/test-spline_b.R b/tests/testthat/test-spline_b.R index 98bf8f3dd..68a2e62f2 100644 --- a/tests/testthat/test-spline_b.R +++ b/tests/testthat/test-spline_b.R @@ -87,7 +87,7 @@ test_that("errors if degree > deg_free (#1170)", { step_spline_b(mpg, degree = 3, deg_free = 3, complete_set = FALSE) %>% prep() ) - + expect_snapshot( error = TRUE, recipe(~., data = mtcars) %>% @@ -255,3 +255,26 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + +test_that("bad args", { + skip_if_not_installed("splines2") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_b(disp, degree = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_b(disp, deg_free = "a") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_b(disp, complete_set = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-spline_convex.R b/tests/testthat/test-spline_convex.R index 5380a9bcf..60f199f1d 100644 --- a/tests/testthat/test-spline_convex.R +++ b/tests/testthat/test-spline_convex.R @@ -87,7 +87,7 @@ test_that("errors if degree > deg_free (#1170)", { step_spline_convex(mpg, degree = 3, deg_free = 3, complete_set = FALSE) %>% prep() ) - + expect_snapshot( error = TRUE, recipe(~., data = mtcars) %>% @@ -255,3 +255,20 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + +test_that("bad args", { + skip_if_not_installed("splines2") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_convex(disp, degree = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_convex(disp, complete_set = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-spline_monotone.R b/tests/testthat/test-spline_monotone.R index ad3902272..f1d428170 100644 --- a/tests/testthat/test-spline_monotone.R +++ b/tests/testthat/test-spline_monotone.R @@ -87,7 +87,7 @@ test_that("errors if degree > deg_free (#1170)", { step_spline_monotone(mpg, degree = 3, deg_free = 3, complete_set = FALSE) %>% prep() ) - + expect_snapshot( error = TRUE, recipe(~., data = mtcars) %>% @@ -255,3 +255,26 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + +test_that("bad args", { + skip_if_not_installed("splines2") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_monotone(disp, degree = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_monotone(disp, deg_free = "a") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_monotone(disp, complete_set = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-spline_natural.R b/tests/testthat/test-spline_natural.R index aaa0444ab..b6b16cc03 100644 --- a/tests/testthat/test-spline_natural.R +++ b/tests/testthat/test-spline_natural.R @@ -225,3 +225,20 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 1L) }) + +test_that("bad args", { + skip_if_not_installed("splines2") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_natural(disp, deg_free = "a") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_natural(disp, complete_set = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-spline_nonnegative.R b/tests/testthat/test-spline_nonnegative.R index 95f39692d..a80281cb5 100644 --- a/tests/testthat/test-spline_nonnegative.R +++ b/tests/testthat/test-spline_nonnegative.R @@ -87,7 +87,7 @@ test_that("errors if degree > deg_free (#1170)", { step_spline_nonnegative(mpg, degree = 3, deg_free = 3, complete_set = FALSE) %>% prep() ) - + expect_snapshot( error = TRUE, recipe(~., data = mtcars) %>% @@ -255,3 +255,26 @@ test_that("tunable is setup to work with extract_parameter_set_dials", { expect_s3_class(params, "parameters") expect_identical(nrow(params), 2L) }) + +test_that("bad args", { + skip_if_not_installed("splines2") + + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_nonnegative(disp, degree = -1) %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_nonnegative(disp, deg_free = "a") %>% + prep(), + error = TRUE + ) + expect_snapshot( + recipe(mpg ~ ., data = mtcars) %>% + step_spline_nonnegative(disp, complete_set = 1) %>% + prep(), + error = TRUE + ) +}) diff --git a/tests/testthat/test-string2factor.R b/tests/testthat/test-string2factor.R index 5f81d000e..203641d10 100644 --- a/tests/testthat/test-string2factor.R +++ b/tests/testthat/test-string2factor.R @@ -43,7 +43,7 @@ test_that("bad args", { ) expect_snapshot(error = TRUE, rec %>% - step_string2factor(n, ordered = "yes") %>% + step_string2factor(w, x, ordered = "yes") %>% prep(ex_dat) ) }) diff --git a/tests/testthat/test-unknown.R b/tests/testthat/test-unknown.R index be1e8d90d..9d4cfb557 100644 --- a/tests/testthat/test-unknown.R +++ b/tests/testthat/test-unknown.R @@ -66,6 +66,10 @@ test_that("bad args", { step_unknown(city, new_level = "FAIR_OAKS") %>% prep() ) + expect_snapshot(error = TRUE, + recipe(~., data = sacr_tr) %>% + step_unknown(city, new_level = 2) %>% prep() + ) }) test_that("tidy methods", { diff --git a/tests/testthat/test-window.R b/tests/testthat/test-window.R index 59b76929c..13cf9ebf9 100644 --- a/tests/testthat/test-window.R +++ b/tests/testthat/test-window.R @@ -13,36 +13,42 @@ sim_dat$fac <- sample(letters[1:3], size = n, replace = TRUE) rec <- recipe(~., data = sim_dat) test_that("error checks", { + skip_if_not_installed("RcppRoll") + expect_snapshot(error = TRUE, - rec %>% step_window(y1, size = 6) + rec %>% step_window(y1, size = 6) %>% prep() ) expect_snapshot(error = TRUE, - rec %>% step_window(y1, size = NA) + rec %>% step_window(y1, size = NA) %>% prep() ) expect_snapshot( error = TRUE, - rec %>% step_window(y1, size = NULL) + rec %>% step_window(y1, size = NULL) %>% prep() ) expect_snapshot(error = TRUE, rec %>% step_window(y1, statistic = "average") ) expect_snapshot(error = TRUE, - rec %>% step_window(y1, size = 1) + rec %>% step_window(y1, size = 1) %>% prep() ) expect_snapshot(error = TRUE, - rec %>% step_window(y1, size = 2) + rec %>% step_window(y1, size = 2) %>% prep() ) expect_snapshot(error = TRUE, - rec %>% step_window(y1, size = -1) + rec %>% step_window(y1, size = -1) %>% prep() ) expect_snapshot( - rec %>% step_window(y1, size = pi) + rec %>% step_window(y1, size = 3 + .Machine$double.eps) %>% prep() + ) + expect_snapshot( + rec %>% step_window(y1, size = 3 + 2 * .Machine$double.eps) %>% prep(), + error = TRUE ) expect_snapshot(error = TRUE, prep(rec %>% step_window(fac), training = sim_dat) ) expect_snapshot(error = TRUE, - prep(rec %>% step_window(y1, size = 1000L), training = sim_dat) + prep(rec %>% step_window(y1, size = 1000L), training = sim_dat) %>% prep() ) bad_names <- rec %>% step_window(starts_with("y"), names = "only_one_name") @@ -194,7 +200,7 @@ test_that("empty selection prep/bake is a no-op", { test_that("empty selection tidy method works", { rec <- recipe(mpg ~ ., mtcars) - rec <- step_window(rec) + rec <- step_window(rec, size = 3L) expect <- tibble( terms = character(),