From da9325a5784024ab03b6a045f7e97effc3e8764a Mon Sep 17 00:00:00 2001 From: Alex Ho Date: Fri, 19 Sep 2025 11:58:37 -0400 Subject: [PATCH 1/3] Convert errors to use cli. Fixes #90 --- NAMESPACE | 1 - R/C5_rules.R | 4 ++-- R/cubist_rules.R | 6 ++++-- R/rule_fit.R | 40 ++++++++++++++-------------------------- R/rules-package.R | 1 - R/tidy_C5.0.R | 4 ++-- R/tidy_cubist.R | 4 ++-- R/tidy_xrf.R | 6 +++--- 8 files changed, 27 insertions(+), 39 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 5875acd..a3b1664 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,7 +28,6 @@ importFrom(generics,tidy) importFrom(generics,tunable) importFrom(parsnip,multi_predict) importFrom(purrr,map_dfr) -importFrom(rlang,abort) importFrom(rlang,call2) importFrom(rlang,enquos) importFrom(rlang,eval_tidy) diff --git a/R/C5_rules.R b/R/C5_rules.R index 680d1bd..96a33bd 100644 --- a/R/C5_rules.R +++ b/R/C5_rules.R @@ -41,7 +41,7 @@ c5_fit <- function(x, y, trials = 1, minCases = 2, cost = NULL, ...) { if (!is.null(cost)) { if (length(levels(y) != 2)) { - rlang::abort("Cost-sensitive models only implemented for 2 classes.") + cli::cli_abort("Cost-sensitive models only implemented for 2 classes.") } costs <- matrix(c(0, 1, cost, 0), nrow = 2, byrow = TRUE) args$costs <- costs @@ -54,7 +54,7 @@ c5_fit <- function(x, y, trials = 1, minCases = 2, cost = NULL, ...) { c5_pred_wrap <- function(trials = 1, object, new_data, type = "class", ...) { if (length(trials) > 1) { - rlang::abort("`c5_pred_wrap` takes a single value of `trials`") + cli::cli_abort("`c5_pred_wrap` takes a single value of {.arg trials}") } trials[trials < 1] <- 1L trials[trials > 100] <- 100L diff --git a/R/cubist_rules.R b/R/cubist_rules.R index dd00be7..3dc1d55 100644 --- a/R/cubist_rules.R +++ b/R/cubist_rules.R @@ -95,7 +95,7 @@ get_neighbors <- function(x) { cubist_pred_wrap <- function(neighbors = 0, object, new_data, ...) { if (length(neighbors) > 1) { - rlang::abort("`cubist_pred_wrap` takes a single neighbor.") + cli::cli_abort("{.fn cubist_pred_wrap} takes a single neighbor.") } object$spec$args$neighbors <- neighbors res <- predict(object, new_data) @@ -203,7 +203,9 @@ tunable.cubist_rules <- function(x, ...) { multi_predict._cubist <- function(object, new_data, type = NULL, neighbors = NULL, ...) { if (any(names(enquos(...)) == "newdata")) { - rlang::abort("Did you mean to use `new_data` instead of `newdata`?") + cli::cli_abort( + "Did you mean to use {.arg new_data} instead of {.arg newdata}?" + ) } if (is.null(neighbors)) { n <- 1 diff --git a/R/rule_fit.R b/R/rule_fit.R index 5312761..c9abb67 100644 --- a/R/rule_fit.R +++ b/R/rule_fit.R @@ -82,7 +82,7 @@ xrf_fit <- process_mtry <- function(colsample_bytree, counts, n_predictors, is_missing) { if (!is.logical(counts)) { - rlang::abort("'counts' should be a logical value.") + cli::cli_abort("{.arg counts} should be a logical value.") } ineq <- if (counts) { @@ -102,35 +102,21 @@ process_mtry <- function(colsample_bytree, counts, n_predictors, is_missing) { } if ((colsample_bytree < 1 & counts) | (colsample_bytree > 1 & !counts)) { - rlang::abort( - paste0( - "The supplied argument `mtry = ", - colsample_bytree, - "` must be ", - ineq, - " than or equal to 1. \n\n`mtry` is currently being interpreted ", - "as a ", - interp, - " rather than a ", - opp, - ". Supply `counts = ", - !counts, - "` to `set_engine()` to supply this argument as a ", - opp, - " rather than ", - # TODO: add a section to the linked parsnip docs on mtry vs mtry_prop - "a ", - interp, - ". \n\nSee `?details_rule_fit_xrf` for more details." - ), - call = NULL + cli::cli_abort( + c( + "The supplied argument {.arg mtry} must be {ineq} than or equal to 1.", + "i" = "{.arg mtry} is currently being interpreted as a {interp} rather than a {opp}.", + "i" = "Supply `{.arg counts} = {rlang::expr(!counts)}` to `set_engine()` to supply this argument as a {opp} rather than a {interp}.", + "i" = "See {.help details_rule_fit_xrf} for more details.", + call = NULL + ) ) } if (rlang::is_call(colsample_bytree)) { if (rlang::call_name(colsample_bytree) == "tune") { - rlang::abort( - paste0( + cli::cli_abort( + c( "The supplied `mtry` parameter is a call to `tune`. Did you forget ", "to optimize hyperparameters with a tuning function like `tune::tune_grid`?" ), @@ -220,7 +206,9 @@ xrf_pred <- function(object, new_data, lambda = object$fit$lambda, type, ...) { multi_predict._xrf <- function(object, new_data, type = NULL, penalty = NULL, ...) { if (any(names(enquos(...)) == "newdata")) { - rlang::abort("Did you mean to use `new_data` instead of `newdata`?") + cli::cli_abort( + "Did you mean to use {.arg new_data} instead of {.arg newdata}?" + ) } if (is.null(penalty)) { penalty <- object$fit$lambda diff --git a/R/rules-package.R b/R/rules-package.R index 1427c89..36a06dc 100644 --- a/R/rules-package.R +++ b/R/rules-package.R @@ -6,7 +6,6 @@ #' @importFrom dplyr as_tibble #' @importFrom dplyr tibble #' @importFrom purrr map_dfr -#' @importFrom rlang abort #' @importFrom rlang call2 #' @importFrom rlang enquos #' @importFrom rlang eval_tidy diff --git a/R/tidy_C5.0.R b/R/tidy_C5.0.R index b009cf8..01ab4e4 100644 --- a/R/tidy_C5.0.R +++ b/R/tidy_C5.0.R @@ -200,7 +200,7 @@ get_rule_index <- function(index, tree, history = c(), levels) { res <- c(res, new_rules) } else { msg <- paste("Unknown split type", curr$type) - rlang::abort(msg) + cli::cli_abort(msg) } res @@ -231,7 +231,7 @@ get_freqs <- function(rule, tree, lvls) { length(lvls), ")." ) - rlang::abort(msg) + cli::cli_abort(msg) } tibble::tibble(value = lvls, count = freqs) } diff --git a/R/tidy_cubist.R b/R/tidy_cubist.R index f29f66c..ffa4306 100644 --- a/R/tidy_cubist.R +++ b/R/tidy_cubist.R @@ -144,7 +144,7 @@ get_reg_data <- function(txt, results = "expression") { if (length(vals) > 0) { n_elem <- length(vals) if (n_elem %% 2 != 0) { - rlang::abort("number of remaining terms not even", call. = FALSE) + cli::cli_abort("Number of remaining terms is not even.", call. = FALSE) } n_terms <- n_elem / 2 split_terms <- split(vals, rep(1:n_terms, each = 2)) @@ -158,7 +158,7 @@ get_reg_data <- function(txt, results = "expression") { splits_to_coefs <- function(x, int) { num_check <- purrr::map_int(x, length) if (!all(num_check == 2)) { - rlang::abort("Problem with getting coefficients") + cli::cli_abort("Problem with getting coefficients") } coef_val <- purrr::map_dbl(x, \(.x) as.numeric(.x[2])) res <- tibble(term = purrr::map_chr(x, \(.x) .x[1]), estimate = coef_val) diff --git a/R/tidy_xrf.R b/R/tidy_xrf.R index fd0ae2f..4374517 100644 --- a/R/tidy_xrf.R +++ b/R/tidy_xrf.R @@ -5,10 +5,10 @@ tidy.xrf <- function(x, penalty = NULL, unit = c("rules", "columns"), ...) { msg <- "Please choose a single numeric value of 'penalty'." if (is.null(penalty)) { - rlang::abort(msg) + cli::cli_abort(msg) } else { if (!is.numeric(penalty) | length(penalty) != 1) { - rlang::abort(msg) + cli::cli_abort(msg) } } @@ -51,7 +51,7 @@ xrf_coefs <- function(x, penalty = NULL) { penalty <- x$lambda } if (length(penalty) != 1) { - rlang::abort("`penalty` should be a single numeric measure.") + cli::cli_abort("{.arg penalty} should be a single numeric measure.") } lvls <- x$levels From 4cfa9054084bdf08f682d31093cc70a467d61faa Mon Sep 17 00:00:00 2001 From: Alex Ho Date: Fri, 19 Sep 2025 12:30:02 -0400 Subject: [PATCH 2/3] Fix the suggested changes --- R/rule_fit.R | 4 ++-- R/tidy_C5.0.R | 4 ++-- R/tidy_xrf.R | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/rule_fit.R b/R/rule_fit.R index c9abb67..0bbb70e 100644 --- a/R/rule_fit.R +++ b/R/rule_fit.R @@ -104,9 +104,9 @@ process_mtry <- function(colsample_bytree, counts, n_predictors, is_missing) { if ((colsample_bytree < 1 & counts) | (colsample_bytree > 1 & !counts)) { cli::cli_abort( c( - "The supplied argument {.arg mtry} must be {ineq} than or equal to 1.", + "The supplied argument `{.arg mtry} = {colsample_bytree}` must be {ineq} than or equal to 1.", "i" = "{.arg mtry} is currently being interpreted as a {interp} rather than a {opp}.", - "i" = "Supply `{.arg counts} = {rlang::expr(!counts)}` to `set_engine()` to supply this argument as a {opp} rather than a {interp}.", + "i" = "Supply `{.arg counts} = {!counts}` to `set_engine()` to supply this argument as a {opp} rather than a {interp}.", "i" = "See {.help details_rule_fit_xrf} for more details.", call = NULL ) diff --git a/R/tidy_C5.0.R b/R/tidy_C5.0.R index 01ab4e4..0387337 100644 --- a/R/tidy_C5.0.R +++ b/R/tidy_C5.0.R @@ -200,7 +200,7 @@ get_rule_index <- function(index, tree, history = c(), levels) { res <- c(res, new_rules) } else { msg <- paste("Unknown split type", curr$type) - cli::cli_abort(msg) + cli::cli_abort("{msg}") } res @@ -231,7 +231,7 @@ get_freqs <- function(rule, tree, lvls) { length(lvls), ")." ) - cli::cli_abort(msg) + cli::cli_abort("{msg}") } tibble::tibble(value = lvls, count = freqs) } diff --git a/R/tidy_xrf.R b/R/tidy_xrf.R index 4374517..32d8502 100644 --- a/R/tidy_xrf.R +++ b/R/tidy_xrf.R @@ -5,10 +5,10 @@ tidy.xrf <- function(x, penalty = NULL, unit = c("rules", "columns"), ...) { msg <- "Please choose a single numeric value of 'penalty'." if (is.null(penalty)) { - cli::cli_abort(msg) + cli::cli_abort("{msg}") } else { if (!is.numeric(penalty) | length(penalty) != 1) { - cli::cli_abort(msg) + cli::cli_abort("{msg}") } } From 0fd83e7d4778d04aa9c676ee9f11bee10d033844 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Fri, 19 Sep 2025 13:06:31 -0400 Subject: [PATCH 3/3] add cli to imports --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 6d7da7c..609c453 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Depends: parsnip (>= 1.0.3), R (>= 4.1) Imports: + cli, dials (>= 0.1.1.9001), dplyr, generics (>= 0.1.0),