Skip to content

Commit 33f621c

Browse files
topeposimonpcouch
andauthored
A few missing cli error conversions (#1213)
* remaning rlng errors to cli errors * remove redundant tests * updated snapshots * Update R/misc.R Co-authored-by: Simon P. Couch <[email protected]> * Apply suggestions from code review Co-authored-by: Simon P. Couch <[email protected]> * update snapshots --------- Co-authored-by: Simon P. Couch <[email protected]>
1 parent 297320e commit 33f621c

17 files changed

+58
-161
lines changed

R/descriptors.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ get_descr_spark <- function(formula, data) {
245245
.obs <- function() obs
246246
.lvls <- function() y_vals
247247
.facts <- function() factor_pred
248-
.x <- function() abort("Descriptor .x() not defined for Spark.")
249-
.y <- function() abort("Descriptor .y() not defined for Spark.")
250-
.dat <- function() abort("Descriptor .dat() not defined for Spark.")
248+
.x <- function() cli::cli_abort("Descriptor {.fn .x} not defined for Spark.")
249+
.y <- function() cli::cli_abort("Descriptor {.fn .y} not defined for Spark.")
250+
.dat <- function() cli::cli_abort("Descriptor {.fn .dat} not defined for Spark.")
251251

252252
# still need .x(), .y(), .dat() ?
253253

@@ -409,13 +409,13 @@ scoped_descrs <- function(descrs, frame = caller_env()) {
409409
# with their actual implementations
410410
descr_env <- rlang::new_environment(
411411
data = list(
412-
.cols = function() abort("Descriptor context not set"),
413-
.preds = function() abort("Descriptor context not set"),
414-
.obs = function() abort("Descriptor context not set"),
415-
.lvls = function() abort("Descriptor context not set"),
416-
.facts = function() abort("Descriptor context not set"),
417-
.x = function() abort("Descriptor context not set"),
418-
.y = function() abort("Descriptor context not set"),
419-
.dat = function() abort("Descriptor context not set")
412+
.cols = function() cli::cli_abort("Descriptor context not set"),
413+
.preds = function() cli::cli_abort("Descriptor context not set"),
414+
.obs = function() cli::cli_abort("Descriptor context not set"),
415+
.lvls = function() cli::cli_abort("Descriptor context not set"),
416+
.facts = function() cli::cli_abort("Descriptor context not set"),
417+
.x = function() cli::cli_abort("Descriptor context not set"),
418+
.y = function() cli::cli_abort("Descriptor context not set"),
419+
.dat = function() cli::cli_abort("Descriptor context not set")
420420
)
421421
)

R/misc.R

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -369,22 +369,20 @@ check_outcome <- function(y, spec) {
369369
if (spec$mode == "regression") {
370370
outcome_is_numeric <- if (is.atomic(y)) {is.numeric(y)} else {all(map_lgl(y, is.numeric))}
371371
if (!outcome_is_numeric) {
372-
cls <- class(y)[[1]]
373-
abort(paste0(
374-
"For a regression model, the outcome should be `numeric`, ",
375-
"not a `", cls, "`."
376-
))
372+
cli::cli_abort(
373+
"For a regression model, the outcome should be {.cls numeric}, not
374+
{.obj_type_friendly {y}}."
375+
)
377376
}
378377
}
379378

380379
if (spec$mode == "classification") {
381380
outcome_is_factor <- if (is.atomic(y)) {is.factor(y)} else {all(map_lgl(y, is.factor))}
382381
if (!outcome_is_factor) {
383-
cls <- class(y)[[1]]
384-
abort(paste0(
385-
"For a classification model, the outcome should be a `factor`, ",
386-
"not a `", cls, "`."
387-
))
382+
cli::cli_abort(
383+
"For a classification model, the outcome should be a {.cls factor}, not
384+
{.obj_type_friendly {y}}."
385+
)
388386
}
389387

390388
if (inherits(spec, "logistic_reg") && is.atomic(y) && length(levels(y)) > 2) {
@@ -402,11 +400,10 @@ check_outcome <- function(y, spec) {
402400
if (spec$mode == "censored regression") {
403401
outcome_is_surv <- inherits(y, "Surv")
404402
if (!outcome_is_surv) {
405-
cls <- class(y)[[1]]
406-
abort(paste0(
407-
"For a censored regression model, the outcome should be a `Surv` object, ",
408-
"not a `", cls, "`."
409-
))
403+
cli::cli_abort(
404+
"For a censored regression model, the outcome should be a {.cls Surv} object, not
405+
{.obj_type_friendly {y}}."
406+
)
410407
}
411408
}
412409

tests/testthat/_snaps/boost_tree_xgboost.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@
66
Error:
77
! object 'novar' not found
88

9-
# xgboost execution, regression
10-
11-
Code
12-
res <- parsnip::fit_xy(car_basic, x = mtcars[, num_pred], y = factor(mtcars$vs),
13-
control = ctrl)
14-
Condition
15-
Error in `check_outcome()`:
16-
! For a regression model, the outcome should be `numeric`, not a `factor`.
17-
189
# submodel prediction
1910

2011
Code

tests/testthat/_snaps/linear_reg.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
res <- fit_xy(hpc_basic, x = hpc[, num_pred], y = hpc$class, control = ctrl)
4747
Condition
4848
Error in `check_outcome()`:
49-
! For a regression model, the outcome should be `numeric`, not a `factor`.
49+
! For a regression model, the outcome should be <numeric>, not a <factor> object.
5050

5151
---
5252

@@ -55,47 +55,47 @@
5555
control = ctrl)
5656
Condition
5757
Error in `check_outcome()`:
58-
! For a regression model, the outcome should be `numeric`, not a `character`.
58+
! For a regression model, the outcome should be <numeric>, not a character vector.
5959

6060
---
6161

6262
Code
6363
res <- fit(hpc_basic, hpc_bad_form, data = hpc, control = ctrl)
6464
Condition
6565
Error in `check_outcome()`:
66-
! For a regression model, the outcome should be `numeric`, not a `factor`.
66+
! For a regression model, the outcome should be <numeric>, not a <factor> object.
6767

6868
---
6969

7070
Code
7171
lm_form_catch <- fit(hpc_basic, hpc_bad_form, data = hpc, control = caught_ctrl)
7272
Condition
7373
Error in `check_outcome()`:
74-
! For a regression model, the outcome should be `numeric`, not a `factor`.
74+
! For a regression model, the outcome should be <numeric>, not a <factor> object.
7575

7676
# glm execution
7777

7878
Code
7979
res <- fit_xy(hpc_glm, x = hpc[, num_pred], y = hpc$class, control = ctrl)
8080
Condition
8181
Error in `check_outcome()`:
82-
! For a regression model, the outcome should be `numeric`, not a `factor`.
82+
! For a regression model, the outcome should be <numeric>, not a <factor> object.
8383

8484
---
8585

8686
Code
8787
res <- fit(hpc_glm, hpc_bad_form, data = hpc, control = ctrl)
8888
Condition
8989
Error in `check_outcome()`:
90-
! For a regression model, the outcome should be `numeric`, not a `factor`.
90+
! For a regression model, the outcome should be <numeric>, not a <factor> object.
9191

9292
---
9393

9494
Code
9595
lm_form_catch <- fit(hpc_glm, hpc_bad_form, data = hpc, control = caught_ctrl)
9696
Condition
9797
Error in `check_outcome()`:
98-
! For a regression model, the outcome should be `numeric`, not a `factor`.
98+
! For a regression model, the outcome should be <numeric>, not a <factor> object.
9999

100100
# newdata error trapping
101101

tests/testthat/_snaps/logistic_reg.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
res <- fit(lc_basic, funded_amnt ~ term, data = lending_club, control = ctrl)
5252
Condition
5353
Error in `check_outcome()`:
54-
! For a classification model, the outcome should be a `factor`, not a `integer`.
54+
! For a classification model, the outcome should be a <factor>, not an integer vector.
5555

5656
---
5757

@@ -60,7 +60,7 @@
6060
control = caught_ctrl)
6161
Condition
6262
Error in `check_outcome()`:
63-
! For a classification model, the outcome should be a `factor`, not a `integer`.
63+
! For a classification model, the outcome should be a <factor>, not an integer vector.
6464

6565
---
6666

@@ -69,15 +69,15 @@
6969
num_pred], y = lending_club$total_bal_il)
7070
Condition
7171
Error in `check_outcome()`:
72-
! For a classification model, the outcome should be a `factor`, not a `integer`.
72+
! For a classification model, the outcome should be a <factor>, not an integer vector.
7373

7474
# liblinear execution
7575

7676
Code
7777
res <- fit(ll_basic, funded_amnt ~ term, data = lending_club, control = ctrl)
7878
Condition
7979
Error in `check_outcome()`:
80-
! For a classification model, the outcome should be a `factor`, not a `integer`.
80+
! For a classification model, the outcome should be a <factor>, not an integer vector.
8181

8282
---
8383

@@ -86,7 +86,7 @@
8686
control = caught_ctrl)
8787
Condition
8888
Error in `check_outcome()`:
89-
! For a classification model, the outcome should be a `factor`, not a `integer`.
89+
! For a classification model, the outcome should be a <factor>, not an integer vector.
9090

9191
---
9292

@@ -95,7 +95,7 @@
9595
num_pred], y = lending_club$total_bal_il)
9696
Condition
9797
Error in `check_outcome()`:
98-
! For a classification model, the outcome should be a `factor`, not a `integer`.
98+
! For a classification model, the outcome should be a <factor>, not an integer vector.
9999

100100
# check_args() works
101101

tests/testthat/_snaps/mars.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131
x Engine "wat?" is not supported for `mars()`
3232
i See `show_engines("mars")`.
3333

34-
# mars execution
35-
36-
Code
37-
res <- fit(hpc_basic, hpc_bad_form, data = hpc, control = ctrl)
38-
Condition
39-
Error in `check_outcome()`:
40-
! For a regression model, the outcome should be `numeric`, not a `factor`.
41-
4234
# submodel prediction
4335

4436
Code

tests/testthat/_snaps/misc.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,6 @@
150150

151151
# check_outcome works as expected
152152

153-
Code
154-
check_outcome(factor(1:2), reg_spec)
155-
Condition
156-
Error in `check_outcome()`:
157-
! For a regression model, the outcome should be `numeric`, not a `factor`.
158-
159-
---
160-
161153
Code
162154
check_outcome(NULL, reg_spec)
163155
Condition
@@ -192,14 +184,6 @@
192184
! `linear_reg()` was unable to find an outcome.
193185
i Ensure that you have specified an outcome column and that it hasn't been removed in pre-processing.
194186

195-
---
196-
197-
Code
198-
check_outcome(1:2, class_spec)
199-
Condition
200-
Error in `check_outcome()`:
201-
! For a classification model, the outcome should be a `factor`, not a `integer`.
202-
203187
---
204188

205189
Code
@@ -233,5 +217,5 @@
233217
check_outcome(1:2, cens_spec)
234218
Condition
235219
Error in `check_outcome()`:
236-
! For a censored regression model, the outcome should be a `Surv` object, not a `integer`.
220+
! For a censored regression model, the outcome should be a <Surv> object, not an integer vector.
237221

tests/testthat/_snaps/nearest_neighbor_kknn.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# kknn execution
22

3-
Code
4-
fit_xy(hpc_basic, control = ctrl, x = hpc[, num_pred], y = hpc$input_fields)
5-
Condition
6-
Error in `check_outcome()`:
7-
! For a classification model, the outcome should be a `factor`, not a `numeric`.
8-
9-
---
10-
113
Code
124
fit(hpc_basic, hpc_bad_form, data = hpc, control = ctrl)
135
Condition

tests/testthat/_snaps/predict_formats.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class = class == "VF"))
1515
Condition
1616
Error in `check_outcome()`:
17-
! For a classification model, the outcome should be a `factor`, not a `logical`.
17+
! For a classification model, the outcome should be a <factor>, not a logical vector.
1818

1919
---
2020

@@ -23,7 +23,7 @@
2323
class = ifelse(class == "VF", 1, 0)))
2424
Condition
2525
Error in `check_outcome()`:
26-
! For a classification model, the outcome should be a `factor`, not a `numeric`.
26+
! For a classification model, the outcome should be a <factor>, not a double vector.
2727

2828
---
2929

@@ -32,5 +32,5 @@
3232
dplyr::mutate(class = as.character(class)))
3333
Condition
3434
Error in `check_outcome()`:
35-
! For a classification model, the outcome should be a `factor`, not a `character`.
35+
! For a classification model, the outcome should be a <factor>, not a character vector.
3636

tests/testthat/_snaps/rand_forest_ranger.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@
44
res <- fit(lc_ranger, funded_amnt ~ Class + term, data = lending_club, control = ctrl)
55
Condition
66
Error in `check_outcome()`:
7-
! For a classification model, the outcome should be a `factor`, not a `integer`.
8-
9-
---
10-
11-
Code
12-
res <- fit(bad_ranger_cls, funded_amnt ~ term, data = lending_club, control = ctrl)
13-
Condition
14-
Error in `check_outcome()`:
15-
! For a classification model, the outcome should be a `factor`, not a `integer`.
7+
! For a classification model, the outcome should be a <factor>, not an integer vector.
168

179
# ranger classification probabilities
1810

0 commit comments

Comments
 (0)