Skip to content

change rlang abort to cli_abort #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions R/glmnet-engines.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +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, {.arg penalty} must be a single number
(or a value of {.fn tune}).",
"!" = "There are {length(pen)} value{?s} for {.arg 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 +387,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(
"{.arg penalty} should be a single numeric value.",
"i" = "{.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
{.arg object$fit$lambda}. Predicting with a value of {.arg 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("For the LiblineaR engine, {.arg 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 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
4 changes: 2 additions & 2 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" = "{.arg 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,7 +195,7 @@ 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("Please use either dropout or weight decay.", call = NULL)
}
if (!is.matrix(x)) {
x <- as.matrix(x)
Expand Down
56 changes: 28 additions & 28 deletions tests/testthat/_snaps/translate.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,10 @@
translate_args(basic %>% set_engine("glmnet"))
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

---

Expand Down Expand Up @@ -555,10 +555,10 @@
translate_args(mixture %>% set_engine("glmnet"))
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

---

Expand Down Expand Up @@ -688,10 +688,10 @@
translate_args(basic %>% set_engine("glmnet"))
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

---

Expand Down Expand Up @@ -827,10 +827,10 @@
translate_args(mixture %>% set_engine("glmnet"))
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

---

Expand Down Expand Up @@ -968,10 +968,10 @@
translate_args(mixture_v %>% set_engine("glmnet"))
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

---

Expand Down Expand Up @@ -1334,10 +1334,10 @@
translate_args(basic %>% set_engine("glmnet"))
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

---

Expand Down Expand Up @@ -1552,10 +1552,10 @@
basic_incomplete %>% translate_args()
Condition
Error in `.check_glmnet_penalty_fit()`:
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
* There are 0 values for `penalty`.
* To try multiple values for total regularization, use the tune package.
* To predict multiple penalties, use `multi_predict()`
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
! There are 0 values for `penalty`.
i To try multiple values for total regularization, use the tune package.
i To predict multiple penalties, use `multi_predict()`.

# arguments (rand_forest)

Expand Down
Loading