Skip to content

updated rlang messages to use cli. Fixes #1137 #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fit.model_spec <-
...
) {
if (object$mode == "unknown") {
rlang::abort("Please set the mode in the model specification.")
cli::cli_abort("Please set the mode in the model specification.")
}
control <- condense_control(control, control_parsnip())
check_case_weights(case_weights, object)
Expand Down Expand Up @@ -145,12 +145,12 @@ fit.model_spec <-
eng_vals <- possible_engines(object)
object$engine <- eng_vals[1]
if (control$verbosity > 0) {
rlang::warn(glue::glue("Engine set to `{object$engine}`."))
cli::cli_warn("Engine set to {.val {object$engine}}.")
}
}

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

if (object$engine == "spark" && !inherits(eval_env$data, "tbl_spark"))
rlang::abort(
glue::glue(
"spark objects can only be used with the formula interface to `fit()` ",
"with a spark data object."
cli::cli_abort(
"spark objects can only be used with the formula interface to {.fn fit}
with a spark data object."
)
)


# populate `method` with the details for this model type
object <- add_methods(object, engine = object$engine)
Expand Down Expand Up @@ -221,7 +220,7 @@ fit.model_spec <-
...
),

rlang::abort(glue::glue("{interfaces} is unknown."))
cli::cli_abort("{.val {interfaces}} is unknown.")
)
res$censor_probs <- reverse_km(object, eval_env)
model_classes <- class(res$fit)
Expand All @@ -243,17 +242,17 @@ fit_xy.model_spec <-
...
) {
if (object$mode == "unknown") {
rlang::abort("Please set the mode in the model specification.")
cli::cli_abort("Please set the mode in the model specification.")
}

if (inherits(object, "surv_reg")) {
rlang::abort("Survival models must use the formula interface.")
cli::cli_abort("Survival models must use the formula interface.")
}

control <- condense_control(control, control_parsnip())

if (is.null(colnames(x))) {
rlang::abort("'x' should have column names.")
cli::cli_abort("{.arg {x}} should have column names.")
}
check_case_weights(case_weights, object)

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

if (object$engine == "spark")
rlang::abort(
glue::glue(
"spark objects can only be used with the formula interface to `fit()` ",
"with a spark data object."
)
cli::cli_abort(
"spark objects can only be used with the formula interface to {.fn fit} with a spark data object."
)

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

inher <- function(x, cls, cl) {
if (!is.null(x) && !inherits(x, cls)) {

call <- match.call()
obj <- deparse(call[["x"]])

if (length(cls) > 1)
rlang::abort(
glue::glue(
"`{obj}` should be one of the following classes: ",
glue::glue_collapse(glue::glue("'{cls}'"), sep = ", ")
)
)
cli::cli_abort(
"{.arg {obj}} should be one of the following classes: {.cls {cls}}.")

else
rlang::abort(
glue::glue("`{obj}` should be a {cls} object")
)
cli::cli_abort("{.arg {obj}} should be a {.cls {cls}} object")
}
invisible(x)
}
Expand All @@ -394,15 +387,15 @@ check_interface <- function(formula, data, cl, model) {

if (form_interface)
return("formula")
rlang::abort("Error when checking the interface.")
cli::cli_abort("Error when checking the interface.")
}

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

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

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

# rule out spark data sets that don't use the formula interface
if (inherits(x, "tbl_spark") | inherits(y, "tbl_spark"))
rlang::abort(
glue::glue(
"spark objects can only be used with the formula interface via `fit()` ",
"with a spark data object."
)
cli::cli_abort(
"spark objects can only be used with the formula interface via {.fn fit} with a spark data object."
)


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

check_outcome(y, model)

rlang::abort("Error when checking the interface")
cli::cli_abort("Error when checking the interface")
}

allow_sparse <- function(x) {
Expand Down
Loading