From 81b64c381b92bd4c6f1f291093f2997009a09aa4 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 27 Jan 2025 14:02:31 -0800 Subject: [PATCH 1/2] rlang::abort() -> cli::abort() --- R/arguments.R | 4 +- R/augment.R | 2 +- R/control.R | 4 +- R/convert_data.R | 30 +++--- R/engines.R | 19 ++-- R/extract.R | 2 +- R/extract_cluster_assignment.R | 26 ++--- R/extract_fit_summary.R | 6 +- R/extract_parameter_set_dials.R | 6 +- R/finalize.R | 4 +- R/fit.R | 31 +++--- R/fit_helpers.R | 2 +- R/hier_clust.R | 2 +- R/k_means.R | 10 +- R/load_ns.R | 3 +- R/metric-aaa.R | 50 +++++----- R/metric-helpers.R | 7 +- R/metric-silhouette.R | 13 ++- R/metric-sse.R | 24 ++--- R/misc.R | 9 +- R/predict.R | 17 ++-- R/predict_helpers.R | 6 +- R/reconcile_clusterings.R | 25 ++--- R/required_pkgs.R | 2 +- R/translate.R | 8 +- R/tunable.R | 18 ++-- R/tune_args.R | 18 ++-- R/tune_cluster.R | 95 ++++++++----------- R/tune_helpers.R | 8 +- tests/testthat/_snaps/cluster_metric_set.md | 17 ++-- tests/testthat/_snaps/engines.md | 3 +- tests/testthat/_snaps/extract_centroids.md | 9 +- .../_snaps/extract_cluster_assignment.md | 9 +- tests/testthat/_snaps/extract_fit_summary.md | 3 +- tests/testthat/_snaps/fiting.md | 4 +- tests/testthat/_snaps/metric-silhouette.md | 6 +- tests/testthat/_snaps/metric-sse.md | 3 +- tests/testthat/_snaps/predict.md | 9 +- .../testthat/_snaps/reconcile_clusterings.md | 2 +- tests/testthat/_snaps/tune_cluster.md | 4 +- tests/testthat/_snaps/workflows.md | 6 +- 41 files changed, 233 insertions(+), 293 deletions(-) diff --git a/R/arguments.R b/R/arguments.R index 3ffafc9f..d977ed06 100644 --- a/R/arguments.R +++ b/R/arguments.R @@ -32,7 +32,7 @@ make_x_call <- function(object, target) { none = rlang::expr(x), data.frame = rlang::expr(maybe_data_frame(x)), matrix = rlang::expr(maybe_matrix(x)), - rlang::abort(glue::glue("Invalid data type target: {target}.")) + cli::cli_abort("Invalid data type target: {target}.") ) fit_call <- make_call( @@ -78,7 +78,7 @@ make_form_call <- function(object, env = NULL) { set_args.cluster_spec <- function(object, ...) { the_dots <- enquos(...) if (length(the_dots) == 0) { - rlang::abort("Please pass at least one named argument.") + cli::cli_abort("Please pass at least one named argument.") } main_args <- names(object$args) new_args <- names(the_dots) diff --git a/R/augment.R b/R/augment.R index ba2c8232..7dd2499b 100644 --- a/R/augment.R +++ b/R/augment.R @@ -31,7 +31,7 @@ augment.cluster_fit <- function(x, new_data, ...) { stats::predict(x, new_data = new_data) ) } else { - rlang::abort(paste("Unknown mode:", x$spec$mode)) + cli::cli_abort("Unknown mode: {x$spec$mode}") } as_tibble(ret) } diff --git a/R/control.R b/R/control.R index 331b8820..ba54b853 100644 --- a/R/control.R +++ b/R/control.R @@ -37,10 +37,10 @@ check_control <- function(x, call = rlang::caller_env()) { abs(x - round(x)) < tol } if (!int_check(x$verbosity)) { - rlang::abort("verbosity should be an integer.", call = call) + cli::cli_abort("verbosity should be an integer.", call = call) } if (!is.logical(x$catch)) { - rlang::abort("catch should be a logical.", call = call) + cli::cli_abort("catch should be a logical.", call = call) } x } diff --git a/R/convert_data.R b/R/convert_data.R index 30e7a009..b502b92b 100644 --- a/R/convert_data.R +++ b/R/convert_data.R @@ -42,7 +42,9 @@ remove_intercept = TRUE ) { if (!(composition %in% c("data.frame", "matrix"))) { - rlang::abort("`composition` should be either 'data.frame' or 'matrix'.") + cli::cli_abort( + "{.arg composition} should be {.cls data.frame} or {.cls matrix}." + ) } ## Assemble model.frame call from call arguments @@ -59,7 +61,7 @@ w <- as.vector(model.weights(mod_frame)) if (!is.null(w) && !is.numeric(w)) { - rlang::abort("`weights` must be a numeric vector") + cli::cli_abort("The {.arg weights} must be a numeric vector.") } # TODO: Do we actually use the offset when fitting? @@ -124,15 +126,11 @@ check_form_dots <- function(x) { good_args <- c("subset", "weights") good_names <- names(x) %in% good_args if (any(!good_names)) { - rlang::abort( - glue::glue( - "These argument(s) cannot be used to create the data: ", - glue::glue_collapse( - glue::glue("`{names(x)[!good_names]}`"), - sep = ", " - ), - ". Possible arguments are: ", - glue::glue_collapse(glue::glue("`{good_args}`"), sep = ", ") + cli::cli_abort( + c( + "The argument{?s} {.code {names(x)[!good_names]}} cannot be used + to create the data.", + "i" = "Possible arguments are: {.code {good_args}}." ) ) } @@ -159,7 +157,7 @@ local_one_hot_contrasts <- function(frame = rlang::caller_env()) { #' @keywords internal .convert_x_to_form_fit <- function(x, weights = NULL, remove_intercept = TRUE) { if (is.vector(x)) { - rlang::abort("`x` cannot be a vector.") + cli::cli_abort("{.arg x} cannot be a vector.") } if (remove_intercept) { @@ -182,10 +180,10 @@ local_one_hot_contrasts <- function(frame = rlang::caller_env()) { if (!is.null(weights)) { if (!is.numeric(weights)) { - rlang::abort("`weights` must be a numeric vector") + cli::cli_abort("The {.arg weights} must be a numeric vector.") } if (length(weights) != nrow(x)) { - rlang::abort(glue::glue("`weights` should have {nrow(x)} elements")) + cli::cli_abort("{.arg weights} should have {nrow(x)} elements.") } } @@ -219,7 +217,9 @@ make_formula <- function(x, short = TRUE) { composition = "data.frame" ) { if (!(composition %in% c("data.frame", "matrix"))) { - rlang::abort("`composition` should be either 'data.frame' or 'matrix'.") + cli::cli_abort( + "{.arg composition} should be either {.code data.frame} or {.code matrix}." + ) } mod_terms <- object$terms diff --git a/R/engines.R b/R/engines.R index e464f61b..dbfc513d 100644 --- a/R/engines.R +++ b/R/engines.R @@ -42,11 +42,15 @@ stop_missing_engine <- function(cls, call = rlang::caller_env()) { .groups = "drop" ) if (nrow(info) == 0) { - rlang::abort(glue::glue("No known engines for `{cls}()`."), call = call) + cli::cli_abort("No known engines for {.fn {cls}}.", call = call) } - msg <- paste0(info$msg, collapse = ", ") - msg <- paste("Missing engine. Possible mode/engine combinations are:", msg) - rlang::abort(msg, call = call) + cli::cli_abort( + c( + "Missing engine.", + "i" = "Possible mode/engine combinations are: {info$msg}." + ), + call = call + ) } load_libs <- function(x, quiet, attach = FALSE) { @@ -86,11 +90,8 @@ check_installs <- function(x, call = rlang::caller_env()) { if (any(!is_inst)) { missing_pkg <- x$method$libs[!is_inst] missing_pkg <- paste0(missing_pkg, collapse = ", ") - rlang::abort( - glue::glue( - "This engine requires some package installs: ", - glue::glue_collapse(glue::glue("'{missing_pkg}'"), sep = ", ") - ), + cli::cli_abort( + "This engine requires installing {.pkg {missing_pkg}}.", call = call ) } diff --git a/R/extract.R b/R/extract.R index 4672516e..f55defc3 100644 --- a/R/extract.R +++ b/R/extract.R @@ -52,5 +52,5 @@ extract_fit_engine.cluster_fit <- function(x, ...) { if (any(names(x) == "fit")) { return(x$fit) } - rlang::abort("Internal error: The model fit does not have an engine fit.") + cli::cli_abort("Internal error: The model fit does not have an engine fit.") } diff --git a/R/extract_cluster_assignment.R b/R/extract_cluster_assignment.R index c975d22e..f530744c 100644 --- a/R/extract_cluster_assignment.R +++ b/R/extract_cluster_assignment.R @@ -69,10 +69,10 @@ extract_cluster_assignment <- function(object, ...) { #' @export extract_cluster_assignment.cluster_spec <- function(object, ...) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } @@ -121,20 +121,20 @@ extract_cluster_assignment.hclust <- function( args <- list(...) if (!is.null(args[["h"]])) { - rlang::abort( - paste( - "Using `h` argument is not supported.", - "Please use `cut_height` instead." + cli::cli_abort( + c( + "Using {.arg h} argument is not supported.", + "i" = "Please use {.arg cut_height} instead." ), call = call ) } if (!is.null(args[["k"]])) { - rlang::abort( - paste( - "Using `k` argument is not supported.", - "Please use `num_clusters` instead." + cli::cli_abort( + c( + "Using {.arg k} argument is not supported.", + "i" = "Please use {.arg num_clusters} instead." ), call = call ) @@ -149,8 +149,8 @@ extract_cluster_assignment.hclust <- function( } if (is.null(num_clusters) && is.null(cut_height)) { - rlang::abort( - "Please specify either `num_clusters` or `cut_height`.", + cli::cli_abort( + "Please specify either {.arg num_clusters} or {.arg cut_height}.", call = call ) } diff --git a/R/extract_fit_summary.R b/R/extract_fit_summary.R index fd787831..905b5184 100644 --- a/R/extract_fit_summary.R +++ b/R/extract_fit_summary.R @@ -28,10 +28,10 @@ extract_fit_summary.cluster_spec <- function( ..., call = rlang::caller_env(n = 0) ) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ), call = call ) diff --git a/R/extract_parameter_set_dials.R b/R/extract_parameter_set_dials.R index 620f8921..7fd4ff48 100644 --- a/R/extract_parameter_set_dials.R +++ b/R/extract_parameter_set_dials.R @@ -36,11 +36,7 @@ eval_call_info <- function(x) { silent = TRUE ) if (inherits(res, "try-error")) { - rlang::abort( - glue::glue( - "Error when calling {x$fun}(): {as.character(res)}" - ) - ) + cli::cli_abort("Error when calling {.fn {x$fun}}: {as.character(res)}") } } else { res <- NA diff --git a/R/finalize.R b/R/finalize.R index ac42bf3e..5e9a2e81 100644 --- a/R/finalize.R +++ b/R/finalize.R @@ -22,7 +22,7 @@ #' @export finalize_model_tidyclust <- function(x, parameters) { if (!inherits(x, "cluster_spec")) { - rlang::abort("`x` should be a tidyclust model specification.") + cli::cli_abort("{.arg x} should be a tidyclust model specification.") } parsnip::check_final_param(parameters) pset <- hardhat::extract_parameter_set_dials(x) @@ -46,7 +46,7 @@ finalize_model_tidyclust <- function(x, parameters) { #' @export finalize_workflow_tidyclust <- function(x, parameters) { if (!inherits(x, "workflow")) { - rlang::abort("`x` should be a workflow") + cli::cli_abort("{.arg x} should be {.obj_type_friendly workflow}") } parsnip::check_final_param(parameters) diff --git a/R/fit.R b/R/fit.R index f5a6cfae..df80391f 100644 --- a/R/fit.R +++ b/R/fit.R @@ -93,7 +93,7 @@ fit.cluster_spec <- function( ... ) { 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 <- parsnip::condense_control(control, control_cluster()) @@ -108,8 +108,8 @@ fit.cluster_spec <- function( } if (all(c("x", "y") %in% names(dots))) { - rlang::abort( - "`fit.cluster_spec()` is for the formula methods. Use `fit_xy()` instead." + cli::cli_abort( + "The {.fn fit.cluster_spec} function is for the formula methods. Use {.fn fit_xy} instead." ) } cl <- match.call(expand.dots = TRUE) @@ -159,7 +159,7 @@ fit.cluster_spec <- function( target = object$method$fit$interface, ... ), - rlang::abort(glue::glue("{interfaces} is unknown.")) + cli::cli_abort("{interfaces} is unknown.") ) model_classes <- class(res$fit) class(res) <- c(paste0("_", model_classes[1]), "cluster_fit") @@ -176,7 +176,7 @@ 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.") } inher <- function(x, cls, cl) { @@ -184,16 +184,9 @@ inher <- function(x, cls, cl) { 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("{.code {obj}} should be {.cls {cls}}.") } else { - rlang::abort( - glue::glue("`{obj}` should be a {cls} object") - ) + cli::cli_abort("{.code {obj}} should be {.obj_type_friendly {cls}}.") } } invisible(x) @@ -241,14 +234,14 @@ fit_xy.cluster_spec <- control <- parsnip::condense_control(control, control_cluster()) if (is.null(colnames(x))) { - rlang::abort("'x' should have column names.") + cli::cli_abort("{.arg x} should have column names.") } if (is.null(object$engine)) { 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 {.code {object$engine}}.") } } @@ -298,7 +291,7 @@ fit_xy.cluster_spec <- control = control, ... ), - rlang::abort(glue::glue("{interfaces} is unknown.")) + cli::cli_abort("{interfaces} is unknown.") ) model_classes <- class(res$fit) class(res) <- c(paste0("_", model_classes[1]), "cluster_fit") @@ -309,7 +302,7 @@ check_x_interface <- function(x, cl, model) { sparse_ok <- allow_sparse(model) sparse_x <- inherits(x, "dgCMatrix") if (!sparse_ok && sparse_x) { - rlang::abort( + cli::cli_abort( "Sparse matrices not supported by this model/engine combination." ) } @@ -334,7 +327,7 @@ check_x_interface <- function(x, cl, model) { if (df_interface) { return("data.frame") } - rlang::abort("Error when checking the interface") + cli::cli_abort("Error when checking the interface") } allow_sparse <- function(x) { diff --git a/R/fit_helpers.R b/R/fit_helpers.R index b2141571..fd52affd 100644 --- a/R/fit_helpers.R +++ b/R/fit_helpers.R @@ -70,7 +70,7 @@ form_x <- function(object, control, env, target = "none", ...) { x_x <- function(object, env, control, target = "none", y = NULL, ...) { if (!is.null(y) && length(y) > 0) { - rlang::abort("Outcomes are not used in `cluster_spec` objects.") + cli::cli_abort("Outcomes are not used in {.cls cluster_spec} objects.") } encoding_info <- modelenv::get_encoding(class(object)[1]) %>% diff --git a/R/hier_clust.R b/R/hier_clust.R index 8dd95a73..762d0075 100644 --- a/R/hier_clust.R +++ b/R/hier_clust.R @@ -156,7 +156,7 @@ check_args.hier_clust <- function(object) { args <- lapply(object$args, rlang::eval_tidy) if (all(is.numeric(args$num_clusters)) && any(args$num_clusters < 0)) { - rlang::abort("The number of centers should be >= 0.") + cli::cli_abort("The number of centers should be >= 0.") } invisible(object) diff --git a/R/k_means.R b/R/k_means.R index b79d3933..024e41c3 100644 --- a/R/k_means.R +++ b/R/k_means.R @@ -133,7 +133,7 @@ check_args.k_means <- function(object) { args <- lapply(object$args, rlang::eval_tidy) if (all(is.numeric(args$num_clusters)) && any(args$num_clusters < 0)) { - rlang::abort("The number of centers should be >= 0.") + cli::cli_abort("The number of centers should be >= 0.") } invisible(object) @@ -186,8 +186,8 @@ check_args.k_means <- function(object) { seed = 1 ) { if (is.null(clusters)) { - rlang::abort( - "Please specify `num_clust` to be able to fit specification.", + cli::cli_abort( + "Please specify {.arg num_clust} to be able to fit specification.", call = call("fit") ) } @@ -229,8 +229,8 @@ check_args.k_means <- function(object) { #' @export .k_means_fit_stats <- function(data, centers = NULL, ...) { if (is.null(centers)) { - rlang::abort( - "Please specify `num_clust` to be able to fit specification.", + cli::cli_abort( + "Please specify {.arg num_clust} to be able to fit specification.", call = call("fit") ) } diff --git a/R/load_ns.R b/R/load_ns.R index 4439d4b3..1bd0a25a 100644 --- a/R/load_ns.R +++ b/R/load_ns.R @@ -28,8 +28,7 @@ load_namespace <- function(x) { did_load <- map_lgl(x, requireNamespace, quietly = TRUE) if (any(!did_load)) { bad <- x[!did_load] - msg <- paste0("'", bad, "'", collapse = ", ") - rlang::abort(paste("These packages could not be loaded:", msg)) + cli::cli_abort("The package{?s} {.pkg {bad}} could not be loaded.") } } diff --git a/R/metric-aaa.R b/R/metric-aaa.R index d32a97bc..d1fe5831 100644 --- a/R/metric-aaa.R +++ b/R/metric-aaa.R @@ -17,7 +17,7 @@ #' @export new_cluster_metric <- function(fn, direction) { if (!is.function(fn)) { - rlang::abort("`fn` must be a function.") + cli::cli_abort("{.arg fn} must be a function.") } direction <- rlang::arg_match( @@ -61,19 +61,17 @@ cluster_metric_set <- function(...) { if (fn_cls == "cluster_metric") { make_cluster_metric_function(fns) } else { - rlang::abort( - paste0( - "Internal error: `validate_function_class()` should have ", - "errored on unknown classes." - ) + cli::cli_abort( + "Internal error: {.fn validate_function_class} should have errored on + unknown classes." ) } } validate_not_empty <- function(x) { if (rlang::is_empty(x)) { - rlang::abort( - "`cluster_metric_set()` requires at least 1 function supplied to `...`." + cli::cli_abort( + "{.fn cluster_metric_set} requires at least 1 function supplied to {.arg ...}." ) } } @@ -84,10 +82,10 @@ validate_inputs_are_functions <- function(fns) { if (!all_fns) { not_fn <- which(!is_fun_vec) not_fn <- paste(not_fn, collapse = ", ") - rlang::abort( - glue::glue( - "All inputs to `cluster_metric_set()` must be functions. ", - "These inputs are not: ({not_fn})." + cli::cli_abort( + c( + "All inputs to {.fn cluster_metric_set} must be functions.", + "i" = "These inputs are not: {not_fn}." ) ) } @@ -96,11 +94,9 @@ validate_inputs_are_functions <- function(fns) { get_quo_label <- function(quo) { out <- rlang::as_label(quo) if (length(out) != 1L) { - rlang::abort( - glue::glue( - "Internal error: ", - "`as_label(quo)` resulted in a character vector of length > 1." - ) + cli::cli_abort( + "Internal error: {.code as_label(quo)} resulted in a character vector + of length > 1." ) } is_namespaced <- grepl("::", out, fixed = TRUE) @@ -113,14 +109,14 @@ get_quo_label <- function(quo) { validate_function_typo <- function(fns, call = rlang::caller_env()) { if (any(map_lgl(fns, identical, silhouette))) { - rlang::abort( - "`silhouette` is not a cluster metric. Did you mean `silhouette_avg`?", + cli::cli_abort( + "The value {.val silhouette} is not a cluster metric. Did you mean {.code silhouette_avg}?", call = call ) } if (any(map_lgl(fns, identical, sse_within))) { - rlang::abort( - "`sse_within_total` is not a cluster metric. Did you mean `sse_within_total`?", + cli::cli_abort( + "{.arg sse_within_total} is not a cluster metric. Did you mean {.code sse_within_total}?", call = call ) } @@ -166,12 +162,10 @@ validate_function_class <- function(fns) { fn_names = fn_bad_names, USE.NAMES = FALSE ) - fn_pastable <- paste0(fn_pastable, collapse = "\n") - rlang::abort( - paste0( - "\nThe combination of metric functions must be:\n", - "- only clustering metrics\n", - "The following metric function types are being mixed:\n", + cli::cli_abort( + c( + "The combination of metric functions must be only clustering metrics.", + "i" = "The following metric function types are being mixed:", fn_pastable ) ) @@ -207,7 +201,7 @@ eval_safely <- function(expr, expr_nm, data = NULL, env = rlang::caller_env()) { rlang::eval_tidy(expr, data = data, env = env) }, error = function(e) { - rlang::abort(paste0("In metric: `", expr_nm, "`\n", conditionMessage(e))) + cli::cli_abort("In metric: {.code {expr_nm}}\n{conditionMessage(e)}") } ) } diff --git a/R/metric-helpers.R b/R/metric-helpers.R index 35572b85..b6d8d736 100644 --- a/R/metric-helpers.R +++ b/R/metric-helpers.R @@ -16,7 +16,7 @@ prep_data_dist <- function( ) { # Sihouettes requires a distance matrix if (is.null(new_data) && is.null(dists)) { - rlang::abort( + cli::cli_abort( "Must supply either a dataset or distance matrix to compute silhouettes." ) } @@ -31,9 +31,9 @@ prep_data_dist <- function( # If they supplied distance, check that it matches the data dimension if (!is.null(dists)) { if (!is.null(new_data) && nrow(new_data) != attr(dists, "Size")) { - rlang::abort("Dimensions of dataset and distance matrix must match.") + cli::cli_abort("Dimensions of dataset and distance matrix must match.") } else if (is.null(new_data) && length(clusters) != attr(dists, "Size")) { - rlang::abort( + cli::cli_abort( "Dimensions of training dataset and distance matrix must match." ) } @@ -75,7 +75,6 @@ get_centroid_dists <- function( } ) { if (ncol(new_data) != ncol(centroids)) { - rlang::abort("Centroids must have same columns as data.") } suppressMessages( diff --git a/R/metric-silhouette.R b/R/metric-silhouette.R index 2722b466..3b4a8ea2 100644 --- a/R/metric-silhouette.R +++ b/R/metric-silhouette.R @@ -30,10 +30,10 @@ silhouette <- function( dist_fun = philentropy::distance ) { if (inherits(object, "cluster_spec")) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } @@ -74,7 +74,6 @@ silhouette <- function( #' @param dist_fun A function for calculating distances between observations. #' Defaults to Euclidean distance on processed data. #' @param ... Other arguments passed to methods. -#' #' @details Not to be confused with [silhouette()] that returns a tibble #' with silhouette for each observation. #' @@ -108,10 +107,10 @@ silhouette_avg <- new_cluster_metric( #' @export #' @rdname silhouette_avg silhouette_avg.cluster_spec <- function(object, ...) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } diff --git a/R/metric-sse.R b/R/metric-sse.R index ac957d78..1760c317 100644 --- a/R/metric-sse.R +++ b/R/metric-sse.R @@ -27,10 +27,10 @@ sse_within <- function( } ) { if (inherits(object, "cluster_spec")) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } @@ -115,10 +115,10 @@ sse_within_total <- new_cluster_metric( #' @export #' @rdname sse_within_total sse_within_total.cluster_spec <- function(object, ...) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } @@ -208,10 +208,10 @@ sse_total <- new_cluster_metric( #' @export #' @rdname sse_total sse_total.cluster_spec <- function(object, ...) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } @@ -319,10 +319,10 @@ sse_ratio <- new_cluster_metric( #' @export #' @rdname sse_ratio sse_ratio.cluster_spec <- function(object, ...) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } diff --git a/R/misc.R b/R/misc.R index d2598ead..1cae8c73 100644 --- a/R/misc.R +++ b/R/misc.R @@ -12,13 +12,10 @@ check_args.default <- function(object) { check_spec_pred_type <- function(object, type) { if (!spec_has_pred_type(object, type)) { possible_preds <- names(object$spec$method$pred) - rlang::abort( + cli::cli_abort( c( - glue::glue("No {type} prediction method available for this model."), - glue::glue( - "Value for `type` should be one of: ", - glue::glue_collapse(glue::glue("'{possible_preds}'"), sep = ", ") - ) + "No {type} prediction method available for this model.", + "i" = "{.arg type} should be one of {.val {possible_preds}}." ) ) } diff --git a/R/predict.R b/R/predict.R index 6b78028b..4d05c4be 100644 --- a/R/predict.R +++ b/R/predict.R @@ -109,7 +109,7 @@ predict.cluster_fit <- function( type, cluster = predict_cluster(object = object, new_data = new_data, ...), raw = predict_raw(object = object, new_data = new_data, opts = opts, ...), - rlang::abort(glue::glue("I don't know about type = '{type}'")) + cli::cli_abort("I don't know about type = {.val {type}}") ) res <- switch(type, cluster = format_cluster(res), res) @@ -122,16 +122,11 @@ check_pred_type <- function(object, type, ...) { switch( object$spec$mode, partition = "cluster", - rlang::abort("`type` should be 'cluster'.") + cli::cli_abort("The {.arg type} argument should be {.val cluster}.") ) } if (!(type %in% pred_types)) { - rlang::abort( - glue::glue( - "`type` should be one of: ", - glue::glue_collapse(pred_types, sep = ", ", last = " and ") - ) - ) + cli::cli_abort("{.arg type} should be {.or {pred_types}}.") } type } @@ -182,10 +177,10 @@ make_pred_call <- function(x) { #' @export predict.cluster_spec <- function(object, ...) { - rlang::abort( - paste( + cli::cli_abort( + c( "This function requires a fitted model.", - "Please use `fit()` on your cluster specification." + "i" = "Please use {.fn fit} on your cluster specification." ) ) } diff --git a/R/predict_helpers.R b/R/predict_helpers.R index e1c13f18..2ad5dabd 100644 --- a/R/predict_helpers.R +++ b/R/predict_helpers.R @@ -167,10 +167,8 @@ make_predictions <- function(x, prefix, n_clusters) { pred_clusts_num <- map_dbl(change_in_ess, which.min) } else { - rlang::abort( - glue::glue( - "linkage_method {linkage_method} is not supported for prediction." - ) + cli::cli_abort( + "linkage_method {.val {linkage_method}} is not supported for prediction." ) } pred_clusts <- unique(clusters$.cluster)[pred_clusts_num] diff --git a/R/reconcile_clusterings.R b/R/reconcile_clusterings.R index a9ba296b..f14ea26c 100644 --- a/R/reconcile_clusterings.R +++ b/R/reconcile_clusterings.R @@ -39,12 +39,9 @@ reconcile_clusterings_mapping <- function( ) { rlang::check_installed("RcppHungarian") if (length(primary) != length(alternative)) { - rlang::abort( - glue::glue( - "`primary` ({length(primary)}) ", - "and `alternative` ({length(alternative)}) ", - "must be the same length." - ) + cli::cli_abort( + "{.arg primary} ({length(primary)}) and {.arg alternative} ({length(alternative)}) + must be the same length." ) } @@ -55,18 +52,14 @@ reconcile_clusterings_mapping <- function( nclust_2 <- length(levels(clusters_2)) if (one_to_one && nclust_1 != nclust_2) { - rlang::abort( - glue::glue( - "For one-to-one matching, must have the same number of clusters in", - "primary and alt." - ) + cli::cli_abort( + "For one-to-one matching, must have the same number of clusters in + primary and alt." ) } else if (nclust_1 > nclust_2) { - rlang::abort( - glue::glue( - "Primary clustering must have equal or fewer clusters to alternate", - "clustering." - ) + cli::cli_abort( + "Primary clustering must have equal or fewer clusters to alternate + clustering." ) } diff --git a/R/required_pkgs.R b/R/required_pkgs.R index ec596cb2..2566fbc4 100644 --- a/R/required_pkgs.R +++ b/R/required_pkgs.R @@ -2,7 +2,7 @@ #' @export required_pkgs.cluster_spec <- function(x, infra = TRUE, ...) { if (is.null(x$engine)) { - rlang::abort("Please set an engine.") + cli::cli_abort("Please set an engine.") } get_pkgs(x, infra) } diff --git a/R/translate.R b/R/translate.R index 24367020..d6e05a97 100644 --- a/R/translate.R +++ b/R/translate.R @@ -40,14 +40,14 @@ translate_tidyclust <- function(x, ...) { translate_tidyclust.default <- function(x, engine = x$engine, ...) { check_empty_ellipse_tidyclust(...) if (is.null(engine)) { - rlang::abort("Please set an engine.") + cli::cli_abort("Please set an engine.") } mod_name <- specific_model(x) x$engine <- engine if (x$mode == "unknown") { - rlang::abort("Model code depends on the mode; please specify one.") + cli::cli_abort("Model code depends on the mode. Please specify one.") } modelenv::check_spec_mode_engine_val( @@ -149,8 +149,8 @@ deharmonize <- function(args, key) { check_empty_ellipse_tidyclust <- function(...) { terms <- quos(...) if (!rlang::is_empty(terms)) { - rlang::abort( - "Please pass other arguments to the model function via `set_engine()`." + cli::cli_abort( + "Please pass other arguments to the model function via {.fn set_engine}." ) } terms diff --git a/R/tunable.R b/R/tunable.R index f4c73427..534cd692 100644 --- a/R/tunable.R +++ b/R/tunable.R @@ -5,23 +5,17 @@ tunable.cluster_spec <- function(x, ...) { mod_env <- rlang::ns_env("modelenv")$modelenv if (is.null(x$engine)) { - rlang::abort( - "Please declare an engine first using `set_engine()`.", - call. = FALSE + cli::cli_abort( + "Please declare an engine first using {.fn set_engine}.", + call = FALSE ) } arg_name <- paste0(mod_type(x), "_args") if (!(any(arg_name == names(mod_env)))) { - rlang::abort( - paste( - "The `tidyclust` model database doesn't know about the arguments for ", - "model `", - mod_type(x), - "`. Was it registered?", - sep = "" - ), - call. = FALSE + cli::cli_abort( + "The {.pkg tidyclust} model database doesn't know about the arguments for + model {.code {mod_type(x)}}. Was it registered?" ) } diff --git a/R/tune_args.R b/R/tune_args.R index 1c275d32..42807b6e 100644 --- a/R/tune_args.R +++ b/R/tune_args.R @@ -77,10 +77,10 @@ find_tune_id <- function(x) { } if (sum(tunable_elems == "", na.rm = TRUE) > 1) { - rlang::abort( - glue::glue( - "Only one tunable value is currently allowed per argument. ", - "The current argument has: `{paste0(deparse(x), collapse = '')}`." + cli::cli_abort( + c( + "Only one tunable value is currently allowed per argument.", + "i" = "The current argument has: {.code {paste0(deparse(x), collapse = '')}}." ) ) } @@ -136,14 +136,8 @@ tune_tbl <- function( complete_id <- id[!is.na(id)] dups <- duplicated(complete_id) if (any(dups)) { - rlang::abort( - paste( - "There are duplicate `id` values listed in [tune()]: ", - paste0("'", unique(complete_id[dups]), "'", collapse = ", "), - ".", - sep = "" - ), - call. = FALSE + cli::cli_abort( + "There are duplicate {.code id} values listed in [{.fn tune}]: {.val {unique(complete_id[dups])}}." ) } diff --git a/R/tune_cluster.R b/R/tune_cluster.R index dd58ce8f..38516bb8 100644 --- a/R/tune_cluster.R +++ b/R/tune_cluster.R @@ -62,11 +62,9 @@ tune_cluster <- function(object, ...) { #' @export tune_cluster.default <- function(object, ...) { - msg <- paste0( - "The first argument to [tune_cluster()] should be either ", - "a model or workflow." + cli::cli_abort( + "The first argument to {.fn tune_cluster} should be either a model or workflow." ) - rlang::abort(msg) } #' @export @@ -82,11 +80,8 @@ tune_cluster.cluster_spec <- function( control = tune::control_grid() ) { if (rlang::is_missing(preprocessor) || !tune::is_preprocessor(preprocessor)) { - rlang::abort( - paste( - "To tune a model spec, you must preprocess", - "with a formula or recipe" - ) + cli::cli_abort( + "To tune a model spec, you must preprocess with a formula or recipe." ) } @@ -130,7 +125,7 @@ tune_cluster.workflow <- function( # Disallow `NULL` grids in `tune_cluster()`, as this is the special signal # used when no tuning is required if (is.null(grid)) { - rlang::abort(grid_msg) + cli::cli_abort(grid_msg) } tune_cluster_workflow( @@ -306,7 +301,7 @@ tune_cluster_loop <- function( ) ) } else { - rlang::abort("Internal error: Invalid `parallel_over`.") + cli::cli_abort("Internal error: Invalid {.arg parallel_over}.") } resamples <- pull_metrics(resamples, results, control) @@ -343,11 +338,10 @@ compute_grid_info <- function(workflow, grid) { if (any_parameters_preprocessor) { compute_grid_info_preprocessor(workflow, grid, parameters_model) } else { - rlang::abort( - paste0( - "Internal error: ", - "`workflow` should have some tunable parameters ", - "if `grid` is not `NULL`." + cli::cli_abort( + c( + "Internal error: {.code workflow} should have some tunable parameters + if {.code grid} is not {.code NULL}." ) ) } @@ -865,14 +859,11 @@ check_metrics <- function(x, object) { x <- cluster_metric_set(sse_within_total, sse_total) }, unknown = { - rlang::abort( - paste0( - "Internal error: ", - "`check_installs()` should have caught an `unknown` mode." - ) + cli::cli_abort( + "Internal error: {.fn check_installs} should have caught an {.code unknown} mode." ) }, - rlang::abort("Unknown `mode` for parsnip model.") + cli::cli_abort("Unknown {.arg mode} for tidyclust model.") ) return(x) @@ -881,11 +872,8 @@ check_metrics <- function(x, object) { is_cluster_metric_set <- inherits(x, "cluster_metric_set") if (!is_cluster_metric_set) { - rlang::abort( - paste0( - "The `metrics` argument should be the results of ", - "[cluster_metric_set()]." - ) + cli::cli_abort( + "The {.arg metrics} argument should be the results of {.fn cluster_metric_set}." ) } x @@ -911,11 +899,11 @@ check_parameters <- function( if (needs_finalization(pset, grid_names)) { if (tune_recipe) { - rlang::abort( - paste( - "Some tuning parameters require finalization but there are recipe", - "parameters that require tuning. Please use `parameters()` to", - "finalize the parameter ranges." + cli::cli_abort( + c( + "Some tuning parameters require finalization but there are recipe + parameters that require tuning.", + "i" = "Please use {.fn parameters} to finalize the parameter ranges." ) ) } @@ -950,15 +938,17 @@ needs_finalization <- function(x, nms = character(0)) { # https://github.com/tidymodels/tune/blob/main/R/checks.R#L274 check_workflow <- function(x, pset = NULL, check_dials = FALSE) { if (!inherits(x, "workflow")) { - rlang::abort("The `object` argument should be a 'workflow' object.") + cli::cli_abort( + "The {.arg object} argument should be a {.cls workflow} object." + ) } if (!has_preprocessor(x)) { - rlang::abort("A formula, recipe, or variables preprocessor is required.") + cli::cli_abort("A formula, recipe, or variables preprocessor is required.") } if (!has_spec(x)) { - rlang::abort("A tidyclust model is required.") + cli::cli_abort("A tidyclust model is required.") } if (check_dials) { @@ -971,11 +961,8 @@ check_workflow <- function(x, pset = NULL, check_dials = FALSE) { incompl <- dials::has_unknowns(pset$object) if (any(incompl)) { - rlang::abort( - paste0( - "The workflow has arguments whose ranges are not finalized: ", - paste0("'", pset$id[incompl], "'", collapse = ", ") - ) + cli::cli_abort( + "The workflow has arguments whose ranges are not finalized: {.arg {pset$id[incompl]}}." ) } } @@ -991,12 +978,8 @@ check_param_objects <- function(pset) { params <- map_lgl(pset$object, inherits, "param") if (!all(params)) { - rlang::abort( - paste0( - "The workflow has arguments to be tuned that are missing some ", - "parameter objects: ", - paste0("'", pset$id[!params], "'", collapse = ", ") - ) + cli::cli_abort( + "The workflow has arguments to be tuned that are missing parameter objects: {.arg {pset$id[!params]}}." ) } invisible(pset) @@ -1029,7 +1012,7 @@ check_grid <- function(grid, workflow, pset = NULL) { if (!is.numeric(grid)) { if (!is.data.frame(grid)) { - rlang::abort(grid_msg) + cli::cli_abort(grid_msg) } grid_distinct <- dplyr::distinct(grid) @@ -1055,29 +1038,25 @@ check_grid <- function(grid, workflow, pset = NULL) { extra_grid_params <- glue::single_quote(extra_grid_params) extra_grid_params <- glue::glue_collapse(extra_grid_params, sep = ", ") - msg <- glue::glue( - "The provided `grid` has the following parameter columns that have ", - "not been marked for tuning by `tune()`: {extra_grid_params}." + cli::cli_abort( + "The provided {.arg grid} has parameter column{?s} {extra_grid_params} + that {?has/have} not been marked for tuning by {.fn tune}." ) - - rlang::abort(msg) } if (length(extra_tune_params) != 0L) { extra_tune_params <- glue::single_quote(extra_tune_params) extra_tune_params <- glue::glue_collapse(extra_tune_params, sep = ", ") - msg <- glue::glue( - "The provided `grid` is missing the following parameter columns that ", - "have been marked for tuning by `tune()`: {extra_tune_params}." + cli::cli_abort( + "The provided {.arg grid} is missing parameter column{?s} {.val {extra_tune_params}} + that {?has/have} been marked for tuning by {.fn tune}." ) - - rlang::abort(msg) } } else { grid <- as.integer(grid[1]) if (grid < 1) { - rlang::abort(grid_msg) + cli::cli_abort(grid_msg) } check_workflow(workflow, pset = pset, check_dials = TRUE) diff --git a/R/tune_helpers.R b/R/tune_helpers.R index 5c387dd8..25f9c30a 100644 --- a/R/tune_helpers.R +++ b/R/tune_helpers.R @@ -331,7 +331,7 @@ merge.cluster_spec <- function(x, y, ...) { merger <- function(x, y, ...) { if (!is.data.frame(y)) { - rlang::abort("The second argument should be a data frame.") + cli::cli_abort("The second argument should be a data frame.") } pset <- hardhat::extract_parameter_set_dials(x) if (nrow(pset) == 0) { @@ -366,7 +366,7 @@ update_model <- function(grid, object, pset, step_id, nms, ...) { param_info <- pset %>% dplyr::filter(id == i & source == "cluster_spec") if (nrow(param_info) > 1) { # TODO figure this out and write a better message - rlang::abort("There are too many things.") + cli::cli_abort("There are too many things.") } if (nrow(param_info) == 1) { if (param_info$component_id == "main") { @@ -406,7 +406,7 @@ catch_and_log_fit <- function(expr, ..., notes) { return(result) } if (!is_workflow(result)) { - rlang::abort("Internal error: Model result is not a workflow!") + cli::cli_abort("Internal error: Model result is not a workflow!") } fit <- result$fit$fit$fit if (is_failure(fit)) { @@ -449,7 +449,7 @@ predict_model <- function(split, workflow, grid, metrics, submodels = NULL) { ) } - rlang::abort(msg) + cli::cli_abort(msg) } # Determine the type of prediction that is required diff --git a/tests/testthat/_snaps/cluster_metric_set.md b/tests/testthat/_snaps/cluster_metric_set.md index a671956a..1b97e4fa 100644 --- a/tests/testthat/_snaps/cluster_metric_set.md +++ b/tests/testthat/_snaps/cluster_metric_set.md @@ -4,8 +4,7 @@ my_metrics(kmeans_fit) Condition Error in `value[[3L]]()`: - ! In metric: `silhouette_avg` - Must supply either a dataset or distance matrix to compute silhouettes. + ! In metric: `silhouette_avg` Must supply either a dataset or distance matrix to compute silhouettes. # cluster_metric_set error with wrong input @@ -13,10 +12,8 @@ cluster_metric_set(mean) Condition Error in `validate_function_class()`: - ! - The combination of metric functions must be: - - only clustering metrics - The following metric function types are being mixed: + ! The combination of metric functions must be only clustering metrics. + i The following metric function types are being mixed: - other (mean ) --- @@ -25,10 +22,8 @@ cluster_metric_set(sse_ratio, mean) Condition Error in `validate_function_class()`: - ! - The combination of metric functions must be: - - only clustering metrics - The following metric function types are being mixed: + ! The combination of metric functions must be only clustering metrics. + i The following metric function types are being mixed: - cluster (sse_ratio) - other (mean ) @@ -38,7 +33,7 @@ cluster_metric_set(silhouette) Condition Error in `cluster_metric_set()`: - ! `silhouette` is not a cluster metric. Did you mean `silhouette_avg`? + ! The value "silhouette" is not a cluster metric. Did you mean `silhouette_avg`? --- diff --git a/tests/testthat/_snaps/engines.md b/tests/testthat/_snaps/engines.md index d5685e00..43ba20b2 100644 --- a/tests/testthat/_snaps/engines.md +++ b/tests/testthat/_snaps/engines.md @@ -4,5 +4,6 @@ set_engine(k_means()) Condition Error in `set_engine()`: - ! Missing engine. Possible mode/engine combinations are: partition {stats, ClusterR, clustMixType, klaR} + ! Missing engine. + i Possible mode/engine combinations are: partition {stats, ClusterR, clustMixType, klaR}. diff --git a/tests/testthat/_snaps/extract_centroids.md b/tests/testthat/_snaps/extract_centroids.md index 3003671c..c2b75e8c 100644 --- a/tests/testthat/_snaps/extract_centroids.md +++ b/tests/testthat/_snaps/extract_centroids.md @@ -4,7 +4,8 @@ extract_centroids(spec) Condition Error in `extract_centroids()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. # extract_centroids() errors for hier_clust() with missing args @@ -20,7 +21,8 @@ hclust_fit %>% extract_centroids(k = 3) Condition Error in `extract_centroids()`: - ! Using `k` argument is not supported. Please use `num_clusters` instead. + ! Using `k` argument is not supported. + i Please use `num_clusters` instead. # extract_centroids() errors for hier_clust() with h arg @@ -28,5 +30,6 @@ hclust_fit %>% extract_centroids(h = 3) Condition Error in `extract_centroids()`: - ! Using `h` argument is not supported. Please use `cut_height` instead. + ! Using `h` argument is not supported. + i Please use `cut_height` instead. diff --git a/tests/testthat/_snaps/extract_cluster_assignment.md b/tests/testthat/_snaps/extract_cluster_assignment.md index d1381c37..fdb378cb 100644 --- a/tests/testthat/_snaps/extract_cluster_assignment.md +++ b/tests/testthat/_snaps/extract_cluster_assignment.md @@ -4,7 +4,8 @@ extract_cluster_assignment(spec) Condition Error in `extract_cluster_assignment()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. # extract_cluster_assignment() errors for hier_clust() with missing args @@ -20,7 +21,8 @@ hclust_fit %>% extract_cluster_assignment(k = 3) Condition Error in `extract_cluster_assignment()`: - ! Using `k` argument is not supported. Please use `num_clusters` instead. + ! Using `k` argument is not supported. + i Please use `num_clusters` instead. # extract_cluster_assignment() errors for hier_clust() with h arg @@ -28,5 +30,6 @@ hclust_fit %>% extract_cluster_assignment(h = 3) Condition Error in `extract_cluster_assignment()`: - ! Using `h` argument is not supported. Please use `cut_height` instead. + ! Using `h` argument is not supported. + i Please use `cut_height` instead. diff --git a/tests/testthat/_snaps/extract_fit_summary.md b/tests/testthat/_snaps/extract_fit_summary.md index 42b14a91..2828d5b7 100644 --- a/tests/testthat/_snaps/extract_fit_summary.md +++ b/tests/testthat/_snaps/extract_fit_summary.md @@ -4,5 +4,6 @@ extract_fit_summary(spec) Condition Error in `extract_fit_summary()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. diff --git a/tests/testthat/_snaps/fiting.md b/tests/testthat/_snaps/fiting.md index 73345116..534d0770 100644 --- a/tests/testthat/_snaps/fiting.md +++ b/tests/testthat/_snaps/fiting.md @@ -4,7 +4,7 @@ k_means(num_clusters = 5) %>% fit_xy(mtcars, y = mtcars$mpg) Condition Error in `x_x()`: - ! Outcomes are not used in `cluster_spec` objects. + ! Outcomes are not used in objects. --- @@ -12,5 +12,5 @@ workflows::workflow(mpg ~ ., km) %>% fit(mtcars) Condition Error in `x_x()`: - ! Outcomes are not used in `cluster_spec` objects. + ! Outcomes are not used in objects. diff --git a/tests/testthat/_snaps/metric-silhouette.md b/tests/testthat/_snaps/metric-silhouette.md index 155ad10b..a76c1ce4 100644 --- a/tests/testthat/_snaps/metric-silhouette.md +++ b/tests/testthat/_snaps/metric-silhouette.md @@ -4,7 +4,8 @@ silhouette(spec) Condition Error in `silhouette()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. # silhouette_avg() errors for cluster spec @@ -12,5 +13,6 @@ silhouette_avg(spec) Condition Error in `silhouette_avg()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. diff --git a/tests/testthat/_snaps/metric-sse.md b/tests/testthat/_snaps/metric-sse.md index c8a3b91e..fbbceb28 100644 --- a/tests/testthat/_snaps/metric-sse.md +++ b/tests/testthat/_snaps/metric-sse.md @@ -28,5 +28,6 @@ sse_ratio(spec) Condition Error in `sse_ratio()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. diff --git a/tests/testthat/_snaps/predict.md b/tests/testthat/_snaps/predict.md index 4fd978b6..f5c91b90 100644 --- a/tests/testthat/_snaps/predict.md +++ b/tests/testthat/_snaps/predict.md @@ -4,7 +4,8 @@ predict(spec) Condition Error in `predict()`: - ! This function requires a fitted model. Please use `fit()` on your cluster specification. + ! This function requires a fitted model. + i Please use `fit()` on your cluster specification. # predict() errors for hier_clust() with missing args @@ -20,7 +21,8 @@ hclust_fit %>% predict(mtcars, k = 3) Condition Error in `predict()`: - ! Using `k` argument is not supported. Please use `num_clusters` instead. + ! Using `k` argument is not supported. + i Please use `num_clusters` instead. # predict() errors for hier_clust() with h arg @@ -28,5 +30,6 @@ hclust_fit %>% predict(mtcars, h = 3) Condition Error in `predict()`: - ! Using `h` argument is not supported. Please use `cut_height` instead. + ! Using `h` argument is not supported. + i Please use `cut_height` instead. diff --git a/tests/testthat/_snaps/reconcile_clusterings.md b/tests/testthat/_snaps/reconcile_clusterings.md index 77bd9932..24904d06 100644 --- a/tests/testthat/_snaps/reconcile_clusterings.md +++ b/tests/testthat/_snaps/reconcile_clusterings.md @@ -5,7 +5,7 @@ alt_cluster_assignment, one_to_one = TRUE) Condition Error in `reconcile_clusterings_mapping()`: - ! For one-to-one matching, must have the same number of clusters inprimary and alt. + ! For one-to-one matching, must have the same number of clusters in primary and alt. # reconciliation errors for uneven lengths diff --git a/tests/testthat/_snaps/tune_cluster.md b/tests/testthat/_snaps/tune_cluster.md index 4e0a9fcf..93c2264b 100644 --- a/tests/testthat/_snaps/tune_cluster.md +++ b/tests/testthat/_snaps/tune_cluster.md @@ -94,7 +94,7 @@ rsample::vfold_cv(mtcars, v = 2)) Condition Error in `tune_cluster()`: - ! The first argument to [tune_cluster()] should be either a model or workflow. + ! The first argument to `tune_cluster()` should be either a model or workflow. # argument order gives errors for formula @@ -103,7 +103,7 @@ mtcars, v = 2)) Condition Error in `tune_cluster()`: - ! The first argument to [tune_cluster()] should be either a model or workflow. + ! The first argument to `tune_cluster()` should be either a model or workflow. # ellipses with tune_cluster diff --git a/tests/testthat/_snaps/workflows.md b/tests/testthat/_snaps/workflows.md index 01de672d..e8898e3f 100644 --- a/tests/testthat/_snaps/workflows.md +++ b/tests/testthat/_snaps/workflows.md @@ -4,7 +4,7 @@ fit(wf_spec, data = mtcars) Condition Error in `x_x()`: - ! Outcomes are not used in `cluster_spec` objects. + ! Outcomes are not used in objects. # integrates with workflows::add_formula() @@ -12,7 +12,7 @@ fit(wf_spec, data = mtcars) Condition Error in `x_x()`: - ! Outcomes are not used in `cluster_spec` objects. + ! Outcomes are not used in objects. # integrates with workflows::add_recipe() @@ -20,5 +20,5 @@ fit(wf_spec, data = mtcars) Condition Error in `x_x()`: - ! Outcomes are not used in `cluster_spec` objects. + ! Outcomes are not used in objects. From 13f30dd93a2012675a6063e6f608534f32618291 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 27 Jan 2025 14:11:54 -0800 Subject: [PATCH 2/2] convert to cli_warn() --- R/arguments.R | 7 ++----- R/fit.R | 5 +++-- R/predict.R | 4 +++- R/predict_cluster.R | 2 +- R/predict_raw.R | 2 +- R/tune_cluster.R | 20 +++++++++++++------- R/tune_helpers.R | 19 ++++--------------- tests/testthat/_snaps/tune_cluster.md | 3 ++- 8 files changed, 29 insertions(+), 33 deletions(-) diff --git a/R/arguments.R b/R/arguments.R index d977ed06..0c2df94f 100644 --- a/R/arguments.R +++ b/R/arguments.R @@ -8,11 +8,8 @@ check_eng_args <- function(args, obj, core_args) { if (length(common_args) > 0) { args <- args[!(names(args) %in% common_args)] common_args <- paste0(common_args, collapse = ", ") - rlang::warn( - glue::glue( - "The following arguments cannot be manually modified ", - "and were removed: {common_args}." - ) + cli::cli_warn( + "The arguments {common_args} cannot be manually modified and were removed." ) } args diff --git a/R/fit.R b/R/fit.R index df80391f..635f2d0d 100644 --- a/R/fit.R +++ b/R/fit.R @@ -103,13 +103,14 @@ fit.cluster_spec <- function( 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 {.code {object$engine}}.") } } if (all(c("x", "y") %in% names(dots))) { cli::cli_abort( - "The {.fn fit.cluster_spec} function is for the formula methods. Use {.fn fit_xy} instead." + "The {.fn fit.cluster_spec} function is for the formula methods. + Use {.fn fit_xy} instead." ) } cl <- match.call(expand.dots = TRUE) diff --git a/R/predict.R b/R/predict.R index 4d05c4be..595a81e4 100644 --- a/R/predict.R +++ b/R/predict.R @@ -96,7 +96,9 @@ predict.cluster_fit <- function( ... ) { if (inherits(object$fit, "try-error")) { - rlang::warn("Model fit failed; cannot make predictions.") + cli::cli_warn( + "Model fit failed; cannot make predictions." + ) return(NULL) } diff --git a/R/predict_cluster.R b/R/predict_cluster.R index ceee4988..75646b5e 100644 --- a/R/predict_cluster.R +++ b/R/predict_cluster.R @@ -23,7 +23,7 @@ predict_cluster.cluster_fit <- function(object, new_data, ...) { check_spec_pred_type(object, "cluster") if (inherits(object$fit, "try-error")) { - rlang::warn("Model fit failed; cannot make predictions.") + cli::cli_warn("Model fit failed; cannot make predictions.") return(NULL) } diff --git a/R/predict_raw.R b/R/predict_raw.R index 6cf59f9e..4d64b59c 100644 --- a/R/predict_raw.R +++ b/R/predict_raw.R @@ -16,7 +16,7 @@ predict_raw.cluster_fit <- function(object, new_data, opts = list(), ...) { check_spec_pred_type(object, "raw") if (inherits(object$fit, "try-error")) { - rlang::warn("Cluster fit failed; cannot make predictions.") + cli::cli_warn("Cluster fit failed; cannot make predictions.") return(NULL) } diff --git a/R/tune_cluster.R b/R/tune_cluster.R index 38516bb8..89680836 100644 --- a/R/tune_cluster.R +++ b/R/tune_cluster.R @@ -182,7 +182,12 @@ tune_cluster_workflow <- function( ) if (is_cataclysmic(resamples)) { - rlang::warn("All models failed. See the `.notes` column.") + cli::cli_warn( + c( + "All models failed.", + "i" = "See the {.code .notes} column." + ) + ) } workflow <- set_workflow(workflow, control) @@ -999,12 +1004,13 @@ check_grid <- function(grid, workflow, pset = NULL) { } if (nrow(pset) == 0L) { - msg <- paste0( - "No tuning parameters have been detected, ", - "performance will be evaluated using the resamples with no tuning. ", - "Did you want to [tune()] parameters?" + cli::cli_warn( + c( + "No tuning parameters have been detected, performance will be evaluated using + the resamples with no tuning.", + "i" = "Did you want to {.fn tune} parameters?" + ) ) - rlang::warn(msg) # Return `NULL` as the new `grid`, like what is used in `fit_resamples()` return(NULL) @@ -1017,7 +1023,7 @@ check_grid <- function(grid, workflow, pset = NULL) { grid_distinct <- dplyr::distinct(grid) if (!identical(nrow(grid_distinct), nrow(grid))) { - rlang::warn( + cli::cli_warn( "Duplicate rows in grid of tuning combinations found and removed." ) } diff --git a/R/tune_helpers.R b/R/tune_helpers.R index 25f9c30a..9f75615d 100644 --- a/R/tune_helpers.R +++ b/R/tune_helpers.R @@ -31,22 +31,11 @@ set_workflow <- function(workflow, control) { if (!is.null(workflow$pre$actions$recipe)) { w_size <- utils::object.size(workflow$pre$actions$recipe) if (w_size / 1024^2 > 5) { - msg <- paste0( - "The workflow being saved contains a recipe, which is ", - format(w_size, units = "Mb", digits = 2), - " in memory. If this was not intentional, please set the control ", - "setting `save_workflow = FALSE`." + cli::cli_inform( + "The workflow being saved contains a recipe, which is {format(w_size, units = 'Mb', + digits = 2)} in memory. If this was not intentional, please set the control + setting {.code save_workflow = FALSE}." ) - cols <- get_tidyclust_colors() - msg <- strwrap( - msg, - prefix = paste0( - cols$symbol$info(cli::symbol$info), - " " - ) - ) - msg <- cols$message$info(paste0(msg, collapse = "\n")) - rlang::inform(msg) } } workflow diff --git a/tests/testthat/_snaps/tune_cluster.md b/tests/testthat/_snaps/tune_cluster.md index 93c2264b..ecd4ede0 100644 --- a/tests/testthat/_snaps/tune_cluster.md +++ b/tests/testthat/_snaps/tune_cluster.md @@ -85,7 +85,8 @@ ! The following predi... Condition Warning: - All models failed. See the `.notes` column. + All models failed. + i See the `.notes` column. # argument order gives errors for recipes