diff --git a/DESCRIPTION b/DESCRIPTION index e1ccc3a..9d01126 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,6 +20,7 @@ Depends: R (>= 3.6), recipes (>= 1.1.0.9000) Imports: + cli, glue, dplyr (>= 1.1.0), generics (>= 0.1.0), diff --git a/R/collapse_stringdist.R b/R/collapse_stringdist.R index a2ee8e6..eb58b57 100644 --- a/R/collapse_stringdist.R +++ b/R/collapse_stringdist.R @@ -79,7 +79,7 @@ step_collapse_stringdist <- skip = FALSE, id = rand_id("collapse_stringdist")) { if (is.null(distance)) { - rlang::abort("`distance` argument must be set.") + cli::cli_abort("The {.arg distance} argument must be set.") } add_step( diff --git a/R/discretize_cart.R b/R/discretize_cart.R index 04cc02f..a0d7748 100644 --- a/R/discretize_cart.R +++ b/R/discretize_cart.R @@ -104,7 +104,9 @@ step_discretize_cart <- recipes_pkg_check(required_pkgs.step_discretize_cart()) if (is.null(outcome)) { - rlang::abort("`outcome` should select at least one column.") + cli::cli_abort( + "The {.arg outcome} argument should select at least one column." + ) } add_step( @@ -167,24 +169,20 @@ cart_binning <- function(predictor, term, outcome, cost_complexity, tree_depth, if (inherits(cart_mdl, "try-error")) { err <- conditionMessage(attr(cart_mdl, "condition")) - msg <- - glue( - "`step_discretize_cart()` failed to create a tree with error for ", - "predictor '{term}', which will not be binned. The error: {err}" - ) - rlang::warn(msg) + cli::cli_warn( + "step_discretize_cart() failed to create a tree for predictor {.var {term}}, + which will not be binned. The error: {err}" + ) return(numeric(0)) } if (any(names(cart_mdl) == "splits")) { cart_split <- sort(unique(cart_mdl$splits[, "index"])) } else { - msg <- - glue( - "`step_discretize_cart()` failed to find any meaningful splits for ", - "predictor '{term}', which will not be binned." - ) - rlang::warn(msg) + cli::cli_warn( + "{.fn step_discretize_cart} failed to find any meaningful splits for + predictor {.val {term}}, which will not be binned." + ) cart_split <- numeric(0) } cart_split diff --git a/R/discretize_xgb.R b/R/discretize_xgb.R index e1fe0f6..4e5a40b 100644 --- a/R/discretize_xgb.R +++ b/R/discretize_xgb.R @@ -117,7 +117,7 @@ step_discretize_xgb <- skip = FALSE, id = rand_id("discretize_xgb")) { if (is.null(outcome)) { - rlang::abort("`outcome` should select at least one column.") + cli::cli_abort("{.arg outcome} should select at least one column.") } recipes_pkg_check(required_pkgs.step_discretize_xgb()) @@ -300,10 +300,10 @@ xgb_binning <- function(df, outcome, predictor, sample_val, learn_rate, nthread = 1 ) } else { - rlang::abort( - paste0( - "Outcome variable only has less than 2 levels. ", - "Doesn't conform to regresion or classification task." + cli::cli_abort( + c( + "Outcome variable only has less than 2 levels.", + "i" = "Doesn't conform to regresion or classification task." ), call = call ) @@ -330,12 +330,10 @@ xgb_binning <- function(df, outcome, predictor, sample_val, learn_rate, if (inherits(xgb_mdl, "try-error")) { err <- conditionMessage(attr(xgb_mdl, "condition")) - msg <- - glue( - "`step_discretize_xgb()` failed to create a tree with error for ", - "predictor '{predictor}', which was not binned. The error: {err}" - ) - rlang::warn(msg) + cli::cli_warn( + "Failed to create {.fn step_discretize_xgb} tree for predictor + {.val {predictor}}, which was not binned. The error: {err}" + ) return(numeric(0)) } @@ -356,20 +354,21 @@ xgb_binning <- function(df, outcome, predictor, sample_val, learn_rate, if (inherits(xgb_tree, "try-error")) { err <- conditionMessage(attr(xgb_tree, "condition")) if (grepl("Non-tree model detected", err)) { - msg <- glue( - "`step_discretize_xgb()` failed for predictor '{predictor}'. ", - "This could be because the data have no trend or because ", - "the learning rate is too low (current value: {learn_rate}). ", - "The predictor was not binned." + cli::cli_warn( + c( + "{.fn step_discretize_xgb} failed for predictor {.val {predictor}}.", + "i" = "This could be because the data have no trend or because + the learning rate is too low (current value: {learn_rate}).", + "i" = "The predictor was not binned." + ) ) } else { - msg <- glue( - "`step_discretize_xgb()` failed to create a tree with error for ", - "predictor '{predictor}', which was not binned. The error: {err}" + cli::cli_warn( + "step_discretize_xgb() failed to create a tree with error for predictor + {.val {predictor}}, which was not binned. The error: {err}" ) } - rlang::warn(msg) return(numeric(0)) } @@ -409,10 +408,10 @@ prep.step_discretize_xgb <- function(x, training, info = NULL, ...) { test_size <- sum(complete.cases(training)) * x$sample_val if (floor(test_size) < 2) { - rlang::abort( - glue( + cli::cli_abort( + c( "Too few observations in the early stopping validation set.", - "Consider increasing the `sample_val` parameter." + "i" = "Consider increasing the {.arg sample_val} parameter." ) ) } @@ -424,11 +423,11 @@ prep.step_discretize_xgb <- function(x, training, info = NULL, ...) { too_few <- num_unique < 20 if (any(too_few)) { predictors <- paste0("'", col_names[too_few], "'", collapse = ", ") - rlang::warn( - glue( - "More than 20 unique training set values are required. ", - "Predictors {predictors} were not processed; ", - "their original values will be used." + cli::cli_warn( + c( + "More than 20 unique training set values are required.", + "i" = "Predictors {predictors} were not processed; + their original values will be used." ) ) col_names <- col_names[!too_few] diff --git a/R/embed.R b/R/embed.R index 495d366..f01f0b5 100644 --- a/R/embed.R +++ b/R/embed.R @@ -159,7 +159,7 @@ step_embed <- is_tf_available() if (is.null(outcome)) { - rlang::abort("Please list a variable in `outcome`") + cli::cli_abort("Please list a variable in {.arg outcome}.") } add_step( recipe, @@ -261,10 +261,10 @@ prep.step_embed <- function(x, training, info = NULL, ...) { is_tf_2 <- function() { if (!is_tf_available()) { - rlang::abort( + cli::cli_abort( c( "tensorflow could now be found.", - "Please run `tensorflow::install_tensorflow()` to install." + "i" = "Please run {.code tensorflow::install_tensorflow()} to install." ) ) } @@ -491,13 +491,13 @@ embed_control <- function(loss = "mse", verbose = 0, callbacks = NULL) { if (batch_size < 1) { - rlang::abort("`batch_size` should be a positive integer") + cli::cli_abort("{.arg batch_size} should be a positive integer.") } if (epochs < 1) { - rlang::abort("`epochs` should be a positive integer") + cli::cli_abort("{.arg epochs} should be a positive integer.") } if (validation_split < 0 || validation_split > 1) { - rlang::abort("`validation_split` should be on [0, 1)") + cli::cli_abort("{.arg validation_split} should be on [0, 1).") } list( loss = loss, metrics = metrics, optimizer = optimizer, epochs = epochs, @@ -518,11 +518,9 @@ tf_options_check <- function(opt) { ) if (length(setdiff(exp_names, names(opt))) > 0) { - rlang::abort( - paste0( - "The following options are missing from the `options`: ", - paste0(setdiff(exp_names, names(opt)), collapse = ",") - ) + cli::cli_abort( + "The options {.code {setdiff(exp_names, names(opt))}} are missing from + {.arg options}." ) } opt @@ -530,7 +528,7 @@ tf_options_check <- function(opt) { class2ind <- function(x) { if (!is.factor(x)) { - rlang::abort("'x' should be a factor") + cli::cli_abort("{.arg x} should be a factor.") } y <- model.matrix(~ x - 1) colnames(y) <- gsub("^x", "", colnames(y)) diff --git a/R/lencode_bayes.R b/R/lencode_bayes.R index b1b91f4..7aad3e9 100644 --- a/R/lencode_bayes.R +++ b/R/lencode_bayes.R @@ -117,7 +117,7 @@ step_lencode_bayes <- skip = FALSE, id = rand_id("lencode_bayes")) { if (is.null(outcome)) { - rlang::abort("Please list a variable in `outcome`") + cli::cli_abort("Please list a variable in {.code outcome}.") } add_step( recipe, diff --git a/R/lencode_glm.R b/R/lencode_glm.R index 2a82ba7..c6bfabc 100644 --- a/R/lencode_glm.R +++ b/R/lencode_glm.R @@ -89,7 +89,7 @@ step_lencode_glm <- skip = FALSE, id = rand_id("lencode_glm")) { if (is.null(outcome)) { - rlang::abort("Please list a variable in `outcome`") + cli::cli_abort("Please list a variable in {.arg outcome}.") } add_step( recipe, diff --git a/R/lencode_mixed.R b/R/lencode_mixed.R index 697f5a2..49bba89 100644 --- a/R/lencode_mixed.R +++ b/R/lencode_mixed.R @@ -103,7 +103,7 @@ step_lencode_mixed <- skip = FALSE, id = rand_id("lencode_mixed")) { if (is.null(outcome)) { - rlang::abort("Please list a variable in `outcome`") + cli::cli_abort("Please list a variable in {.arg outcome}.") } add_step( recipe, @@ -153,10 +153,9 @@ prep.step_lencode_mixed <- function(x, training, info = NULL, ...) { y_name <- recipes_eval_select(x$outcome, training, info) if (is.factor(training[[y_name]])) { if (length(levels(training[[y_name]])) > 2) { - rlang::abort(glue( - "Mixed effects methods here are only implemented for ", - "two-class problems." - )) + cli::cli_abort( + "Mixed effects methods here are only implemented for two-class problems." + ) } } res <- diff --git a/R/umap.R b/R/umap.R index ae21b86..7986176 100644 --- a/R/umap.R +++ b/R/umap.R @@ -142,7 +142,7 @@ step_umap <- seed <- as.integer(seed) } if (length(seed) != 2) { - rlang::abort("Two integers are required for `seed`.") + cli::cli_abort("Two integers are required for {.arg seed}.") } add_step( diff --git a/R/woe.R b/R/woe.R index e0d8733..d2a4e7e 100644 --- a/R/woe.R +++ b/R/woe.R @@ -156,7 +156,7 @@ step_woe <- function(recipe, skip = FALSE, id = rand_id("woe")) { if (missing(outcome)) { - rlang::abort('argument "outcome" is missing, with no default') + cli::cli_abort("The {.arg outcome} argument is missing, with no default.") } add_step( @@ -233,12 +233,13 @@ woe_table <- function(predictor, } if (length(outcome_original_labels) != 2) { - rlang::abort(sprintf( - "'outcome' must have exactly 2 categories (has %s)", - length(outcome_original_labels) - ), call = call) + cli::cli_abort( + "{.arg outcome} must have exactly 2 categories + (has {length(outcome_original_labels)}).", + call = call + ) } - + if (is.factor(predictor)) { predictor <- as.character(predictor) } @@ -356,26 +357,26 @@ dictionary <- function(.data, outcome, ..., Laplace = 1e-6) { #' @export add_woe <- function(.data, outcome, ..., dictionary = NULL, prefix = "woe") { if (missing(.data)) { - rlang::abort('argument ".data" is missing, with no default') + cli::cli_abort("The {.arg .data} argument is missing, with no default.") } if (missing(outcome)) { - rlang::abort('argument "outcome" is missing, with no default') + cli::cli_abort("Argument {.arg outcome} is missing, with no default.") } if (!is.character(outcome)) { - rlang::abort("'outcome' should be a single character value.") + cli::cli_abort("{.arg outcome} should be a single character value.") } if (is.null(dictionary)) { dictionary <- dictionary(.data, outcome, ...) } else { if (is.null(dictionary$variable)) { - rlang::abort('column "variable" is missing in dictionary.') + cli::cli_abort('Column {.field variable} is missing in dictionary.') } if (is.null(dictionary$predictor)) { - rlang::abort('column "predictor" is missing in dictionary.') + cli::cli_abort('The column {.code predictor} is missing in the dictionary.') } if (is.null(dictionary$woe)) { - rlang::abort('column "woe" is missing in dictionary.') + cli::cli_abort('Column {.field woe} is missing in dictionary.') } } @@ -447,12 +448,10 @@ prep.step_woe <- function(x, training, info = NULL, ...) { if (any(n_count$low_n > 0)) { flagged <- n_count$variable[n_count$low_n > 0] - flagged <- paste0("'", unique(flagged), "'", collapse = ", ") - msg <- glue( - "Some columns used by `step_woe()` have categories with ", - "less than 10 values: {flagged}" + cli::cli_warn( + "Some columns used by {.fn step_woe} have categories with fewer than 10 + values: {.val {unique(flagged)}}" ) - rlang::warn(msg) } } else { x$dictionary <- tibble::tibble() diff --git a/tests/testthat/_snaps/discretize_cart.md b/tests/testthat/_snaps/discretize_cart.md index 67f961b..8125247 100644 --- a/tests/testthat/_snaps/discretize_cart.md +++ b/tests/testthat/_snaps/discretize_cart.md @@ -5,7 +5,7 @@ cost_complexity = 0.01, tree_depth = 5, min_n = 10) Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'x', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "x", which will not be binned. # low-level binning for regression @@ -14,7 +14,7 @@ cost_complexity = 0.01, tree_depth = 5, min_n = 10) Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'potato', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "potato", which will not be binned. # step function for classification @@ -23,7 +23,7 @@ all_predictors(), outcome = "class") %>% prep() Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'z', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "z", which will not be binned. # step function for regression @@ -32,7 +32,7 @@ all_predictors(), outcome = "y") %>% prep() Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'z', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "z", which will not be binned. # bad args @@ -51,7 +51,7 @@ cart_rec <- prep(cart_rec) Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'z', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "z", which will not be binned. # case weights step functions @@ -60,7 +60,7 @@ all_predictors(), outcome = "class") %>% prep() Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'z', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "z", which will not be binned. --- @@ -94,7 +94,7 @@ rec_trained <- prep(rec, training = sim_tr_cls, verbose = FALSE) Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'z', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "z", which will not be binned. --- @@ -161,7 +161,7 @@ prep(rec) Condition Warning: - `step_discretize_cart()` failed to find any meaningful splits for predictor 'z', which will not be binned. + `step_discretize_cart()` failed to find any meaningful splits for predictor "z", which will not be binned. Message -- Recipe ---------------------------------------------------------------------- diff --git a/tests/testthat/_snaps/discretize_xgb.md b/tests/testthat/_snaps/discretize_xgb.md index 8aa8444..fc3bd5c 100644 --- a/tests/testthat/_snaps/discretize_xgb.md +++ b/tests/testthat/_snaps/discretize_xgb.md @@ -189,7 +189,8 @@ Condition Error in `step_discretize_xgb()`: Caused by error in `prep()`: - ! Too few observations in the early stopping validation set.Consider increasing the `sample_val` parameter. + ! Too few observations in the early stopping validation set. + i Consider increasing the `sample_val` parameter. --- @@ -199,7 +200,8 @@ outcome = "Status") %>% prep(retain = TRUE) Condition Warning: - More than 20 unique training set values are required. Predictors 'Time' were not processed; their original values will be used. + More than 20 unique training set values are required. + i Predictors 'Time' were not processed; their original values will be used. Message -- Recipe ---------------------------------------------------------------------- @@ -261,7 +263,8 @@ Condition Error in `step_discretize_xgb()`: Caused by error in `prep()`: - ! Too few observations in the early stopping validation set.Consider increasing the `sample_val` parameter. + ! Too few observations in the early stopping validation set. + i Consider increasing the `sample_val` parameter. # xgb_binning() errors if only one class in outcome @@ -270,7 +273,8 @@ learn_rate = 0.3, num_breaks = 10, tree_depth = 1, min_n = 5) Condition Error: - ! Outcome variable only has less than 2 levels. Doesn't conform to regresion or classification task. + ! Outcome variable only has less than 2 levels. + i Doesn't conform to regresion or classification task. # case weights step_discretize_xgb diff --git a/tests/testthat/_snaps/woe.md b/tests/testthat/_snaps/woe.md index 2a52af8..c607a81 100644 --- a/tests/testthat/_snaps/woe.md +++ b/tests/testthat/_snaps/woe.md @@ -4,7 +4,7 @@ embed:::woe_table(rep(c(0, 1), 20), rep(letters[1:4], 5)) Condition Error in `embed:::woe_table()`: - ! 'outcome' must have exactly 2 categories (has 4) + ! `outcome` must have exactly 2 categories (has 4). # woe_table accepts only outcome with 2 distinct categories @@ -12,7 +12,7 @@ embed:::woe_table(rep(letters[1:3], 10), rep(c(0, 1, 2), 10)) Condition Error in `embed:::woe_table()`: - ! 'outcome' must have exactly 2 categories (has 3) + ! `outcome` must have exactly 2 categories (has 3). --- @@ -20,7 +20,7 @@ embed:::woe_table(rep(letters[1:3], 10), rep(c(0), 30)) Condition Error in `embed:::woe_table()`: - ! 'outcome' must have exactly 2 categories (has 1) + ! `outcome` must have exactly 2 categories (has 1). --- @@ -28,7 +28,7 @@ embed:::woe_table(df$x2, df$x1) Condition Error in `embed:::woe_table()`: - ! 'outcome' must have exactly 2 categories (has 3) + ! `outcome` must have exactly 2 categories (has 3). # add_woe accepts only outcome with 2 distinct categories @@ -36,7 +36,7 @@ dictionary(df %>% filter(y %in% "B"), "y") Condition Error in `dictionary()`: - ! 'outcome' must have exactly 2 categories (has 1) + ! `outcome` must have exactly 2 categories (has 1). # add_woe do not accept dictionary with unexpected layout @@ -44,7 +44,7 @@ add_woe(df, outcome = "y", x1, dictionary = iris) Condition Error in `add_woe()`: - ! column "variable" is missing in dictionary. + ! Column variable is missing in dictionary. --- @@ -52,7 +52,7 @@ add_woe(df, outcome = "y", x1, dictionary = iris %>% mutate(variable = 1)) Condition Error in `add_woe()`: - ! column "predictor" is missing in dictionary. + ! The column `predictor` is missing in the dictionary. # step_woe @@ -60,7 +60,7 @@ woe_models <- prep(rec, training = credit_tr) Condition Warning: - Some columns used by `step_woe()` have categories with less than 10 values: 'Home', 'Job' + Some columns used by `step_woe()` have categories with fewer than 10 values: "Home" and "Job" --- @@ -70,7 +70,7 @@ oper 1 step woe [training] Condition Warning: - Some columns used by `step_woe()` have categories with less than 10 values: 'Home', 'Job' + Some columns used by `step_woe()` have categories with fewer than 10 values: "Home" and "Job" Output The retained training set is ~ 0.14 Mb in memory. @@ -107,7 +107,7 @@ Condition Error in `step_woe()`: Caused by error in `dictionary()`: - ! 'outcome' must have exactly 2 categories (has 3) + ! `outcome` must have exactly 2 categories (has 3). # bake method errors when needed non-standard role columns are missing @@ -187,7 +187,7 @@ prep(rec) Condition Warning: - Some columns used by `step_woe()` have categories with less than 10 values: 'Home', 'Job' + Some columns used by `step_woe()` have categories with fewer than 10 values: "Home" and "Job" Message -- Recipe ----------------------------------------------------------------------