Skip to content

Commit d5166a7

Browse files
author
‘topepo’
committed
route some glmnet checking calls
1 parent f97a6cc commit d5166a7

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

R/glmnet-engines.R

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,20 @@ format_glmnet_multinom_class <- function(pred, penalty, lvl, n_obs) {
357357
#' @rdname glmnet_helpers
358358
#' @keywords internal
359359
#' @export
360-
.check_glmnet_penalty_fit <- function(x) {
360+
.check_glmnet_penalty_fit <- function(x, call = rlang::caller_env()) {
361361
pen <- rlang::eval_tidy(x$args$penalty)
362362

363363
if (length(pen) != 1) {
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}."
371-
))
364+
cli::cli_abort(
365+
c(
366+
"x" = "For the glmnet engine, {.arg penalty} must be a single number
367+
(or a value of {.fn tune}).",
368+
"!" = "There are {length(pen)} value{?s} for {.arg penalty}.",
369+
"i" = "To try multiple values for total regularization, use the
370+
{.pkg tune} package.",
371+
"i" = "To predict multiple penalties, use {.fn multi_predict}."
372+
),
373+
call = call)
372374
}
373375
}
374376

@@ -379,27 +381,32 @@ format_glmnet_multinom_class <- function(pred, penalty, lvl, n_obs) {
379381
#' @rdname glmnet_helpers
380382
#' @keywords internal
381383
#' @export
382-
.check_glmnet_penalty_predict <- function(penalty = NULL, object, multi = FALSE) {
384+
.check_glmnet_penalty_predict <- function(penalty = NULL, object, multi = FALSE,
385+
call = rlang::caller_env()) {
383386
if (is.null(penalty)) {
384387
penalty <- object$fit$lambda
385388
}
386389

387390
# when using `predict()`, allow for a single lambda
388391
if (!multi) {
389392
if (length(penalty) != 1) {
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-
))
393+
cli::cli_abort(
394+
c(
395+
"{.arg penalty} should be a single numeric value.",
396+
"i" = "{.fn multi_predict} can be used to get multiple predictions per row of data."
397+
),
398+
call = call)
394399
}
395400
}
396401

397402
if (length(object$fit$lambda) == 1 && penalty != object$fit$lambda) {
398-
cli::cli_abort(c(
399-
"The glmnet model was fit with a single penalty value of
403+
cli::cli_abort(
404+
c(
405+
"The glmnet model was fit with a single penalty value of
400406
{.arg object$fit$lambda}. Predicting with a value of {.arg penalty}
401407
will give incorrect results from `glmnet()`."
402-
))
408+
),
409+
call = call)
403410
}
404411

405412
penalty

tests/testthat/_snaps/translate.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@
503503
Code
504504
translate_args(basic %>% set_engine("glmnet"))
505505
Condition
506-
Error in `.check_glmnet_penalty_fit()`:
506+
Error in `translate()`:
507507
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
508508
! There are 0 values for `penalty`.
509509
i To try multiple values for total regularization, use the tune package.
@@ -554,7 +554,7 @@
554554
Code
555555
translate_args(mixture %>% set_engine("glmnet"))
556556
Condition
557-
Error in `.check_glmnet_penalty_fit()`:
557+
Error in `translate()`:
558558
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
559559
! There are 0 values for `penalty`.
560560
i To try multiple values for total regularization, use the tune package.
@@ -687,7 +687,7 @@
687687
Code
688688
translate_args(basic %>% set_engine("glmnet"))
689689
Condition
690-
Error in `.check_glmnet_penalty_fit()`:
690+
Error in `translate()`:
691691
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
692692
! There are 0 values for `penalty`.
693693
i To try multiple values for total regularization, use the tune package.
@@ -826,7 +826,7 @@
826826
Code
827827
translate_args(mixture %>% set_engine("glmnet"))
828828
Condition
829-
Error in `.check_glmnet_penalty_fit()`:
829+
Error in `translate()`:
830830
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
831831
! There are 0 values for `penalty`.
832832
i To try multiple values for total regularization, use the tune package.
@@ -967,7 +967,7 @@
967967
Code
968968
translate_args(mixture_v %>% set_engine("glmnet"))
969969
Condition
970-
Error in `.check_glmnet_penalty_fit()`:
970+
Error in `translate()`:
971971
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
972972
! There are 0 values for `penalty`.
973973
i To try multiple values for total regularization, use the tune package.
@@ -1333,7 +1333,7 @@
13331333
Code
13341334
translate_args(basic %>% set_engine("glmnet"))
13351335
Condition
1336-
Error in `.check_glmnet_penalty_fit()`:
1336+
Error in `translate()`:
13371337
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
13381338
! There are 0 values for `penalty`.
13391339
i To try multiple values for total regularization, use the tune package.
@@ -1551,7 +1551,7 @@
15511551
Code
15521552
basic_incomplete %>% translate_args()
15531553
Condition
1554-
Error in `.check_glmnet_penalty_fit()`:
1554+
Error in `translate()`:
15551555
x For the glmnet engine, `penalty` must be a single number (or a value of `tune()`).
15561556
! There are 0 values for `penalty`.
15571557
i To try multiple values for total regularization, use the tune package.

0 commit comments

Comments
 (0)