Skip to content

Commit

Permalink
switch to cli functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Oct 29, 2024
1 parent 5a92baa commit 7dfb8da
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 102 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Depends:
R (>= 3.6),
recipes (>= 1.1.0.9000)
Imports:
cli,
glue,
dplyr (>= 1.1.0),
generics (>= 0.1.0),
Expand Down
2 changes: 1 addition & 1 deletion R/collapse_stringdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Check warning on line 82 in R/collapse_stringdist.R

View check run for this annotation

Codecov / codecov/patch

R/collapse_stringdist.R#L82

Added line #L82 was not covered by tests
}

add_step(
Expand Down
24 changes: 11 additions & 13 deletions R/discretize_cart.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)

Check warning on line 109 in R/discretize_cart.R

View check run for this annotation

Codecov / codecov/patch

R/discretize_cart.R#L107-L109

Added lines #L107 - L109 were not covered by tests
}

add_step(
Expand Down Expand Up @@ -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}"
)

Check warning on line 175 in R/discretize_cart.R

View check run for this annotation

Codecov / codecov/patch

R/discretize_cart.R#L172-L175

Added lines #L172 - L175 were not covered by tests
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
Expand Down
55 changes: 27 additions & 28 deletions R/discretize_xgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Check warning on line 120 in R/discretize_xgb.R

View check run for this annotation

Codecov / codecov/patch

R/discretize_xgb.R#L120

Added line #L120 was not covered by tests
}

recipes_pkg_check(required_pkgs.step_discretize_xgb())
Expand Down Expand Up @@ -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
)
Expand All @@ -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}"
)

Check warning on line 336 in R/discretize_xgb.R

View check run for this annotation

Codecov / codecov/patch

R/discretize_xgb.R#L333-L336

Added lines #L333 - L336 were not covered by tests
return(numeric(0))
}

Expand All @@ -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."
)

Check warning on line 363 in R/discretize_xgb.R

View check run for this annotation

Codecov / codecov/patch

R/discretize_xgb.R#L357-L363

Added lines #L357 - L363 were not covered by tests
)
} 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}"

Check warning on line 368 in R/discretize_xgb.R

View check run for this annotation

Codecov / codecov/patch

R/discretize_xgb.R#L366-L368

Added lines #L366 - L368 were not covered by tests
)
}

rlang::warn(msg)
return(numeric(0))
}

Expand Down Expand Up @@ -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."
)
)
}
Expand All @@ -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]
Expand Down
22 changes: 10 additions & 12 deletions R/embed.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")

Check warning on line 162 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L162

Added line #L162 was not covered by tests
}
add_step(
recipe,
Expand Down Expand Up @@ -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(

Check warning on line 264 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L264

Added line #L264 was not covered by tests
c(
"tensorflow could now be found.",
"Please run `tensorflow::install_tensorflow()` to install."
"i" = "Please run {.code tensorflow::install_tensorflow()} to install."

Check warning on line 267 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L267

Added line #L267 was not covered by tests
)
)
}
Expand Down Expand Up @@ -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.")

Check warning on line 494 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L494

Added line #L494 was not covered by tests
}
if (epochs < 1) {
rlang::abort("`epochs` should be a positive integer")
cli::cli_abort("{.arg epochs} should be a positive integer.")

Check warning on line 497 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L497

Added line #L497 was not covered by tests
}
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).")

Check warning on line 500 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L500

Added line #L500 was not covered by tests
}
list(
loss = loss, metrics = metrics, optimizer = optimizer, epochs = epochs,
Expand All @@ -518,19 +518,17 @@ 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}."

Check warning on line 523 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L521-L523

Added lines #L521 - L523 were not covered by tests
)
}
opt
}

class2ind <- function(x) {
if (!is.factor(x)) {
rlang::abort("'x' should be a factor")
cli::cli_abort("{.arg x} should be a factor.")

Check warning on line 531 in R/embed.R

View check run for this annotation

Codecov / codecov/patch

R/embed.R#L531

Added line #L531 was not covered by tests
}
y <- model.matrix(~ x - 1)
colnames(y) <- gsub("^x", "", colnames(y))
Expand Down
2 changes: 1 addition & 1 deletion R/lencode_bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")

Check warning on line 120 in R/lencode_bayes.R

View check run for this annotation

Codecov / codecov/patch

R/lencode_bayes.R#L120

Added line #L120 was not covered by tests
}
add_step(
recipe,
Expand Down
2 changes: 1 addition & 1 deletion R/lencode_glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")

Check warning on line 92 in R/lencode_glm.R

View check run for this annotation

Codecov / codecov/patch

R/lencode_glm.R#L92

Added line #L92 was not covered by tests
}
add_step(
recipe,
Expand Down
9 changes: 4 additions & 5 deletions R/lencode_mixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")

Check warning on line 106 in R/lencode_mixed.R

View check run for this annotation

Codecov / codecov/patch

R/lencode_mixed.R#L106

Added line #L106 was not covered by tests
}
add_step(
recipe,
Expand Down Expand Up @@ -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."
)

Check warning on line 158 in R/lencode_mixed.R

View check run for this annotation

Codecov / codecov/patch

R/lencode_mixed.R#L156-L158

Added lines #L156 - L158 were not covered by tests
}
}
res <-
Expand Down
2 changes: 1 addition & 1 deletion R/umap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")

Check warning on line 145 in R/umap.R

View check run for this annotation

Codecov / codecov/patch

R/umap.R#L145

Added line #L145 was not covered by tests
}

add_step(
Expand Down
33 changes: 16 additions & 17 deletions R/woe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Check warning on line 159 in R/woe.R

View check run for this annotation

Codecov / codecov/patch

R/woe.R#L159

Added line #L159 was not covered by tests
}

add_step(
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.")

Check warning on line 360 in R/woe.R

View check run for this annotation

Codecov / codecov/patch

R/woe.R#L360

Added line #L360 was not covered by tests
}
if (missing(outcome)) {
rlang::abort('argument "outcome" is missing, with no default')
cli::cli_abort("Argument {.arg outcome} is missing, with no default.")

Check warning on line 363 in R/woe.R

View check run for this annotation

Codecov / codecov/patch

R/woe.R#L363

Added line #L363 was not covered by tests
}
if (!is.character(outcome)) {
rlang::abort("'outcome' should be a single character value.")
cli::cli_abort("{.arg outcome} should be a single character value.")

Check warning on line 366 in R/woe.R

View check run for this annotation

Codecov / codecov/patch

R/woe.R#L366

Added line #L366 was not covered by tests
}

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.')

Check warning on line 379 in R/woe.R

View check run for this annotation

Codecov / codecov/patch

R/woe.R#L379

Added line #L379 was not covered by tests
}
}

Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit 7dfb8da

Please sign in to comment.