Skip to content

Commit ffb7570

Browse files
authored
updated rlang messages to use cli. Fixes #1137 (#1147)
1 parent aa788b8 commit ffb7570

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

R/fit.R

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fit.model_spec <-
111111
...
112112
) {
113113
if (object$mode == "unknown") {
114-
rlang::abort("Please set the mode in the model specification.")
114+
cli::cli_abort("Please set the mode in the model specification.")
115115
}
116116
control <- condense_control(control, control_parsnip())
117117
check_case_weights(case_weights, object)
@@ -145,12 +145,12 @@ fit.model_spec <-
145145
eng_vals <- possible_engines(object)
146146
object$engine <- eng_vals[1]
147147
if (control$verbosity > 0) {
148-
rlang::warn(glue::glue("Engine set to `{object$engine}`."))
148+
cli::cli_warn("Engine set to {.val {object$engine}}.")
149149
}
150150
}
151151

152152
if (all(c("x", "y") %in% names(dots))) {
153-
rlang::abort("`fit.model_spec()` is for the formula methods. Use `fit_xy()` instead.")
153+
cli::cli_abort("`fit.model_spec()` is for the formula methods. Use `fit_xy()` instead.")
154154
}
155155
cl <- match.call(expand.dots = TRUE)
156156
# Create an environment with the evaluated argument objects. This will be
@@ -173,12 +173,11 @@ fit.model_spec <-
173173
check_interface(eval_env$formula, eval_env$data, cl, object)
174174

175175
if (object$engine == "spark" && !inherits(eval_env$data, "tbl_spark"))
176-
rlang::abort(
177-
glue::glue(
178-
"spark objects can only be used with the formula interface to `fit()` ",
179-
"with a spark data object."
176+
cli::cli_abort(
177+
"spark objects can only be used with the formula interface to {.fn fit}
178+
with a spark data object."
180179
)
181-
)
180+
182181

183182
# populate `method` with the details for this model type
184183
object <- add_methods(object, engine = object$engine)
@@ -221,7 +220,7 @@ fit.model_spec <-
221220
...
222221
),
223222

224-
rlang::abort(glue::glue("{interfaces} is unknown."))
223+
cli::cli_abort("{.val {interfaces}} is unknown.")
225224
)
226225
res$censor_probs <- reverse_km(object, eval_env)
227226
model_classes <- class(res$fit)
@@ -243,17 +242,17 @@ fit_xy.model_spec <-
243242
...
244243
) {
245244
if (object$mode == "unknown") {
246-
rlang::abort("Please set the mode in the model specification.")
245+
cli::cli_abort("Please set the mode in the model specification.")
247246
}
248247

249248
if (inherits(object, "surv_reg")) {
250-
rlang::abort("Survival models must use the formula interface.")
249+
cli::cli_abort("Survival models must use the formula interface.")
251250
}
252251

253252
control <- condense_control(control, control_parsnip())
254253

255254
if (is.null(colnames(x))) {
256-
rlang::abort("'x' should have column names.")
255+
cli::cli_abort("{.arg {x}} should have column names.")
257256
}
258257
check_case_weights(case_weights, object)
259258

@@ -262,7 +261,7 @@ fit_xy.model_spec <-
262261
eng_vals <- possible_engines(object)
263262
object$engine <- eng_vals[1]
264263
if (control$verbosity > 0) {
265-
rlang::warn(glue::glue("Engine set to `{object$engine}`."))
264+
cli::cli_warn("Engine set to {.val {object$engine}}.")
266265
}
267266
}
268267
y_var <- colnames(y)
@@ -286,11 +285,8 @@ fit_xy.model_spec <-
286285
fit_interface <- check_xy_interface(eval_env$x, eval_env$y, cl, object)
287286

288287
if (object$engine == "spark")
289-
rlang::abort(
290-
glue::glue(
291-
"spark objects can only be used with the formula interface to `fit()` ",
292-
"with a spark data object."
293-
)
288+
cli::cli_abort(
289+
"spark objects can only be used with the formula interface to {.fn fit} with a spark data object."
294290
)
295291

296292
# populate `method` with the details for this model type
@@ -335,7 +331,7 @@ fit_xy.model_spec <-
335331
control = control,
336332
...
337333
),
338-
rlang::abort(glue::glue("{interfaces} is unknown."))
334+
cli::cli_abort("{.val {interfaces}} is unknown.")
339335
)
340336
res$censor_probs <- reverse_km(object, eval_env)
341337
model_classes <- class(res$fit)
@@ -366,19 +362,16 @@ eval_mod <- function(e, capture = FALSE, catch = FALSE, envir = NULL, ...) {
366362

367363
inher <- function(x, cls, cl) {
368364
if (!is.null(x) && !inherits(x, cls)) {
365+
369366
call <- match.call()
370367
obj <- deparse(call[["x"]])
368+
371369
if (length(cls) > 1)
372-
rlang::abort(
373-
glue::glue(
374-
"`{obj}` should be one of the following classes: ",
375-
glue::glue_collapse(glue::glue("'{cls}'"), sep = ", ")
376-
)
377-
)
370+
cli::cli_abort(
371+
"{.arg {obj}} should be one of the following classes: {.cls {cls}}.")
372+
378373
else
379-
rlang::abort(
380-
glue::glue("`{obj}` should be a {cls} object")
381-
)
374+
cli::cli_abort("{.arg {obj}} should be a {.cls {cls}} object")
382375
}
383376
invisible(x)
384377
}
@@ -394,15 +387,15 @@ check_interface <- function(formula, data, cl, model) {
394387

395388
if (form_interface)
396389
return("formula")
397-
rlang::abort("Error when checking the interface.")
390+
cli::cli_abort("Error when checking the interface.")
398391
}
399392

400393
check_xy_interface <- function(x, y, cl, model) {
401394

402395
sparse_ok <- allow_sparse(model)
403396
sparse_x <- inherits(x, "dgCMatrix")
404397
if (!sparse_ok & sparse_x) {
405-
rlang::abort("Sparse matrices not supported by this model/engine combination.")
398+
cli::cli_abort("Sparse matrices not supported by this model/engine combination.")
406399
}
407400

408401
if (sparse_ok) {
@@ -416,11 +409,8 @@ check_xy_interface <- function(x, y, cl, model) {
416409

417410
# rule out spark data sets that don't use the formula interface
418411
if (inherits(x, "tbl_spark") | inherits(y, "tbl_spark"))
419-
rlang::abort(
420-
glue::glue(
421-
"spark objects can only be used with the formula interface via `fit()` ",
422-
"with a spark data object."
423-
)
412+
cli::cli_abort(
413+
"spark objects can only be used with the formula interface via {.fn fit} with a spark data object."
424414
)
425415

426416

@@ -441,7 +431,7 @@ check_xy_interface <- function(x, y, cl, model) {
441431

442432
check_outcome(y, model)
443433

444-
rlang::abort("Error when checking the interface")
434+
cli::cli_abort("Error when checking the interface")
445435
}
446436

447437
allow_sparse <- function(x) {

0 commit comments

Comments
 (0)