Skip to content

Commit

Permalink
change rlang abort to cli_abort
Browse files Browse the repository at this point in the history
fixes tidymodels#1140

Co-authored-by: Sarah Wright <[email protected]>
  • Loading branch information
RobLBaker and wright13 committed Aug 15, 2024
1 parent aa788b8 commit 6373d59
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
35 changes: 16 additions & 19 deletions R/glmnet-engines.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,13 @@ format_glmnet_multinom_class <- function(pred, penalty, lvl, n_obs) {
pen <- rlang::eval_tidy(x$args$penalty)

if (length(pen) != 1) {
rlang::abort(c(
"For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).",
glue::glue("There are {length(pen)} values for `penalty`."),
"To try multiple values for total regularization, use the tune package.",
"To predict multiple penalties, use `multi_predict()`"
))
cli::cli_abort(c(
"x" = "For the glmnet engine, {.var penalty} must be a single number
(or a value of {.fn tune}).",
"!" = "There are {length(pen)} value{?s} for {.var penalty}.",
"i" = "To try multiple values for total regularization, use the
{.pkg tune} package.",
"i" = "To predict multiple penalties, use {.fn multi_predict}."))
}
}

Expand All @@ -385,23 +386,19 @@ format_glmnet_multinom_class <- function(pred, penalty, lvl, n_obs) {
# when using `predict()`, allow for a single lambda
if (!multi) {
if (length(penalty) != 1) {
rlang::abort(
glue::glue(
"`penalty` should be a single numeric value. `multi_predict()` ",
"can be used to get multiple predictions per row of data."
)
)
cli::cli_abort(c(
"{.var penalty} should be a single numeric value. {.fn multi_predict}
can be used to get multiple predictions per row of data."
))
}
}

if (length(object$fit$lambda) == 1 && penalty != object$fit$lambda) {
rlang::abort(
glue::glue(
"The glmnet model was fit with a single penalty value of ",
"{object$fit$lambda}. Predicting with a value of {penalty} ",
"will give incorrect results from `glmnet()`."
)
)
cli::cli_abort(c(
"The glmnet model was fit with a single penalty value of
{.var object$fit$lambda}. Predicting with a value of {.envar penalty}
will give incorrect results from `glmnet()`."
))
}

penalty
Expand Down
4 changes: 2 additions & 2 deletions R/logistic_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ translate.logistic_reg <- function(x, engine = x$engine, ...) {
} else if (quo_get_expr(x$args$mixture) == 1) {
arg_vals$type <- 6 ## lasso
} else {
rlang::abort("For the LiblineaR engine, mixture must be 0 or 1.")
cli::cli_abort(c("x" = "For the LiblineaR engine, {.var mixture} must be 0 or 1."))
}
}
x$method$fit$args <- arg_vals
Expand Down Expand Up @@ -153,7 +153,7 @@ check_args.logistic_reg <- function(object, call = rlang::caller_env()) {
call = call
)
}

if ((!is.null(args$penalty)) && args$penalty == 0) {
cli::cli_abort(
"For the {.pkg LiblineaR} engine, {.arg penalty} must be {.code > 0}, \\
Expand Down
10 changes: 5 additions & 5 deletions R/mars.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ multi_predict._earth <-
object$fit$call[[i]] <- eval_tidy(object$fit$call[[i]])
}

msg <-
paste("Please use `keepxy = TRUE` as an option to enable submodel",
"predictions with `earth`.")
msg <- c("x" = "Please use {.code keepxy = TRUE} as an
option to enable submodel predictions with
{.var earth}.")
if (any(names(object$fit$call) == "keepxy")) {
if (!isTRUE(object$fit$call$keepxy))
rlang::abort(msg)
cli::cli_abort(msg)
} else {
rlang::abort(msg)
cli::cli_abort(msg)
}

if (is.null(type)) {
Expand Down
7 changes: 4 additions & 3 deletions R/mlp.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ check_args.mlp <- function(object, call = rlang::caller_env()) {

class2ind <- function (x, drop2nd = FALSE) {
if (!is.factor(x))
rlang::abort("`x` should be a factor")
cli::cli_abort(c("x" = "{.var x} should be a factor."))
y <- model.matrix( ~ x - 1)
colnames(y) <- gsub("^x", "", colnames(y))
attributes(y)$assign <- NULL
Expand Down Expand Up @@ -195,8 +195,9 @@ keras_mlp <-
rlang::arg_match(activation, act_funs,)

if (penalty > 0 & dropout > 0) {
rlang::abort("Please use either dropoput or weight decay.", call. = FALSE)
}
cli::cli_abort(c("x" = "Please use either dropout or weight decay",
call = NULL))
}
if (!is.matrix(x)) {
x <- as.matrix(x)
}
Expand Down

0 comments on commit 6373d59

Please sign in to comment.