From 29c7d073ad36464d4aafc81204b2754449966e28 Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Sat, 19 Oct 2024 12:09:31 -0400 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Emil Hvitfeldt --- R/checks.R | 10 +++++++--- R/computations.R | 18 +++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/R/checks.R b/R/checks.R index d3cac52..7447593 100644 --- a/R/checks.R +++ b/R/checks.R @@ -7,7 +7,11 @@ check_numeric <- function(x, input = "`x`", call) { check_categorical <- function(x, call) { if (!is.character(x) & !is.factor(x)) { - cli::cli_abort("{.arg x} should be a character or factor vector.") + cli::cli_abort( + "{.arg x} should be a character or factor vector, + not {.obj_type_friendly {x}}.", + call = call + ) } invisible(NULL) } @@ -65,14 +69,14 @@ is_vector_args <- function(values, d, call) { cli::cli_abort("'d' should be a numeric vector.", call = call) } if (length(values) != length(d)) { - cli::cli_abort("'{.arg values}' and '{.arg d}' should be the same length.", + cli::cli_abort("{.arg values} ({length(values)}) and {.arg d} ({length(d)}) should be the same length.", call = call) } invisible(TRUE) } -check_args <- function(arg, x, use_data, fn, type = "low", call) { +check_args <- function(arg, x, use_data, fn, type = "low", call = rlang::caller_env()) { if (rlang::is_missing(arg)) { if (use_data) { type <- rlang::arg_match0(type, c("low", "high", "target"), error_call = call) diff --git a/R/computations.R b/R/computations.R index d2caaaa..e3050c4 100644 --- a/R/computations.R +++ b/R/computations.R @@ -1,5 +1,5 @@ .comp_max <- function(x, low, high, scale, missing, call) { - check_unit_range(missing) + check_unit_range(missing, call = call) check_numeric(x, call = call) check_value_order(low, high, call = call) @@ -27,9 +27,9 @@ .comp_target <- function(x, low, target, high, scale_low, scale_high, missing, call = rlang::caller_env()) { - check_unit_range(missing) + check_unit_range(missing, call = call) check_numeric(x, call = call) - check_value_order(low, high, target) + check_value_order(low, high, target, call = call) out <- rep(missing, length(x)) @@ -44,13 +44,13 @@ .comp_custom <- function(x, values, d, missing, call = rlang::caller_env()) { - check_unit_range(missing) + check_unit_range(missing, call = call) if (!is.numeric(d) | out_of_unit_range(d)) { cli::cli_abort("Desirability values should be numeric and complete in the range [0, 1].", call = call) } check_numeric(x, call = call) - is_vector_args(values, d) + is_vector_args(values, d, call = call) ord <- order(values) values <- values[ord] @@ -70,8 +70,8 @@ .comp_box <- function(x, low, high, missing, call = rlang::caller_env()) { check_numeric(x, call = call) - check_unit_range(missing) - check_value_order(low, high) + check_unit_range(missing, call = call) + check_value_order(low, high, call = call) out <- rep(missing, length(x)) out[x < low | x > high & !is.na(x)] <- 0 @@ -82,8 +82,8 @@ .comp_category <- function(x, values, missing, call = rlang::caller_env()) { - check_categorical(x) - check_unit_range(missing) + check_categorical(x, call = call) + check_unit_range(missing, call = call) if (!is.numeric(values) | out_of_unit_range(values)) { cli::cli_abort("Desirability values should be numeric and complete in the range [0, 1].", call = call)