Skip to content

Commit 170cc8f

Browse files
aluxhEmilHvitfeldt
andauthored
Convert errors to use cli (#91)
Co-authored-by: Emil Hvitfeldt <[email protected]>
1 parent d30438e commit 170cc8f

File tree

9 files changed

+28
-39
lines changed

9 files changed

+28
-39
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Depends:
2121
parsnip (>= 1.0.3),
2222
R (>= 4.1)
2323
Imports:
24+
cli,
2425
dials (>= 0.1.1.9001),
2526
dplyr,
2627
generics (>= 0.1.0),

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ importFrom(generics,tidy)
2828
importFrom(generics,tunable)
2929
importFrom(parsnip,multi_predict)
3030
importFrom(purrr,map_dfr)
31-
importFrom(rlang,abort)
3231
importFrom(rlang,call2)
3332
importFrom(rlang,enquos)
3433
importFrom(rlang,eval_tidy)

R/C5_rules.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ c5_fit <- function(x, y, trials = 1, minCases = 2, cost = NULL, ...) {
4141

4242
if (!is.null(cost)) {
4343
if (length(levels(y) != 2)) {
44-
rlang::abort("Cost-sensitive models only implemented for 2 classes.")
44+
cli::cli_abort("Cost-sensitive models only implemented for 2 classes.")
4545
}
4646
costs <- matrix(c(0, 1, cost, 0), nrow = 2, byrow = TRUE)
4747
args$costs <- costs
@@ -54,7 +54,7 @@ c5_fit <- function(x, y, trials = 1, minCases = 2, cost = NULL, ...) {
5454

5555
c5_pred_wrap <- function(trials = 1, object, new_data, type = "class", ...) {
5656
if (length(trials) > 1) {
57-
rlang::abort("`c5_pred_wrap` takes a single value of `trials`")
57+
cli::cli_abort("`c5_pred_wrap` takes a single value of {.arg trials}")
5858
}
5959
trials[trials < 1] <- 1L
6060
trials[trials > 100] <- 100L

R/cubist_rules.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ get_neighbors <- function(x) {
9595

9696
cubist_pred_wrap <- function(neighbors = 0, object, new_data, ...) {
9797
if (length(neighbors) > 1) {
98-
rlang::abort("`cubist_pred_wrap` takes a single neighbor.")
98+
cli::cli_abort("{.fn cubist_pred_wrap} takes a single neighbor.")
9999
}
100100
object$spec$args$neighbors <- neighbors
101101
res <- predict(object, new_data)
@@ -203,7 +203,9 @@ tunable.cubist_rules <- function(x, ...) {
203203
multi_predict._cubist <-
204204
function(object, new_data, type = NULL, neighbors = NULL, ...) {
205205
if (any(names(enquos(...)) == "newdata")) {
206-
rlang::abort("Did you mean to use `new_data` instead of `newdata`?")
206+
cli::cli_abort(
207+
"Did you mean to use {.arg new_data} instead of {.arg newdata}?"
208+
)
207209
}
208210
if (is.null(neighbors)) {
209211
n <- 1

R/rule_fit.R

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ xrf_fit <-
8282

8383
process_mtry <- function(colsample_bytree, counts, n_predictors, is_missing) {
8484
if (!is.logical(counts)) {
85-
rlang::abort("'counts' should be a logical value.")
85+
cli::cli_abort("{.arg counts} should be a logical value.")
8686
}
8787

8888
ineq <- if (counts) {
@@ -102,35 +102,21 @@ process_mtry <- function(colsample_bytree, counts, n_predictors, is_missing) {
102102
}
103103

104104
if ((colsample_bytree < 1 & counts) | (colsample_bytree > 1 & !counts)) {
105-
rlang::abort(
106-
paste0(
107-
"The supplied argument `mtry = ",
108-
colsample_bytree,
109-
"` must be ",
110-
ineq,
111-
" than or equal to 1. \n\n`mtry` is currently being interpreted ",
112-
"as a ",
113-
interp,
114-
" rather than a ",
115-
opp,
116-
". Supply `counts = ",
117-
!counts,
118-
"` to `set_engine()` to supply this argument as a ",
119-
opp,
120-
" rather than ",
121-
# TODO: add a section to the linked parsnip docs on mtry vs mtry_prop
122-
"a ",
123-
interp,
124-
". \n\nSee `?details_rule_fit_xrf` for more details."
125-
),
126-
call = NULL
105+
cli::cli_abort(
106+
c(
107+
"The supplied argument `{.arg mtry} = {colsample_bytree}` must be {ineq} than or equal to 1.",
108+
"i" = "{.arg mtry} is currently being interpreted as a {interp} rather than a {opp}.",
109+
"i" = "Supply `{.arg counts} = {!counts}` to `set_engine()` to supply this argument as a {opp} rather than a {interp}.",
110+
"i" = "See {.help details_rule_fit_xrf} for more details.",
111+
call = NULL
112+
)
127113
)
128114
}
129115

130116
if (rlang::is_call(colsample_bytree)) {
131117
if (rlang::call_name(colsample_bytree) == "tune") {
132-
rlang::abort(
133-
paste0(
118+
cli::cli_abort(
119+
c(
134120
"The supplied `mtry` parameter is a call to `tune`. Did you forget ",
135121
"to optimize hyperparameters with a tuning function like `tune::tune_grid`?"
136122
),
@@ -220,7 +206,9 @@ xrf_pred <- function(object, new_data, lambda = object$fit$lambda, type, ...) {
220206
multi_predict._xrf <-
221207
function(object, new_data, type = NULL, penalty = NULL, ...) {
222208
if (any(names(enquos(...)) == "newdata")) {
223-
rlang::abort("Did you mean to use `new_data` instead of `newdata`?")
209+
cli::cli_abort(
210+
"Did you mean to use {.arg new_data} instead of {.arg newdata}?"
211+
)
224212
}
225213
if (is.null(penalty)) {
226214
penalty <- object$fit$lambda

R/rules-package.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#' @importFrom dplyr as_tibble
77
#' @importFrom dplyr tibble
88
#' @importFrom purrr map_dfr
9-
#' @importFrom rlang abort
109
#' @importFrom rlang call2
1110
#' @importFrom rlang enquos
1211
#' @importFrom rlang eval_tidy

R/tidy_C5.0.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ get_rule_index <- function(index, tree, history = c(), levels) {
200200
res <- c(res, new_rules)
201201
} else {
202202
msg <- paste("Unknown split type", curr$type)
203-
rlang::abort(msg)
203+
cli::cli_abort("{msg}")
204204
}
205205

206206
res
@@ -231,7 +231,7 @@ get_freqs <- function(rule, tree, lvls) {
231231
length(lvls),
232232
")."
233233
)
234-
rlang::abort(msg)
234+
cli::cli_abort("{msg}")
235235
}
236236
tibble::tibble(value = lvls, count = freqs)
237237
}

R/tidy_cubist.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ get_reg_data <- function(txt, results = "expression") {
144144
if (length(vals) > 0) {
145145
n_elem <- length(vals)
146146
if (n_elem %% 2 != 0) {
147-
rlang::abort("number of remaining terms not even", call. = FALSE)
147+
cli::cli_abort("Number of remaining terms is not even.", call. = FALSE)
148148
}
149149
n_terms <- n_elem / 2
150150
split_terms <- split(vals, rep(1:n_terms, each = 2))
@@ -158,7 +158,7 @@ get_reg_data <- function(txt, results = "expression") {
158158
splits_to_coefs <- function(x, int) {
159159
num_check <- purrr::map_int(x, length)
160160
if (!all(num_check == 2)) {
161-
rlang::abort("Problem with getting coefficients")
161+
cli::cli_abort("Problem with getting coefficients")
162162
}
163163
coef_val <- purrr::map_dbl(x, \(.x) as.numeric(.x[2]))
164164
res <- tibble(term = purrr::map_chr(x, \(.x) .x[1]), estimate = coef_val)

R/tidy_xrf.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ tidy.xrf <- function(x, penalty = NULL, unit = c("rules", "columns"), ...) {
55

66
msg <- "Please choose a single numeric value of 'penalty'."
77
if (is.null(penalty)) {
8-
rlang::abort(msg)
8+
cli::cli_abort("{msg}")
99
} else {
1010
if (!is.numeric(penalty) | length(penalty) != 1) {
11-
rlang::abort(msg)
11+
cli::cli_abort("{msg}")
1212
}
1313
}
1414

@@ -51,7 +51,7 @@ xrf_coefs <- function(x, penalty = NULL) {
5151
penalty <- x$lambda
5252
}
5353
if (length(penalty) != 1) {
54-
rlang::abort("`penalty` should be a single numeric measure.")
54+
cli::cli_abort("{.arg penalty} should be a single numeric measure.")
5555
}
5656

5757
lvls <- x$levels

0 commit comments

Comments
 (0)