Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Emil Hvitfeldt <[email protected]>
  • Loading branch information
topepo and EmilHvitfeldt authored Oct 19, 2024
1 parent 288f458 commit 29c7d07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 7 additions & 3 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions R/computations.R
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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))

Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 29c7d07

Please sign in to comment.