diff --git a/NAMESPACE b/NAMESPACE index e3d2995a..9d9739dd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,7 +16,6 @@ S3method(mold,formula) S3method(mold,matrix) S3method(mold,recipe) S3method(obj_print_footer,quantile_pred) -S3method(print,formula_blueprint) S3method(print,hardhat_blueprint) S3method(print,hardhat_model) S3method(refresh_blueprint,default_formula_blueprint) diff --git a/R/blueprint-formula-default.R b/R/blueprint-formula-default.R index f20335ff..63b81ee8 100644 --- a/R/blueprint-formula-default.R +++ b/R/blueprint-formula-default.R @@ -878,7 +878,10 @@ check_levels <- function(x, } if (!all(map_lgl(x, is.character))) { - cli::cli_abort("{.arg {arg}} must only contain character vectors.", call = call) + cli::cli_abort( + "{.arg {arg}} must only contain character vectors.", + call = call + ) } invisible(NULL) @@ -966,11 +969,9 @@ expr_check_no_factorish_in_functions <- function(expr, expr <- as_label(original_expr) message <- c( - paste0( - "Functions involving factors or characters have been detected on the ", - "RHS of `formula`. These are not allowed when `indicators = \"none\"`." - ), - i = "Functions involving factors were detected for {.str {name}} in {.arg {expr}}." + "Functions involving factors or characters have been detected on the + RHS of {.arg formula}. These are not allowed when {.code indicators = \"none\"}.", + i = "Functions involving factors were detected for {.val {name}} in {.arg {expr}}." ) cli::cli_abort(message, call = error_call) @@ -1031,11 +1032,9 @@ expr_check_no_factorish_in_interaction_term <- function(expr, expr <- as_label(expr_original) message <- c( - paste0( - "Interaction terms involving factors or characters have been detected on the ", - "RHS of `formula`. These are not allowed when `indicators = \"none\"`." - ), - i = "Interactions terms involving factors were detected for {.str {name}} in {.arg {expr}}." + "Interaction terms involving factors or characters have been detected on the + RHS of {.arg formula}. These are not allowed when {.code indicators = \"none\"}.", + i = "Interactions terms involving factors were detected for {.val {name}} in {.arg {expr}}." ) cli::cli_abort(message, call = error_call) @@ -1072,7 +1071,7 @@ expr_check_no_interactions <- function(expr, error_call) { expr <- as_label(expr) message <- c( - "Interaction terms can't be specified on the LHS of `formula`.", + "Interaction terms can't be specified on the LHS of {.arg formula}.", i = "The following interaction term was found: {.arg {expr}}." ) diff --git a/R/blueprint.R b/R/blueprint.R index a1872c53..d1933b10 100644 --- a/R/blueprint.R +++ b/R/blueprint.R @@ -289,11 +289,10 @@ check_has_name <- function(x, } } - message <- cli::format_inline( - "{.arg {arg}} must have an element named {.str {name}}." + cli::cli_abort( + "{.arg {arg}} must have an element named {.str {name}}.", + call = call ) - - abort(message, call = call) } # https://github.com/r-lib/rlang/pull/1605 diff --git a/R/case-weights.R b/R/case-weights.R index 762c73ac..3fb80649 100644 --- a/R/case-weights.R +++ b/R/case-weights.R @@ -31,7 +31,7 @@ importance_weights <- function(x) { x <- vec_cast_named(x, to = double(), x_arg = "x") if (any(x < 0, na.rm = TRUE)) { - abort("`x` can't contain negative weights.") + cli::cli_abort("{.arg x} can't contain negative weights.") } new_importance_weights(x) @@ -58,7 +58,7 @@ importance_weights <- function(x) { #' new_importance_weights(c(1.5, 2.3, 10)) new_importance_weights <- function(x = double(), ..., class = character()) { if (!is.double(x)) { - abort("`x` must be a double vector.") + cli::cli_abort("{.arg x} must be a double vector.") } new_case_weights( @@ -153,7 +153,7 @@ frequency_weights <- function(x) { x <- vec_cast_named(x, to = integer(), x_arg = "x") if (any(x < 0L, na.rm = TRUE)) { - abort("`x` can't contain negative weights.") + cli::cli_abort("{.arg x} can't contain negative weights.") } new_frequency_weights(x) @@ -180,7 +180,7 @@ frequency_weights <- function(x) { #' new_frequency_weights(1:5) new_frequency_weights <- function(x = integer(), ..., class = character()) { if (!is.integer(x)) { - abort("`x` must be an integer vector.") + cli::cli_abort("{.arg x} must be an integer vector.") } new_case_weights( @@ -265,7 +265,7 @@ vec_ptype_abbr.hardhat_frequency_weights <- function(x, ...) { #' new_case_weights(1:5, class = "my_weights") new_case_weights <- function(x, ..., class) { if (!is.integer(x) && !is.double(x)) { - abort("`x` must be an integer or double vector.") + cli::cli_abort("{.arg x} must be an integer or double vector.") } new_vctr( diff --git a/R/constructor.R b/R/constructor.R index 8a22aae4..6c6b7870 100644 --- a/R/constructor.R +++ b/R/constructor.R @@ -67,15 +67,15 @@ new_scalar <- function(elems, ..., class = character()) { check_elems <- function(elems) { if (!is.list(elems) || length(elems) == 0) { - abort("`elems` must be a list of length 1 or greater.") + cli::cli_abort("{.arg elems} must be a list of length 1 or greater.") } if (!has_unique_names(elems)) { - abort("`elems` must have unique names.") + cli::cli_abort("{.arg elems} must have unique names.") } if (!identical(names(attributes(elems)), "names")) { - abort("`elems` must have no attributes (apart from names).") + cli::cli_abort("{.arg elems} must have no attributes (apart from names).") } invisible(elems) diff --git a/R/encoding.R b/R/encoding.R index f3587f18..46a57cf3 100644 --- a/R/encoding.R +++ b/R/encoding.R @@ -31,7 +31,7 @@ #' fct_encode_one_hot(factor(sample(letters[1:4], 10, TRUE))) fct_encode_one_hot <- function(x) { if (!is.factor(x)) { - abort("`x` must be a factor.") + cli::cli_abort("{.arg x} must be a factor, not {.obj_type_friendly {x}}.") } row_names <- names(x) @@ -44,7 +44,7 @@ fct_encode_one_hot <- function(x) { x <- unclass(x) if (vec_any_missing(x)) { - abort("`x` can't contain missing values.") + cli::cli_abort("{.arg x} can't contain missing values.") } out <- matrix(0L, nrow = n_rows, ncol = n_cols, dimnames = dim_names) diff --git a/R/forge.R b/R/forge.R index b903711f..1785b83e 100644 --- a/R/forge.R +++ b/R/forge.R @@ -70,7 +70,7 @@ forge <- function(new_data, blueprint, ..., outcomes = FALSE) { #' @export forge.default <- function(new_data, blueprint, ..., outcomes = FALSE) { - glubort("The class of `new_data`, '{class1(new_data)}', is not recognized.") + cli::cli_abort("No {.fn forge} method provided for {.obj_type_friendly {new_data}}.") } #' @export @@ -140,7 +140,5 @@ run_forge.default <- function(blueprint, new_data, ..., outcomes = FALSE) { - class <- class(blueprint)[[1L]] - message <- glue("No `run_forge()` method provided for an object of type <{class}>.") - abort(message) + cli::cli_abort("No {.fn run_forge} method provided for {.obj_type_friendly {blueprint}}.") } diff --git a/R/intercept.R b/R/intercept.R index 35e114ca..56bf6895 100644 --- a/R/intercept.R +++ b/R/intercept.R @@ -22,21 +22,13 @@ #' add_intercept_column(as.matrix(mtcars)) #' @export add_intercept_column <- function(data, name = "(Intercept)") { - ok <- is.data.frame(data) || is.matrix(data) - - if (!ok) { - glubort( - "`data` must be a data.frame or matrix to add an intercept column, ", - "not a '{class1(data)}'." - ) - } - + check_data_frame_or_matrix(data) check_name(name) if (name %in% colnames(data)) { - warn(glue::glue( - "`data` already has a column named '{name}'. ", - "Returning `data` unchanged." + cli::cli_warn(c( + "{.arg data} already has a column named {.val {name}}.", + "i" = "Returning {.arg data} unchanged." )) return(data) diff --git a/R/model-matrix.R b/R/model-matrix.R index fed2e19f..47992f48 100644 --- a/R/model-matrix.R +++ b/R/model-matrix.R @@ -178,11 +178,11 @@ model_matrix_one_hot <- function(terms, data) { #' @keywords internal contr_one_hot <- function(n, contrasts = TRUE, sparse = FALSE) { if (sparse) { - warn("`sparse = TRUE` not implemented for `contr_one_hot()`.") + cli::cli_warn("{.code sparse = TRUE} not implemented for {.fn contr_one_hot}.") } if (!contrasts) { - warn("`contrasts = FALSE` not implemented for `contr_one_hot()`.") + cli::cli_warn("{.code contrasts = FALSE} not implemented for {.fn contr_one_hot}.") } if (is.character(n)) { @@ -192,12 +192,12 @@ contr_one_hot <- function(n, contrasts = TRUE, sparse = FALSE) { n <- as.integer(n) if (length(n) != 1L) { - abort("`n` must have length 1 when an integer is provided.") + cli::cli_abort("{.arg n} must have length 1 when an integer is provided.") } names <- as.character(seq_len(n)) } else { - abort("`n` must be a character vector or an integer of size 1.") + cli::cli_abort("{.arg n} must be a character vector or an integer of size 1.") } out <- diag(n) diff --git a/R/model-offset.R b/R/model-offset.R index f35c08b6..7cfcabde 100644 --- a/R/model-offset.R +++ b/R/model-offset.R @@ -63,9 +63,9 @@ model_offset <- function(terms, data) { if (!is.numeric(.offset_val)) { bad_col <- colnames(data)[.pos] - glubort( - "Column, '{bad_col}', is tagged as an offset, but is not numeric. ", - "All offsets must be numeric." + cli::cli_abort( + "Column {.val {bad_col}} is tagged as an offset and thus must be + numeric, not {.obj_type_friendly { .offset_val }}." ) } diff --git a/R/mold.R b/R/mold.R index 4e2a1729..b770e145 100644 --- a/R/mold.R +++ b/R/mold.R @@ -175,7 +175,5 @@ run_mold <- function(blueprint, ...) { #' @export run_mold.default <- function(blueprint, ...) { - class <- class(blueprint)[[1L]] - message <- glue("No `run_mold()` method provided for an object of type <{class}>.") - abort(message) + cli::cli_abort("No {.fn run_mold} method provided for {.obj_type_friendly {blueprint}}.") } diff --git a/R/print.R b/R/print.R index d0a09f2e..2a479d85 100644 --- a/R/print.R +++ b/R/print.R @@ -9,28 +9,21 @@ format.formula_blueprint <- function(x, ...) "Formula" #' @export print.hardhat_blueprint <- function(x, ...) { - cat_line("{format(x)} blueprint:") - cat_line("\n") - cat_line("# Predictors: {n_blueprint_predictors(x)}") - cat_line(" # Outcomes: {n_blueprint_outcomes(x)}") - cat_line(" Intercept: {x$intercept}") - cat_line("Novel Levels: {x$allow_novel_levels}") - cat_line(" Composition: {x$composition}") - invisible(x) -} + cli::cli_text("{format(x)} blueprint:") -#' @export -print.formula_blueprint <- function(x, ...) { - NextMethod() - cat_line(" Indicators: {x$indicators}") + cli::cli_par() + cli::cli_text("# Predictors: {n_blueprint_predictors(x)}") + cli::cli_text("# Outcomes: {n_blueprint_outcomes(x)}") + cli::cli_text("Intercept: {x$intercept}") + cli::cli_text("Novel Levels: {x$allow_novel_levels}") + cli::cli_text("Composition: {x$composition}") + if (inherits(x, "formula_blueprint")) { + cli::cli_text("Indicators: {x$indicators}") + } + cli::cli_end() invisible(x) } -cat_line <- function(..., .envir = parent.frame()) { - lines <- paste(glue(..., .envir = .envir), "\n") - cat(lines, sep = "") -} - n_blueprint_predictors <- function(x) { ncol(x$ptypes$predictors) %||% 0L } diff --git a/R/quantile-pred.R b/R/quantile-pred.R index fee2e5f6..9695c577 100644 --- a/R/quantile-pred.R +++ b/R/quantile-pred.R @@ -57,7 +57,10 @@ new_quantile_pred <- function(values = list(), quantile_levels = double()) { #' @rdname quantile_pred extract_quantile_levels <- function(x) { if (!inherits(x, "quantile_pred")) { - cli::cli_abort("{.arg x} should have class {.cls quantile_pred}.") + cli::cli_abort( + "{.arg x} must be a {.cls quantile_pred} object, not + {.obj_type_friendly {x}}." + ) } attr(x, "quantile_levels") } @@ -131,12 +134,7 @@ obj_print_footer.quantile_pred <- function(x, digits = 3, ...) { # Checking functions check_quantile_pred_inputs <- function(values, levels, call = caller_env()) { - if (!is.matrix(values)) { - cli::cli_abort( - "{.arg values} must be a {.cls matrix}, not {.obj_type_friendly {values}}.", - call = call - ) - } + check_inherits(values, "matrix", call = call) num_lvls <- length(levels) @@ -168,10 +166,12 @@ check_quantile_levels <- function(levels, call = rlang::caller_env()) { redund <- levels[is_dup] redund <- unique(redund) redund <- signif(redund, digits = 5) - cli::cli_abort(c( - "Quantile levels should be unique.", - i = "The following {cli::qty(length(redund))}value{?s} {?was/were} repeated: - {redund}."), + cli::cli_abort( + c( + "Quantile levels must be unique.", + i = "The following {cli::qty(length(redund))}value{?s} {?was/were} + repeated: {redund}." + ), call = call ) } diff --git a/R/recompose.R b/R/recompose.R index 51e6a8eb..90f0347c 100644 --- a/R/recompose.R +++ b/R/recompose.R @@ -77,7 +77,8 @@ coerce_to_matrix <- function(data, error_call = caller_env()) { message <- c( "{.arg data} must only contain numeric columns.", - i = "These columns aren't numeric: {.str {loc}}." + i = "{cli::qty(length(loc))}{?This/These} column{?s} {?isn't/aren't} + numeric: {.val {loc}}." ) cli::cli_abort(message, call = error_call) diff --git a/R/scream.R b/R/scream.R index ea8f7733..dbf6c22c 100644 --- a/R/scream.R +++ b/R/scream.R @@ -238,13 +238,13 @@ check_novel_levels <- function(x, ptype, column) { } warn_novel_levels <- function(levels, column) { - message <- glue( - "Novel levels found in column '{column}': {glue_quote_collapse(levels)}. ", - "The levels have been removed, and values have been coerced to 'NA'." - ) - - warn( - message, + n_levels <- length(levels) + cli::cli_warn( + c( + "{cli::qty(n_levels)}Novel level{?s} found in column {.val {column}}: {.val {levels}}.", + "i" = "The {cli::qty(n_levels)}level{?s} {?has/have} been removed, + and values have been coerced to {.cls NA}." + ), class = "hardhat_warn_novel_levels", levels = levels, column = column diff --git a/R/spruce.R b/R/spruce.R index 3ac85be8..cbac2bc9 100644 --- a/R/spruce.R +++ b/R/spruce.R @@ -66,9 +66,9 @@ spruce_prob <- function(pred_levels, prob_matrix) { n_col <- ncol(prob_matrix) if (n_levels != n_col) { - glubort( - "The number of levels ({n_levels}) must be - equal to the number of class probability columns ({n_col})." + cli::cli_abort( + "The number of levels ({n_levels}) must be equal to the number + of class probability columns ({n_col})." ) } diff --git a/R/standardize.R b/R/standardize.R index 6e63502b..a2ee1b44 100644 --- a/R/standardize.R +++ b/R/standardize.R @@ -39,7 +39,7 @@ standardize <- function(y) { #' @export standardize.default <- function(y) { - glubort("`y` is of unknown type '{class1(y)}'.") + cli::cli_abort("No {.fn standardize} method provided for {.obj_type_friendly {y}}.") } #' @export @@ -80,7 +80,7 @@ standardize.array <- function(y) { } else if (dims(y) == 2) { standardize.matrix(y) } else { - glubort("3D+ arrays are not supported outcome types.") + cli::cli_abort("3D+ arrays are not supported outcome types.") } } @@ -101,11 +101,12 @@ validate_has_known_outcome_types <- function(y) { if (!all(known)) { not_known <- which(!known) not_known <- colnames(y)[not_known] - not_known <- glue_quote_collapse(not_known) - - glubort( - "Not all columns of `y` are known outcome types. ", - "These columns have unknown types: {not_known}." + cli::cli_abort( + c( + "Not all columns of {.arg y} are known outcome types.", + "i" = "{?This/These} column{?s} {?has/have} {?an/} unknown type{?s}: + {.val {not_known}}." + ) ) } diff --git a/R/table.R b/R/table.R index 2b1778e9..f72f803f 100644 --- a/R/table.R +++ b/R/table.R @@ -103,17 +103,17 @@ weighted_table <- function(..., weights, na_remove = FALSE) { n_args <- length(args) if (n_args == 0L) { - abort("At least one vector must be supplied to `...`.") + cli::cli_abort("At least one vector must be supplied to {.arg ...}.") } if (!all(map_lgl(args, is.factor))) { - abort("All elements of `...` must be factors.") + cli::cli_abort("All elements of {.arg ...} must be factors.") } sizes <- list_sizes(args) size <- sizes[[1L]] if (!all(sizes == size)) { - abort("All elements of `...` must be the same size.") + cli::cli_abort("All elements of {.arg ...} must be the same size.") } weights <- vec_cast(weights, to = double()) diff --git a/R/use.R b/R/use.R index 5b8cd66a..0bbce0d9 100644 --- a/R/use.R +++ b/R/use.R @@ -62,7 +62,7 @@ create_modeling_package <- function(path, check_string(model) if (has_spaces(model)) { - abort("`model` must not contain any spaces.") + cli::cli_abort("{.arg model} must not contain any spaces.") } usethis::create_package(path, fields, open = FALSE) @@ -125,9 +125,9 @@ use_modeling_files_impl <- function(model, prompt_document = TRUE) { check_installed("usethis") check_string(model) - + if (has_spaces(model)) { - abort("`model` must not contain any spaces.") + cli::cli_abort("{.arg model} must not contain any spaces.") } data <- list(model = model) diff --git a/R/util.R b/R/util.R index be8442d5..a491969d 100644 --- a/R/util.R +++ b/R/util.R @@ -1,21 +1,9 @@ -glubort <- function(..., .sep = "", .envir = caller_env(), .call = .envir) { - abort(glue(..., .sep = .sep, .envir = .envir), call = .call) -} - -glue_quote_collapse <- function(x) { - glue::glue_collapse(glue::single_quote(x), sep = ", ") -} - simplify_terms <- function(x) { # This is like stats:::terms.default # but doesn't look at x$terms. - is_terms <- inherits(x, "terms") - - if (!is_terms) { - abort("`x` must be a terms object") - } + check_terms(x) # It removes the environment # (which could be large) @@ -45,8 +33,10 @@ get_all_predictors <- function(formula, data) { extra_predictors <- setdiff(predictors, names(data)) if (length(extra_predictors) > 0) { - extra_predictors <- glue_quote_collapse(extra_predictors) - glubort("The following predictors were not found in `data`: {extra_predictors}.") + cli::cli_abort( + "The following predictor{?s} {?was/were} not found in {.arg data}: + {.val {extra_predictors}}." + ) } predictors @@ -64,13 +54,15 @@ get_all_outcomes <- function(formula, data) { outcomes <- all.vars(outcome_formula) if ("." %in% outcomes) { - abort("The left hand side of the formula cannot contain `.`") + cli::cli_abort("The left-hand side of the formula cannot contain {.code .}.") } extra_outcomes <- setdiff(outcomes, names(data)) if (length(extra_outcomes) > 0) { - extra_outcomes <- glue_quote_collapse(extra_outcomes) - glubort("The following outcomes were not found in `data`: {extra_outcomes}.") + cli::cli_abort( + "The following outcome{?s} {?was/were} not found in {.arg data}: + {.val {extra_outcomes}}." + ) } outcomes @@ -150,10 +142,6 @@ has_unique_column_names <- function(x) { !anyDuplicated(nms) } -class1 <- function(x) { - class(x)[1] -} - # ------------------------------------------------------------------------------ check_data_frame_or_matrix <- function(x, diff --git a/R/validation.R b/R/validation.R index 27390461..15c59975 100644 --- a/R/validation.R +++ b/R/validation.R @@ -39,7 +39,7 @@ validate_outcomes_are_univariate <- function(outcomes) { check <- check_outcomes_are_univariate(outcomes) if (!check$ok) { - glubort( + cli::cli_abort( "The outcome must be univariate, but {check$n_cols} columns were found." ) } @@ -111,21 +111,27 @@ validate_outcomes_are_numeric <- function(outcomes) { check <- check_outcomes_are_numeric(outcomes) if (!check$ok) { - bad_cols <- glue::single_quote(names(check$bad_classes)) - bad_printable_classes <- map(check$bad_classes, glue_quote_collapse) - bad_msg <- glue::glue("{bad_cols}: {bad_printable_classes}") - bad_msg <- glue::glue_collapse(bad_msg, sep = "\n") - - glubort( - "All outcomes must be numeric, but the following are not:", - "\n", - "{bad_msg}" + bad_msg <- style_bad_classes(check$bad_classes) + + cli::cli_abort( + c( + "All outcomes must be numeric.", + "i" = "{cli::qty(length(check$bad_classes))}The following {?is/are} not:", + bad_msg + ) ) } invisible(outcomes) } +style_bad_classes <- function(bad_classes){ + bad_col <- map(names(bad_classes), ~ cli::format_inline("{.val {.x}}")) + bad_class <- map(bad_classes, ~ cli::format_inline("{.cls {.x}}")) + + glue::glue("{bad_col}: {bad_class}") +} + #' @rdname validate_outcomes_are_numeric #' @export check_outcomes_are_numeric <- function(outcomes) { @@ -190,15 +196,14 @@ validate_outcomes_are_factors <- function(outcomes) { check <- check_outcomes_are_factors(outcomes) if (!check$ok) { - bad_cols <- glue::single_quote(names(check$bad_classes)) - bad_printable_classes <- map(check$bad_classes, glue_quote_collapse) - bad_msg <- glue::glue("{bad_cols}: {bad_printable_classes}") - bad_msg <- glue::glue_collapse(bad_msg, sep = "\n") - - glubort( - "All outcomes must be factors, but the following are not:", - "\n", - "{bad_msg}" + bad_msg <- style_bad_classes(check$bad_classes) + + cli::cli_abort( + c( + "All outcomes must be factors.", + "i" = "{cli::qty(length(check$bad_classes))}The following {?is/are} not:", + bad_msg + ) ) } @@ -274,15 +279,17 @@ validate_outcomes_are_binary <- function(outcomes) { check <- check_outcomes_are_binary(outcomes) if (!check$ok) { - bad_cols <- glue::single_quote(check$bad_cols) - bad_msg <- glue::glue("{bad_cols}: {check$num_levels}") - bad_msg <- glue::glue_collapse(bad_msg, sep = "\n") - - glubort( - "The outcome must be binary, ", - "but the following number of levels were found:", - "\n", - "{bad_msg}" + bad_col <- map(check$bad_cols, ~ cli::format_inline("{.val {.x}}")) + + bad_msg <- glue::glue("{bad_col}: {check$num_levels}") + + cli::cli_abort( + c( + "The outcome must be binary.", + "i" = "{cli::qty(length(bad_col))}The following number of levels + {?was/were} found:", + bad_msg + ) ) } @@ -366,15 +373,14 @@ validate_predictors_are_numeric <- function(predictors) { check <- check_predictors_are_numeric(predictors) if (!check$ok) { - bad_cols <- glue::single_quote(names(check$bad_classes)) - bad_printable_classes <- map(check$bad_classes, glue_quote_collapse) - bad_msg <- glue::glue("{bad_cols}: {bad_printable_classes}") - bad_msg <- glue::glue_collapse(bad_msg, sep = "\n") - - glubort( - "All predictors must be numeric, but the following are not:", - "\n", - "{bad_msg}" + bad_msg <- style_bad_classes(check$bad_classes) + + cli::cli_abort( + c( + "All predictors must be numeric.", + "i" = "{cli::qty(length(check$bad_classes))}The following {?is/are} not:", + bad_msg + ) ) } @@ -495,11 +501,9 @@ validate_column_names <- function(data, original_names) { if (!check$ok) { validate_missing_name_isnt_.outcome(check$missing_names) - missing_names <- glue_quote_collapse(check$missing_names) - - message <- glue("The following required columns are missing: {missing_names}.") - - abort(message) + cli::cli_abort( + "The required column{?s} {.val {check$missing_names}} {?is/are} missing." + ) } invisible(data) @@ -508,9 +512,7 @@ validate_column_names <- function(data, original_names) { #' @rdname validate_column_names #' @export check_column_names <- function(data, original_names) { - if (!is.character(original_names)) { - glubort("`original_names` must be a character vector.") - } + check_character(original_names) new_names <- colnames(data) @@ -531,15 +533,14 @@ validate_missing_name_isnt_.outcome <- function(missing_names) { not_ok <- ".outcome" %in% missing_names if (not_ok) { - missing_names <- glue_quote_collapse(missing_names) - - glubort( - "The following required columns are missing: {missing_names}. - - (This indicates that `mold()` was called with a vector for `y`. ", - "When this is the case, and the outcome columns are requested ", - "in `forge()`, `new_data` must include a column with the automatically ", - "generated name, '.outcome', containing the outcome.)" + cli::cli_abort( + c( + "The following required columns are missing: {.val {missing_names}}.", + "i" = "This indicates that {.fn mold} was called with a vector for {.arg y}.", + "i" = "When this is the case, and the outcome columns are requested in + {.fn forge}, {.arg new_data} must include a column with the + automatically generated name, {.code .outcome}, containing the outcome." + ) ) } @@ -615,9 +616,9 @@ validate_prediction_size <- function(pred, new_data) { check <- check_prediction_size(pred, new_data) if (!check$ok) { - glubort( - "The size of `new_data` ({check$size_new_data}) must match the ", - "size of `pred` ({check$size_pred})." + cli::cli_abort( + "The size of {.arg new_data} ({check$size_new_data}) must match the + size of {.arg pred} ({check$size_pred})." ) } @@ -692,11 +693,13 @@ validate_no_formula_duplication <- function(formula, original = FALSE) { check <- check_no_formula_duplication(formula, original) if (!check$ok) { - duplicates <- glue_quote_collapse(check$duplicates) - - glubort( - "The following terms are duplicated on the left and right hand side ", - "of the `formula`: {duplicates}." + cli::cli_abort( + c( + "Terms must not be duplicated on the left- and right-hand side of the + {.arg formula}.", + "i" = "The following duplicated term{?s} {?was/were} found: + {.val {check$duplicates}}" + ) ) } diff --git a/tests/testthat/_snaps/constructor.md b/tests/testthat/_snaps/constructor.md index eb98eff3..4707063d 100644 --- a/tests/testthat/_snaps/constructor.md +++ b/tests/testthat/_snaps/constructor.md @@ -3,17 +3,17 @@ Code new_model() Output - + named list() Code new_model(class = "custom_class") Output - + named list() Code new_model(x = 4, y = "hi", class = "custom_class") Output - + $x [1] 4 diff --git a/tests/testthat/_snaps/encoding.md b/tests/testthat/_snaps/encoding.md index 2c525592..744b534c 100644 --- a/tests/testthat/_snaps/encoding.md +++ b/tests/testthat/_snaps/encoding.md @@ -12,5 +12,5 @@ fct_encode_one_hot(1) Condition Error in `fct_encode_one_hot()`: - ! `x` must be a factor. + ! `x` must be a factor, not a number. diff --git a/tests/testthat/_snaps/forge-formula.md b/tests/testthat/_snaps/forge-formula.md index 695a321d..118ea60f 100644 --- a/tests/testthat/_snaps/forge-formula.md +++ b/tests/testthat/_snaps/forge-formula.md @@ -4,7 +4,7 @@ forge(example_train2, x1$blueprint, outcomes = TRUE) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'fac_1'. + ! The required column "fac_1" is missing. --- @@ -12,7 +12,7 @@ forge(example_train2, x2$blueprint, outcomes = TRUE) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'fac_1'. + ! The required column "fac_1" is missing. # new_data can only be a data frame / matrix @@ -20,7 +20,7 @@ forge("hi", x1$blueprint) Condition Error in `forge()`: - ! The class of `new_data`, 'character', is not recognized. + ! No `forge()` method provided for a string. --- @@ -28,7 +28,7 @@ forge("hi", x2$blueprint) Condition Error in `forge()`: - ! The class of `new_data`, 'character', is not recognized. + ! No `forge()` method provided for a string. # missing predictor columns fail appropriately @@ -36,7 +36,7 @@ forge(example_train[, 1, drop = FALSE], x1$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'num_2'. + ! The required column "num_2" is missing. --- @@ -44,7 +44,7 @@ forge(example_train[, 1, drop = FALSE], x2$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'num_2'. + ! The required column "num_2" is missing. --- @@ -52,7 +52,7 @@ forge(example_train[, 3, drop = FALSE], x1$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'num_1', 'num_2'. + ! The required columns "num_1" and "num_2" are missing. --- @@ -60,7 +60,7 @@ forge(example_train[, 3, drop = FALSE], x2$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'num_1', 'num_2'. + ! The required columns "num_1" and "num_2" are missing. # novel predictor levels are caught @@ -68,7 +68,8 @@ xx1 <- forge(new, x1$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -76,7 +77,17 @@ xx2 <- forge(new, x2$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . + +--- + + Code + xx3 <- forge(new_multiple, x3$blueprint) + Condition + Warning: + Novel levels found in column "f": "e" and "f". + i The levels have been removed, and values have been coerced to . # novel predictor levels can be ignored @@ -94,7 +105,8 @@ xx1 <- forge(new, x1$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -102,7 +114,8 @@ xx2 <- forge(new, x2$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . # novel levels are ignored correctly when the new column is a character @@ -120,7 +133,8 @@ xx1 <- forge(new, x1$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -128,7 +142,8 @@ xx2 <- forge(new, x2$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . # novel outcome levels are always caught, even if `allow_novel_levels = TRUE` @@ -136,7 +151,8 @@ xx1 <- forge(new, x1$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -144,7 +160,8 @@ xx2 <- forge(new, x2$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . # missing predictor levels are restored silently @@ -173,7 +190,16 @@ Condition Error in `recompose()`: ! `data` must only contain numeric columns. - i These columns aren't numeric: "f". + i This column isn't numeric: "f". + +--- + + Code + mold(y ~ f + f_2, dat_2f, blueprint = bp2) + Condition + Error in `recompose()`: + ! `data` must only contain numeric columns. + i These columns aren't numeric: "f" and "f_2". --- @@ -181,7 +207,8 @@ xx <- forge(new, x1$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . # can be both missing levels and have new levels that get ignored @@ -199,5 +226,6 @@ out <- forge(df2, x$blueprint) Condition Warning: - Novel levels found in column 'x': 'd'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "x": "d". + i The level has been removed, and values have been coerced to . diff --git a/tests/testthat/_snaps/forge-recipe.md b/tests/testthat/_snaps/forge-recipe.md index fde9c1ca..659600fd 100644 --- a/tests/testthat/_snaps/forge-recipe.md +++ b/tests/testthat/_snaps/forge-recipe.md @@ -4,7 +4,7 @@ forge(iris2, x1$blueprint, outcomes = TRUE) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Species'. + ! The required column "Species" is missing. --- @@ -12,7 +12,7 @@ forge(iris2, x2$blueprint, outcomes = TRUE) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Species'. + ! The required column "Species" is missing. # missing predictor columns fail appropriately @@ -20,7 +20,7 @@ forge(iris[, 1, drop = FALSE], x$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Width'. + ! The required column "Sepal.Width" is missing. --- @@ -28,7 +28,7 @@ forge(iris[, 3, drop = FALSE], x$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Length', 'Sepal.Width'. + ! The required columns "Sepal.Length" and "Sepal.Width" are missing. # novel predictor levels are caught @@ -36,7 +36,8 @@ xx1 <- forge(new, x1$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -44,7 +45,8 @@ xx2 <- forge(new, x2$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . Warning: ! There are new levels in `f`: NA. i Consider using step_unknown() (`?recipes::step_unknown()`) before `step_dummy()` to handle missing values. @@ -70,7 +72,8 @@ xx1 <- forge(new, x1$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -78,7 +81,8 @@ xx2 <- forge(new, x2$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . # `forge()` will error if required non standard roles are missing @@ -86,7 +90,7 @@ forge(iris, x$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Width'. + ! The required column "Sepal.Width" is missing. # `NA` roles are treated as extra roles that are required at `forge()` time @@ -94,7 +98,7 @@ forge(iris, x$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Petal.Length'. + ! The required column "Petal.Length" is missing. # `forge()` is compatible with hardhat 0.2.0 molded blueprints with a basic recipe @@ -102,7 +106,7 @@ forge(new_data, blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'x'. + ! The required column "x" is missing. # `forge()` is compatible with hardhat 0.2.0 molded blueprints with a recipe with a nonstandard role @@ -110,5 +114,5 @@ forge(new_data, blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'id'. + ! The required column "id" is missing. diff --git a/tests/testthat/_snaps/forge-xy.md b/tests/testthat/_snaps/forge-xy.md index 1a6ed11a..6e4ea288 100644 --- a/tests/testthat/_snaps/forge-xy.md +++ b/tests/testthat/_snaps/forge-xy.md @@ -4,9 +4,9 @@ forge(iris, x1$blueprint, outcomes = TRUE) Condition Error in `validate_missing_name_isnt_.outcome()`: - ! The following required columns are missing: '.outcome'. - - (This indicates that `mold()` was called with a vector for `y`. When this is the case, and the outcome columns are requested in `forge()`, `new_data` must include a column with the automatically generated name, '.outcome', containing the outcome.) + ! The following required columns are missing: ".outcome". + i This indicates that `mold()` was called with a vector for `y`. + i When this is the case, and the outcome columns are requested in `forge()`, `new_data` must include a column with the automatically generated name, `.outcome`, containing the outcome. --- @@ -14,9 +14,9 @@ forge(iris, x2$blueprint, outcomes = TRUE) Condition Error in `validate_missing_name_isnt_.outcome()`: - ! The following required columns are missing: '.outcome'. - - (This indicates that `mold()` was called with a vector for `y`. When this is the case, and the outcome columns are requested in `forge()`, `new_data` must include a column with the automatically generated name, '.outcome', containing the outcome.) + ! The following required columns are missing: ".outcome". + i This indicates that `mold()` was called with a vector for `y`. + i When this is the case, and the outcome columns are requested in `forge()`, `new_data` must include a column with the automatically generated name, `.outcome`, containing the outcome. # new_data can only be a data frame / matrix @@ -24,7 +24,7 @@ forge("hi", x1$blueprint) Condition Error in `forge()`: - ! The class of `new_data`, 'character', is not recognized. + ! No `forge()` method provided for a string. --- @@ -32,7 +32,7 @@ forge("hi", x2$blueprint) Condition Error in `forge()`: - ! The class of `new_data`, 'character', is not recognized. + ! No `forge()` method provided for a string. --- @@ -40,7 +40,7 @@ forge("hi", x3$blueprint) Condition Error in `forge()`: - ! The class of `new_data`, 'character', is not recognized. + ! No `forge()` method provided for a string. # missing predictor columns fail appropriately @@ -48,7 +48,7 @@ forge(iris[, 1, drop = FALSE], x1$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Width'. + ! The required column "Sepal.Width" is missing. --- @@ -56,7 +56,7 @@ forge(iris[, 1, drop = FALSE], x2$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Width'. + ! The required column "Sepal.Width" is missing. --- @@ -64,7 +64,7 @@ forge(iris[, 3, drop = FALSE], x1$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Length', 'Sepal.Width'. + ! The required columns "Sepal.Length" and "Sepal.Width" are missing. --- @@ -72,7 +72,7 @@ forge(iris[, 3, drop = FALSE], x2$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Length', 'Sepal.Width'. + ! The required columns "Sepal.Length" and "Sepal.Width" are missing. # novel predictor levels are caught @@ -80,7 +80,8 @@ xx <- forge(new, x$blueprint) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . # novel predictor levels can be ignored @@ -93,7 +94,8 @@ xx1 <- forge(new, x1$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . --- @@ -101,5 +103,6 @@ xx2 <- forge(new, x2$blueprint, outcomes = TRUE) Condition Warning: - Novel levels found in column 'f': 'e'. The levels have been removed, and values have been coerced to 'NA'. + Novel level found in column "f": "e". + i The level has been removed, and values have been coerced to . diff --git a/tests/testthat/_snaps/forge.md b/tests/testthat/_snaps/forge.md index 621dcfb4..4e2f2e78 100644 --- a/tests/testthat/_snaps/forge.md +++ b/tests/testthat/_snaps/forge.md @@ -4,5 +4,5 @@ run_forge(1) Condition Error in `run_forge()`: - ! No `run_forge()` method provided for an object of type . + ! No `run_forge()` method provided for a number. diff --git a/tests/testthat/_snaps/intercept.md b/tests/testthat/_snaps/intercept.md index 9a1a8dcb..3cc992d7 100644 --- a/tests/testthat/_snaps/intercept.md +++ b/tests/testthat/_snaps/intercept.md @@ -4,7 +4,8 @@ xx <- add_intercept_column(x) Condition Warning: - `data` already has a column named '(Intercept)'. Returning `data` unchanged. + `data` already has a column named "(Intercept)". + i Returning `data` unchanged. # name can only be a single character @@ -22,3 +23,11 @@ Error in `add_intercept_column()`: ! `name` must be a valid name, not the number 1. +# data has to be a data frame or matrix + + Code + add_intercept_column(1) + Condition + Error in `add_intercept_column()`: + ! `data` must be a data frame or a matrix, not the number 1. + diff --git a/tests/testthat/_snaps/levels.md b/tests/testthat/_snaps/levels.md index e8207e5e..444eff85 100644 --- a/tests/testthat/_snaps/levels.md +++ b/tests/testthat/_snaps/levels.md @@ -4,5 +4,5 @@ get_outcome_levels("a") Condition Error in `standardize()`: - ! `y` is of unknown type 'character'. + ! No `standardize()` method provided for a string. diff --git a/tests/testthat/_snaps/model-offset.md b/tests/testthat/_snaps/model-offset.md index cd93def8..7a412737 100644 --- a/tests/testthat/_snaps/model-offset.md +++ b/tests/testthat/_snaps/model-offset.md @@ -4,7 +4,7 @@ mold(~ Sepal.Width + offset(Species), iris) Condition Error in `model_offset()`: - ! Column, 'offset(Species)', is tagged as an offset, but is not numeric. All offsets must be numeric. + ! Column "offset(Species)" is tagged as an offset and thus must be numeric, not a object. # offset columns are stored as predictors @@ -12,5 +12,5 @@ forge(iris2, x$blueprint) Condition Error in `validate_column_names()`: - ! The following required columns are missing: 'Sepal.Length'. + ! The required column "Sepal.Length" is missing. diff --git a/tests/testthat/_snaps/mold-formula.md b/tests/testthat/_snaps/mold-formula.md index 4dd7d4f3..0ae47b6d 100644 --- a/tests/testthat/_snaps/mold-formula.md +++ b/tests/testthat/_snaps/mold-formula.md @@ -127,7 +127,7 @@ mold(fac_1 ~ y + z, example_train) Condition Error in `get_all_predictors()`: - ! The following predictors were not found in `data`: 'y', 'z'. + ! The following predictors were not found in `data`: "y" and "z". --- @@ -135,7 +135,7 @@ mold(fac_1 ~ y + z, example_train, blueprint = bp) Condition Error in `get_all_predictors()`: - ! The following predictors were not found in `data`: 'y', 'z'. + ! The following predictors were not found in `data`: "y" and "z". --- @@ -143,7 +143,7 @@ mold(y + z ~ fac_1, example_train) Condition Error in `get_all_outcomes()`: - ! The following outcomes were not found in `data`: 'y', 'z'. + ! The following outcomes were not found in `data`: "y" and "z". --- @@ -151,7 +151,7 @@ mold(y + z ~ fac_1, example_train, blueprint = bp) Condition Error in `get_all_outcomes()`: - ! The following outcomes were not found in `data`: 'y', 'z'. + ! The following outcomes were not found in `data`: "y" and "z". # global environment variables cannot be used @@ -160,7 +160,7 @@ mold(fac_1 ~ y, example_train) Condition Error in `get_all_predictors()`: - ! The following predictors were not found in `data`: 'y'. + ! The following predictor was not found in `data`: "y". # cannot manually remove intercept in the formula itself @@ -318,7 +318,7 @@ mold(. ~ fac_1, example_train) Condition Error in `get_all_outcomes()`: - ! The left hand side of the formula cannot contain `.` + ! The left-hand side of the formula cannot contain `.`. --- @@ -326,7 +326,7 @@ mold(. ~ fac_1, example_train, blueprint = bp) Condition Error in `get_all_outcomes()`: - ! The left hand side of the formula cannot contain `.` + ! The left-hand side of the formula cannot contain `.`. # `blueprint` is validated diff --git a/tests/testthat/_snaps/mold.md b/tests/testthat/_snaps/mold.md index 40613108..1dc5ace4 100644 --- a/tests/testthat/_snaps/mold.md +++ b/tests/testthat/_snaps/mold.md @@ -4,5 +4,5 @@ run_mold(1) Condition Error in `run_mold()`: - ! No `run_mold()` method provided for an object of type . + ! No `run_mold()` method provided for a number. diff --git a/tests/testthat/_snaps/print.md b/tests/testthat/_snaps/print.md index 8af41660..aeedddc3 100644 --- a/tests/testthat/_snaps/print.md +++ b/tests/testthat/_snaps/print.md @@ -2,50 +2,50 @@ Code mold(Species ~ Sepal.Length, iris)$blueprint - Output - Formula blueprint: - - # Predictors: 1 - # Outcomes: 1 - Intercept: FALSE - Novel Levels: FALSE - Composition: tibble - Indicators: traditional + Message + Formula blueprint: + # Predictors: 1 + # Outcomes: 1 + Intercept: FALSE + Novel Levels: FALSE + Composition: tibble + Indicators: traditional + Code mold(~Sepal.Length, iris)$blueprint - Output - Formula blueprint: - - # Predictors: 1 - # Outcomes: 0 - Intercept: FALSE - Novel Levels: FALSE - Composition: tibble - Indicators: traditional + Message + Formula blueprint: + # Predictors: 1 + # Outcomes: 0 + Intercept: FALSE + Novel Levels: FALSE + Composition: tibble + Indicators: traditional + # print - default Code mold(iris[, c("Sepal.Length"), drop = FALSE], iris$Species)$blueprint - Output - XY blueprint: - - # Predictors: 1 - # Outcomes: 1 - Intercept: FALSE - Novel Levels: FALSE - Composition: tibble + Message + XY blueprint: + # Predictors: 1 + # Outcomes: 1 + Intercept: FALSE + Novel Levels: FALSE + Composition: tibble + # print - recipe Code mold(recipes::recipe(Species ~ Sepal.Length, iris), iris)$blueprint - Output - Recipe blueprint: - - # Predictors: 1 - # Outcomes: 1 - Intercept: FALSE - Novel Levels: FALSE - Composition: tibble + Message + Recipe blueprint: + # Predictors: 1 + # Outcomes: 1 + Intercept: FALSE + Novel Levels: FALSE + Composition: tibble + diff --git a/tests/testthat/_snaps/quantile-pred.md b/tests/testthat/_snaps/quantile-pred.md index 2a706946..cb2b45e7 100644 --- a/tests/testthat/_snaps/quantile-pred.md +++ b/tests/testthat/_snaps/quantile-pred.md @@ -44,7 +44,7 @@ quantile_pred(matrix(1:20, 5), quantile_levels = c(0.7, 0.7, 0.7)) Condition Error in `quantile_pred()`: - ! Quantile levels should be unique. + ! Quantile levels must be unique. i The following value was repeated: 0.7. --- @@ -53,7 +53,7 @@ quantile_pred(matrix(1:20, 5), quantile_levels = c(rep(0.7, 2), rep(0.8, 3))) Condition Error in `quantile_pred()`: - ! Quantile levels should be unique. + ! Quantile levels must be unique. i The following values were repeated: 0.7 and 0.8. --- @@ -70,7 +70,7 @@ extract_quantile_levels(1:10) Condition Error in `extract_quantile_levels()`: - ! `x` should have class . + ! `x` must be a object, not an integer vector. # quantile_pred formatting diff --git a/tests/testthat/_snaps/spruce.md b/tests/testthat/_snaps/spruce.md index cb6cc552..7daa524f 100644 --- a/tests/testthat/_snaps/spruce.md +++ b/tests/testthat/_snaps/spruce.md @@ -60,8 +60,7 @@ spruce_prob(c("a", "b"), matrix(1, ncol = 3)) Condition Error in `spruce_prob()`: - ! The number of levels (2) must be - equal to the number of class probability columns (3). + ! The number of levels (2) must be equal to the number of class probability columns (3). --- @@ -69,8 +68,7 @@ spruce_prob(c("a"), matrix(1, ncol = 2)) Condition Error in `spruce_prob()`: - ! The number of levels (1) must be - equal to the number of class probability columns (2). + ! The number of levels (1) must be equal to the number of class probability columns (2). # spruce multiple helpers check input type diff --git a/tests/testthat/_snaps/standardize.md b/tests/testthat/_snaps/standardize.md index 23967906..9414fef4 100644 --- a/tests/testthat/_snaps/standardize.md +++ b/tests/testthat/_snaps/standardize.md @@ -44,7 +44,17 @@ standardize(bad2) Condition Error in `validate_has_known_outcome_types()`: - ! Not all columns of `y` are known outcome types. These columns have unknown types: 'x'. + ! Not all columns of `y` are known outcome types. + i This column has an unknown type: "x". + +--- + + Code + standardize(bad3) + Condition + Error in `validate_has_known_outcome_types()`: + ! Not all columns of `y` are known outcome types. + i These columns have unknown types: "x" and "y". # standardize - unknown @@ -52,7 +62,7 @@ standardize("hi") Condition Error in `standardize()`: - ! `y` is of unknown type 'character'. + ! No `standardize()` method provided for a string. --- @@ -60,5 +70,5 @@ standardize(Sys.time()) Condition Error in `standardize()`: - ! `y` is of unknown type 'POSIXct'. + ! No `standardize()` method provided for a object. diff --git a/tests/testthat/_snaps/validation.md b/tests/testthat/_snaps/validation.md index 4c397ab9..afb2fb5c 100644 --- a/tests/testthat/_snaps/validation.md +++ b/tests/testthat/_snaps/validation.md @@ -12,8 +12,9 @@ validate_outcomes_are_numeric(iris) Condition Error in `validate_outcomes_are_numeric()`: - ! All outcomes must be numeric, but the following are not: - 'Species': 'factor' + ! All outcomes must be numeric. + i The following is not: + "Species": --- @@ -21,9 +22,10 @@ validate_outcomes_are_numeric(x) Condition Error in `validate_outcomes_are_numeric()`: - ! All outcomes must be numeric, but the following are not: - 'x': 'POSIXct', 'POSIXt' - 'y': 'factor' + ! All outcomes must be numeric. + i The following are not: + "x": + "y": # validate_no_formula_duplication() @@ -31,7 +33,8 @@ validate_no_formula_duplication(y ~ y) Condition Error in `validate_no_formula_duplication()`: - ! The following terms are duplicated on the left and right hand side of the `formula`: 'y'. + ! Terms must not be duplicated on the left- and right-hand side of the `formula`. + i The following duplicated term was found: "y" --- @@ -39,7 +42,8 @@ validate_no_formula_duplication(y ~ log(y), original = TRUE) Condition Error in `validate_no_formula_duplication()`: - ! The following terms are duplicated on the left and right hand side of the `formula`: 'y'. + ! Terms must not be duplicated on the left- and right-hand side of the `formula`. + i The following duplicated term was found: "y" --- @@ -47,7 +51,8 @@ validate_no_formula_duplication(y + x ~ y + x) Condition Error in `validate_no_formula_duplication()`: - ! The following terms are duplicated on the left and right hand side of the `formula`: 'y', 'x'. + ! Terms must not be duplicated on the left- and right-hand side of the `formula`. + i The following duplicated terms were found: "y" and "x" --- @@ -55,7 +60,8 @@ validate_no_formula_duplication(y ~ . + y) Condition Error in `validate_no_formula_duplication()`: - ! The following terms are duplicated on the left and right hand side of the `formula`: 'y'. + ! Terms must not be duplicated on the left- and right-hand side of the `formula`. + i The following duplicated term was found: "y" --- @@ -63,7 +69,8 @@ validate_no_formula_duplication(y ~ offset(y), original = TRUE) Condition Error in `validate_no_formula_duplication()`: - ! The following terms are duplicated on the left and right hand side of the `formula`: 'y'. + ! Terms must not be duplicated on the left- and right-hand side of the `formula`. + i The following duplicated term was found: "y" # validate_outcomes_are_factors() @@ -71,9 +78,10 @@ validate_outcomes_are_factors(x) Condition Error in `validate_outcomes_are_factors()`: - ! All outcomes must be factors, but the following are not: - 'x': 'POSIXct', 'POSIXt' - 'y': 'character' + ! All outcomes must be factors. + i The following are not: + "x": + "y": # validate_outcomes_are_binary() @@ -81,12 +89,13 @@ validate_outcomes_are_binary(iris) Condition Error in `validate_outcomes_are_binary()`: - ! The outcome must be binary, but the following number of levels were found: - 'Sepal.Length': 0 - 'Sepal.Width': 0 - 'Petal.Length': 0 - 'Petal.Width': 0 - 'Species': 3 + ! The outcome must be binary. + i The following number of levels were found: + "Sepal.Length": 0 + "Sepal.Width": 0 + "Petal.Length": 0 + "Petal.Width": 0 + "Species": 3 # validate_predictors_are_numeric() @@ -94,8 +103,9 @@ validate_predictors_are_numeric(iris) Condition Error in `validate_predictors_are_numeric()`: - ! All predictors must be numeric, but the following are not: - 'Species': 'factor' + ! All predictors must be numeric. + i The following is not: + "Species": --- @@ -103,9 +113,10 @@ validate_predictors_are_numeric(x) Condition Error in `validate_predictors_are_numeric()`: - ! All predictors must be numeric, but the following are not: - 'x': 'POSIXct', 'POSIXt' - 'y': 'factor' + ! All predictors must be numeric. + i The following are not: + "x": + "y": # validate_prediction_size() diff --git a/tests/testthat/test-forge-formula.R b/tests/testthat/test-forge-formula.R index dfeebff0..f37544c6 100644 --- a/tests/testthat/test-forge-formula.R +++ b/tests/testthat/test-forge-formula.R @@ -259,6 +259,14 @@ test_that("novel predictor levels are caught", { unname(xx2$predictors[5, 1]), NA_real_ ) + + new_multiple <- data.frame( + y = 1:6, + f = factor(letters[1:6]) + ) + x3 <- mold(y ~ f, dat) + + expect_snapshot(xx3 <- forge(new_multiple, x3$blueprint)) }) test_that("novel predictor levels can be ignored", { @@ -700,6 +708,16 @@ test_that("can be both missing levels and have new levels", { mold(y ~ f, dat, blueprint = bp2) }) + dat_2f <- data.frame( + y = 1:4, + f = factor(letters[1:4]), + f_2 = factor(letters[5:8]) + ) + + expect_snapshot(error = TRUE, { + mold(y ~ f + f_2, dat_2f, blueprint = bp2) + }) + # Warning for the extra level expect_snapshot(xx <- forge(new, x1$blueprint)) diff --git a/tests/testthat/test-intercept.R b/tests/testthat/test-intercept.R index 79d42820..9bf8ebd5 100644 --- a/tests/testthat/test-intercept.R +++ b/tests/testthat/test-intercept.R @@ -37,3 +37,9 @@ test_that("name can only be a single character", { add_intercept_column(mtcars, name = 1) }) }) + +test_that("data has to be a data frame or matrix", { + expect_snapshot(error = TRUE, { + add_intercept_column(1) + }) +}) diff --git a/tests/testthat/test-standardize.R b/tests/testthat/test-standardize.R index bb6aff38..e85469a2 100644 --- a/tests/testthat/test-standardize.R +++ b/tests/testthat/test-standardize.R @@ -73,6 +73,10 @@ test_that("standardize - data.frame", { expect_snapshot(error = TRUE, standardize(bad2)) + bad3 <- data.frame(x = "a", y = "b", stringsAsFactors = FALSE) + + expect_snapshot(error = TRUE, standardize(bad3)) + good <- bad colnames(good) <- c("a", "b")