Skip to content

Commit dc9e188

Browse files
simonpcouchtopepoEmilHvitfeldt
authored
transition remaining rlang warnings and errors to cli (#1162)
--------- Co-authored-by: topepo <[email protected]> Co-authored-by: Emil Hvitfeldt <[email protected]>
1 parent 81d9536 commit dc9e188

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+361
-214
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: parsnip
22
Title: A Common API to Modeling and Analysis Functions
3-
Version: 1.2.1.9001
3+
Version: 1.2.1.9002
44
Authors@R: c(
55
person("Max", "Kuhn", , "[email protected]", role = c("aut", "cre")),
66
person("Davis", "Vaughan", , "[email protected]", role = "aut"),

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* Transitioned package errors and warnings to use cli (#1147 and #1148 by
66
@shum461, #1153 by @RobLBaker and @wright13, #1154 by @JamesHWade, #1160,
7-
#1161).
7+
#1161, #1081).
88

99
* `fit_xy()` currently raises an error for `gen_additive_mod()` model specifications as the default engine (`"mgcv"`) specifies smoothing terms in model formulas. However, some engines specify smooths via additional arguments, in which case the restriction on `fit_xy()` is excessive. parsnip will now only raise an error when fitting a `gen_additive_mod()` with `fit_xy()` when using the `"mgcv"` engine (#775).
1010

R/aaa_multi_predict.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#' @export
2020
multi_predict <- function(object, ...) {
2121
if (inherits(object$fit, "try-error")) {
22-
rlang::warn("Model fit failed; cannot make predictions.")
22+
cli::cli_warn("Model fit failed; cannot make predictions.")
2323
return(NULL)
2424
}
2525
check_for_newdata(...)
@@ -28,17 +28,19 @@ multi_predict <- function(object, ...) {
2828

2929
#' @export
3030
#' @rdname multi_predict
31-
multi_predict.default <- function(object, ...)
32-
rlang::abort(
33-
glue::glue(
34-
"No `multi_predict` method exists for objects with classes ",
35-
glue::glue_collapse(glue::glue("'{class(object)}'"), sep = ", ")
36-
)
37-
)
31+
multi_predict.default <- function(object, ...) {
32+
cli::cli_abort(
33+
"No {.fun multi_predict} method exists for objects with classes
34+
{.cls {class(object)}}."
35+
)
36+
}
3837

3938
#' @export
4039
predict.model_spec <- function(object, ...) {
41-
rlang::abort("You must use `fit()` on your model specification before you can use `predict()`.")
40+
cli::cli_abort(
41+
"You must {.fun fit} your model specification
42+
before you can use {.fun predict}."
43+
)
4244
}
4345

4446
#' Tools for models that predict on sub-models

R/adds.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @export
88
add_rowindex <- function(x) {
99
if (!is.data.frame(x)) {
10-
rlang::abort("`x` should be a data frame.")
10+
cli::cli_abort("{.arg x} should be a data frame.")
1111
}
1212
x <- dplyr::mutate(x, .row = seq_len(nrow(x)))
1313
x

R/arguments.R

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ check_eng_args <- function(args, obj, core_args) {
2222
if (length(common_args) > 0) {
2323
args <- args[!(names(args) %in% common_args)]
2424
common_args <- paste0(common_args, collapse = ", ")
25-
rlang::warn(glue::glue("The following arguments cannot be manually modified ",
26-
"and were removed: {common_args}."))
25+
cli::cli_warn(
26+
"The argument{?s} {.arg {common_args}} cannot be manually
27+
modified and {?was/were} removed."
28+
)
2729
}
2830
args
2931
}
@@ -56,7 +58,7 @@ set_args <- function(object, ...) {
5658
set_args.model_spec <- function(object, ...) {
5759
the_dots <- enquos(...)
5860
if (length(the_dots) == 0)
59-
rlang::abort("Please pass at least one named argument.")
61+
cli::cli_abort("Please pass at least one named argument.")
6062
main_args <- names(object$args)
6163
new_args <- names(the_dots)
6264
for (i in new_args) {
@@ -262,7 +264,7 @@ make_xy_call <- function(object, target, env) {
262264
none = rlang::expr(x),
263265
data.frame = rlang::expr(maybe_data_frame(x)),
264266
matrix = rlang::expr(maybe_matrix(x)),
265-
rlang::abort(glue::glue("Invalid data type target: {target}."))
267+
cli::cli_abort("Invalid data type target: {target}.")
266268
)
267269
if (uses_weights) {
268270
object$method$fit$args[[ unname(data_args["weights"]) ]] <- rlang::expr(weights)
@@ -316,9 +318,13 @@ min_cols <- function(num_cols, source) {
316318
p <- ncol(source)
317319
}
318320
if (num_cols > p) {
319-
msg <- paste0(num_cols, " columns were requested but there were ", p,
320-
" predictors in the data. ", p, " will be used.")
321-
rlang::warn(msg)
321+
cli::cli_warn(
322+
c(
323+
"!" = "{num_cols} column{?s} {?was/were} requested but there {cli::qty(p)} {?was/were}
324+
{p} predictor{?s} in the data.",
325+
"i" = "{p} predictor{?s} will be used."
326+
)
327+
)
322328
num_cols <- p
323329
}
324330

@@ -335,9 +341,14 @@ min_rows <- function(num_rows, source, offset = 0) {
335341
}
336342

337343
if (num_rows > n - offset) {
338-
msg <- paste0(num_rows, " samples were requested but there were ", n,
339-
" rows in the data. ", n - offset, " will be used.")
340-
rlang::warn(msg)
344+
cli::cli_warn(
345+
c(
346+
"!" = "{num_rows} sample{?s} {?was/were} requested but there were
347+
{n} rows in the data.",
348+
"i" = "{n - offset} sample{?s} will be used."
349+
)
350+
)
351+
341352
num_rows <- n - offset
342353
}
343354

R/augment.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ augment.model_fit <- function(x, new_data, eval_time = NULL, ...) {
8686
"regression" = augment_regression(x, new_data),
8787
"classification" = augment_classification(x, new_data),
8888
"censored regression" = augment_censored(x, new_data, eval_time = eval_time),
89-
rlang::abort(paste("Unknown mode:", x$spec$mode))
89+
cli::cli_abort(
90+
c(
91+
"Unknown mode {.val {x$spec$mode}}.",
92+
"i" = "Model mode should be one of {.or {.val {all_modes}}}."
93+
)
94+
)
9095
)
9196
tibble::new_tibble(res)
9297
}

R/autoplot.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ map_glmnet_coefs <- function(x) {
4444
# work. If an object is loaded from a new session, they will need to load the
4545
# package.
4646
if (is.null(coefs)) {
47-
rlang::abort("Please load the glmnet package before running `autoplot()`.")
47+
cli::cli_abort(
48+
"Please load the {.pkg glmnet} package before running {.fun autoplot}."
49+
)
4850
}
4951
p <- x$dim[1]
5052
if (is.list(coefs)) {
@@ -161,8 +163,7 @@ check_penalty_value <- function(x) {
161163
cl <- match.call()
162164
arg_val <- as.character(cl$x)
163165
if (!is.vector(x) || length(x) != 1 || !is.numeric(x) || x < 0) {
164-
msg <- paste0("Argument '", arg_val, "' should be a single, non-negative value.")
165-
rlang::abort(msg)
166+
cli::cli_abort("{.arg {arg_val}} should be a single, non-negative value.")
166167
}
167168
invisible(x)
168169
}

R/case_weights.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ case_weights_allowed <- function(spec) {
8787
get_from_env(paste0(mod_type, "_fit")) %>%
8888
dplyr::filter(engine == mod_eng & mode == mod_mode)
8989
if (nrow(model_info) != 1) {
90-
rlang::abort(
91-
glue::glue(
92-
"Error in getting model information for model {mod_type} with engine {mod_eng} and mode {mod_mode}."
93-
)
90+
cli::cli_abort(
91+
"Error in getting model information for model {mod_type} with
92+
engine {mod_eng} and mode {mod_mode}."
9493
)
9594
}
9695
# If weights are used, they are protected data arguments with the canonical

R/condense_control.R

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,12 @@
2323
condense_control <- function(x, ref) {
2424
mismatch <- setdiff(names(ref), names(x))
2525
if (length(mismatch)) {
26-
rlang::abort(
26+
cli::cli_abort(
2727
c(
28-
glue::glue(
29-
"Object of class `{class(x)[1]}` cannot be coerced to ",
30-
"object of class `{class(ref)[1]}`."
31-
),
32-
"The following arguments are missing:",
33-
glue::glue_collapse(
34-
glue::single_quote(mismatch),
35-
sep = ", ", last = ", and "
36-
)
28+
"Object of class {.cls class(x)[1]} cannot be coerced to
29+
object of class {.cls class(ref)[1]}.",
30+
"i" = "{cli::qty(mismatch)} The argument{?s} {.arg {mismatch}}
31+
{?is/are} missing."
3732
)
3833
)
3934
}

R/contr_one_hot.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#' @export
1717
contr_one_hot <- function(n, contrasts = TRUE, sparse = FALSE) {
1818
if (sparse) {
19-
rlang::warn("`sparse = TRUE` not implemented for `contr_one_hot()`.")
19+
cli::cli_warn("{.code sparse = TRUE} not implemented for {.fun contr_one_hot}.")
2020
}
2121

2222
if (!contrasts) {
23-
rlang::warn("`contrasts = FALSE` not implemented for `contr_one_hot()`.")
23+
cli::cli_warn("{.code contrasts = FALSE} not implemented for {.fun contr_one_hot}.")
2424
}
2525

2626
if (is.character(n)) {
@@ -30,12 +30,12 @@ contr_one_hot <- function(n, contrasts = TRUE, sparse = FALSE) {
3030
n <- as.integer(n)
3131

3232
if (length(n) != 1L) {
33-
rlang::abort("`n` must have length 1 when an integer is provided.")
33+
cli::cli_abort("{.arg n} must have length 1 when an integer is provided.")
3434
}
3535

3636
names <- as.character(seq_len(n))
3737
} else {
38-
rlang::abort("`n` must be a character vector or an integer of size 1.")
38+
cli::cli_abort("{.arg n} must be a character vector or an integer of size 1.")
3939
}
4040

4141
out <- diag(n)

R/control_parsnip.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ control_parsnip <- function(verbosity = 1L, catch = FALSE) {
2828
res
2929
}
3030

31-
check_control <- function(x) {
31+
check_control <- function(x, call = rlang::caller_env()) {
3232
if (!is.list(x))
33-
rlang::abort("control should be a named list.")
33+
cli::cli_abort("{.arg control} should be a named list.", call = call)
3434
if (!isTRUE(all.equal(sort(names(x)), c("catch", "verbosity"))))
35-
rlang::abort("control should be a named list with elements 'verbosity' and 'catch'.")
35+
cli::cli_abort(
36+
"{.arg control} should be a named list with elements {.field verbosity}
37+
and {.field catch}.",
38+
call = call
39+
)
3640
# based on ?is.integer
3741
int_check <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol
3842
if (!int_check(x$verbosity))
39-
rlang::abort("verbosity should be an integer.")
43+
cli::cli_abort("{.arg verbosity} should be an integer.", call = call)
4044
if (!is.logical(x$catch))
41-
rlang::abort("catch should be a logical.")
45+
cli::cli_abort("{.arg catch} should be a logical.", call = call)
4246
x
4347
}
4448

R/convert_data.R

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
composition = "data.frame",
4343
remove_intercept = TRUE) {
4444
if (!(composition %in% c("data.frame", "matrix"))) {
45-
rlang::abort("`composition` should be either 'data.frame' or 'matrix'.")
45+
cli::cli_abort(
46+
"{.arg composition} should be either {.val data.frame} or {.val matrix}."
47+
)
4648
}
4749

4850
if (remove_intercept) {
@@ -74,7 +76,7 @@
7476

7577
w <- as.vector(model.weights(mod_frame))
7678
if (!is.null(w) && !is.numeric(w)) {
77-
rlang::abort("`weights` must be a numeric vector")
79+
cli::cli_abort("{.arg weights} must be a numeric vector.")
7880
}
7981

8082
# TODO: Do we actually use the offset when fitting?
@@ -151,7 +153,9 @@
151153
na.action = na.pass,
152154
composition = "data.frame") {
153155
if (!(composition %in% c("data.frame", "matrix"))) {
154-
rlang::abort("`composition` should be either 'data.frame' or 'matrix'.")
156+
cli::cli_abort(
157+
"{.arg composition} should be either {.val data.frame} or {.val matrix}."
158+
)
155159
}
156160

157161
mod_terms <- object$terms
@@ -218,7 +222,7 @@
218222
y_name = "..y",
219223
remove_intercept = TRUE) {
220224
if (is.vector(x)) {
221-
rlang::abort("`x` cannot be a vector.")
225+
cli::cli_abort("{.arg x} cannot be a vector.")
222226
}
223227

224228
if (remove_intercept) {
@@ -251,10 +255,10 @@
251255

252256
if (!is.null(weights)) {
253257
if (!is.numeric(weights)) {
254-
rlang::abort("`weights` must be a numeric vector")
258+
cli::cli_abort("{.arg weights} must be a numeric vector.")
255259
}
256260
if (length(weights) != nrow(x)) {
257-
rlang::abort(glue::glue("`weights` should have {nrow(x)} elements"))
261+
cli::cli_abort("{.arg weights} should have {nrow(x)} elements.")
258262
}
259263

260264
form <- patch_formula_environment_with_case_weights(
@@ -294,17 +298,17 @@ local_one_hot_contrasts <- function(frame = rlang::caller_env()) {
294298
rlang::local_options(contrasts = contrasts, .frame = frame)
295299
}
296300

297-
check_form_dots <- function(x) {
301+
check_form_dots <- function(x, call = rlang::caller_env()) {
298302
good_args <- c("subset", "weights")
299303
good_names <- names(x) %in% good_args
300304
if (any(!good_names)) {
301-
rlang::abort(
302-
glue::glue(
303-
"These argument(s) cannot be used to create the data: ",
304-
glue::glue_collapse(glue::glue("`{names(x)[!good_names]}`"), sep = ", "),
305-
". Possible arguments are: ",
306-
glue::glue_collapse(glue::glue("`{good_args}`"), sep = ", ")
307-
)
305+
cli::cli_abort(
306+
c(
307+
"The argument{?s} {.arg {names(x)[!good_names]}} cannot be used to create
308+
the data.",
309+
"Possible arguments are {.arg {.or {good_args}}."
310+
),
311+
call = call
308312
)
309313
}
310314
invisible(NULL)
@@ -339,18 +343,21 @@ will_make_matrix <- function(y) {
339343
all(can_convert)
340344
}
341345

342-
check_dup_names <- function(x, y) {
346+
check_dup_names <- function(x, y, call = rlang::caller_env()) {
343347
if (is.vector(y))
344348
return(invisible(NULL))
345349

346350
common_names <- intersect(colnames(x), colnames(y))
347-
if (length(common_names) > 0)
348-
rlang::abort(
349-
glue::glue(
350-
"`x` and `y` have at least one name in common: ",
351-
glue::glue_collapse(glue::glue("'{common_names}'"), sep = ", ")
352-
)
351+
if (length(common_names) > 0) {
352+
cli::cli_abort(
353+
c(
354+
"{.arg x} and {.arg y} have the name{?s} {.val {common_names}} in common.",
355+
"i" = "Please ensure that {.arg x} and {.arg y} don't share any column names."
356+
),
357+
call = call
353358
)
359+
}
360+
354361
invisible(NULL)
355362
}
356363

@@ -369,10 +376,11 @@ maybe_matrix <- function(x) {
369376
non_num_cols <- vapply(x, function(x) !is.numeric(x), logical(1))
370377
if (any(non_num_cols)) {
371378
non_num_cols <- names(non_num_cols)[non_num_cols]
372-
non_num_cols <- glue::glue_collapse(glue::single_quote(non_num_cols), sep = ", ")
373-
msg <- glue::glue("Some columns are non-numeric. The data cannot be ",
374-
"converted to numeric matrix: {non_num_cols}.")
375-
rlang::abort(msg)
379+
380+
cli::cli_abort(
381+
"The column{?s} {.val {non_num_cols}} {?is/are} non-numeric, so the
382+
data cannot be converted to a numeric matrix."
383+
)
376384
}
377385
x <- maybe_sparse_matrix(x)
378386
}

0 commit comments

Comments
 (0)