Skip to content

Commit c7dc696

Browse files
RobLBakerwright13simonpcouch
authored
transition rlang::abort() to cli::cli_abort() (#1153)
Co-authored-by: Sarah Wright <sarah_wright@nps.gov> Co-authored-by: Simon P. Couch <simonpatrickcouch@gmail.com>
1 parent 348f9fa commit c7dc696

File tree

5 files changed

+53
-55
lines changed

5 files changed

+53
-55
lines changed

R/glmnet-engines.R

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,13 @@ format_glmnet_multinom_class <- function(pred, penalty, lvl, n_obs) {
361361
pen <- rlang::eval_tidy(x$args$penalty)
362362

363363
if (length(pen) != 1) {
364-
rlang::abort(c(
365-
"For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).",
366-
glue::glue("There are {length(pen)} values for `penalty`."),
367-
"To try multiple values for total regularization, use the tune package.",
368-
"To predict multiple penalties, use `multi_predict()`"
364+
cli::cli_abort(c(
365+
"x" = "For the glmnet engine, {.arg penalty} must be a single number
366+
(or a value of {.fn tune}).",
367+
"!" = "There are {length(pen)} value{?s} for {.arg penalty}.",
368+
"i" = "To try multiple values for total regularization, use the
369+
{.pkg tune} package.",
370+
"i" = "To predict multiple penalties, use {.fn multi_predict}."
369371
))
370372
}
371373
}
@@ -385,23 +387,19 @@ format_glmnet_multinom_class <- function(pred, penalty, lvl, n_obs) {
385387
# when using `predict()`, allow for a single lambda
386388
if (!multi) {
387389
if (length(penalty) != 1) {
388-
rlang::abort(
389-
glue::glue(
390-
"`penalty` should be a single numeric value. `multi_predict()` ",
391-
"can be used to get multiple predictions per row of data."
392-
)
393-
)
390+
cli::cli_abort(c(
391+
"{.arg penalty} should be a single numeric value.",
392+
"i" = "{.fn multi_predict} can be used to get multiple predictions per row of data."
393+
))
394394
}
395395
}
396396

397397
if (length(object$fit$lambda) == 1 && penalty != object$fit$lambda) {
398-
rlang::abort(
399-
glue::glue(
400-
"The glmnet model was fit with a single penalty value of ",
401-
"{object$fit$lambda}. Predicting with a value of {penalty} ",
402-
"will give incorrect results from `glmnet()`."
403-
)
404-
)
398+
cli::cli_abort(c(
399+
"The glmnet model was fit with a single penalty value of
400+
{.arg object$fit$lambda}. Predicting with a value of {.arg penalty}
401+
will give incorrect results from `glmnet()`."
402+
))
405403
}
406404

407405
penalty

R/logistic_reg.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ translate.logistic_reg <- function(x, engine = x$engine, ...) {
9898
} else if (quo_get_expr(x$args$mixture) == 1) {
9999
arg_vals$type <- 6 ## lasso
100100
} else {
101-
rlang::abort("For the LiblineaR engine, mixture must be 0 or 1.")
101+
cli::cli_abort("For the LiblineaR engine, {.arg mixture} must be 0 or 1.")
102102
}
103103
}
104104
x$method$fit$args <- arg_vals
@@ -153,7 +153,7 @@ check_args.logistic_reg <- function(object, call = rlang::caller_env()) {
153153
call = call
154154
)
155155
}
156-
156+
157157
if ((!is.null(args$penalty)) && args$penalty == 0) {
158158
cli::cli_abort(
159159
"For the {.pkg LiblineaR} engine, {.arg penalty} must be {.code > 0}, \\

R/mars.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ multi_predict._earth <-
161161
object$fit$call[[i]] <- eval_tidy(object$fit$call[[i]])
162162
}
163163

164-
msg <-
165-
paste("Please use `keepxy = TRUE` as an option to enable submodel",
166-
"predictions with `earth`.")
164+
msg <-
165+
c("x" = "Please use {.code keepxy = TRUE} as an option to enable submodel
166+
predictions with earth.")
167167
if (any(names(object$fit$call) == "keepxy")) {
168168
if (!isTRUE(object$fit$call$keepxy))
169-
rlang::abort(msg)
169+
cli::cli_abort(msg)
170170
} else {
171-
rlang::abort(msg)
171+
cli::cli_abort(msg)
172172
}
173173

174174
if (is.null(type)) {

R/mlp.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ check_args.mlp <- function(object, call = rlang::caller_env()) {
148148

149149
class2ind <- function (x, drop2nd = FALSE) {
150150
if (!is.factor(x))
151-
rlang::abort("`x` should be a factor")
151+
cli::cli_abort(c("x" = "{.arg x} should be a factor."))
152152
y <- model.matrix( ~ x - 1)
153153
colnames(y) <- gsub("^x", "", colnames(y))
154154
attributes(y)$assign <- NULL
@@ -195,7 +195,7 @@ keras_mlp <-
195195
rlang::arg_match(activation, act_funs,)
196196

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

tests/testthat/_snaps/translate.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,10 @@
504504
translate_args(basic %>% set_engine("glmnet"))
505505
Condition
506506
Error in `.check_glmnet_penalty_fit()`:
507-
! For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
508-
* There are 0 values for `penalty`.
509-
* To try multiple values for total regularization, use the tune package.
510-
* To predict multiple penalties, use `multi_predict()`
507+
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
508+
! There are 0 values for `penalty`.
509+
i To try multiple values for total regularization, use the tune package.
510+
i To predict multiple penalties, use `multi_predict()`.
511511

512512
---
513513

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

563563
---
564564

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

696696
---
697697

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

835835
---
836836

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

976976
---
977977

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

13421342
---
13431343

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

15601560
# arguments (rand_forest)
15611561

0 commit comments

Comments
 (0)