Skip to content
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

Use cli::cli_abort() in all steps #1262

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions R/cut.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,12 @@ create_full_breaks <- function(var, breaks) {
sort(breaks)
}

full_breaks_check <- function(breaks) {
full_breaks_check <- function(breaks, call = rlang::caller_env()) {
if (length(breaks) == 1) {
rlang::abort("In step_cut: variable is invariant and equal to break point.")
cli::cli_abort(
"Variable is invariant and equal to break point.",
call = call
)
}
if (length(breaks) == 2) {
rlang::warn("In step_cut: this will create a factor with one value only.")
Expand Down
60 changes: 39 additions & 21 deletions R/profile.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,27 @@ step_profile <- function(recipe,
trained = FALSE,
skip = FALSE,
id = rand_id("profile")) {
if (pct < 0 | pct > 1) {
rlang::abort("`pct should be on [0, 1]`")
}

check_number_decimal(pct, min = 0, max = 1)

if (length(grid) != 2) {
rlang::abort("`grid` should have two named elements. See ?step_profile")
cli::cli_abort(c(
x = "`grid` should have 2 elements, not {length(grid)}.",
i = "See {.help [?step_profile](recipes::step_profile)} for information."
))
}
if (all(sort(names(grid)) == c("len", "ptcl"))) {
rlang::abort("`grid` should have two named elements. See ?step_profile")
}
if (grid$len < 2) {
rlang::abort("`grid$len should be at least 2.`")
}
if (!is.logical(grid$pctl)) {
rlang::abort("`grid$pctl should be logical.`")
cli::cli_abort(c(
x = "`grid` should have two named elements {.field len} and \\
{.field ptcl}, not {sort(names(grid))}.",
i = "See {.help [?step_profile](recipes::step_profile)} for information."
))
}

check_number_whole(grid$len, min = 2)
check_bool(grid$pctl)


add_step(
recipe,
step_profile_new(
Expand Down Expand Up @@ -168,15 +173,28 @@ prep.step_profile <- function(x, training, info = NULL, ...) {
fixed_names <- recipes_eval_select(x$terms, training, info)
profile_name <- recipes_eval_select(x$profile, training, info)


if (length(profile_name) != 1) {
rlang::abort("Only one variable should be profiled")
msg <- c(x = "{.arg profile} should select only one column")

if (length(profile_name) == 0) {
msg <- c(msg, i = "No columns were selected.")
} else {
msg <- c(
msg,
i = "The {length(profile_name)} columns where selected: \\
{.var {profile_name}}."
)
}

cli::cli_abort(msg)
}

if (any(profile_name == fixed_names)) {
rlang::abort(
paste0(
"The profiled variable cannot be in the list of ",
"variables to be fixed."
)
offenders <- fixed_names[profile_name == fixed_names]
cli::cli_abort(
"The profiled variable cannot be in the list of variables to be \\
fixed. {.var {offenders}} was in both."
)
}
fixed_vals <- lapply(
Expand Down Expand Up @@ -274,10 +292,10 @@ fixed <- function(x, pct, index, ...) UseMethod("fixed")
#' @export
#' @rdname fixed
fixed.default <- function(x, pct, index, ...) {
rlang::abort("No method for determining a value to fix for ",
"objects of class(s) ",
paste0("'", class(x), "'", collapse = ","),
call. = FALSE
classes <- class(x)
cli::cli_abort(
"No method for determining a value to fix for objects of class{?es}: \\
{.cls {classes}}."
)
}
#' @export
Expand Down
43 changes: 23 additions & 20 deletions R/range_check.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,38 +141,41 @@ range_check_func <- function(x,
)
min_x <- min(x)
max_x <- max(x)
msg <- NULL

if (length(slack_prop) == 1) {
lower_allowed <- lower - ((upper - lower) * slack_prop)
upper_allowed <- upper + ((upper - lower) * slack_prop)
} else if (length(slack_prop) == 2) {
lower_allowed <- lower - ((upper - lower) * slack_prop[1])
upper_allowed <- upper + ((upper - lower) * slack_prop[2])
} else {
rlang::abort("slack_prop should be of length 1 or of length 2")
cli::cli_abort(
"{.arg slack_prop} should be of length 1 or 2, not {length(slack_prop)}."
)
}

if (min_x < lower_allowed & max_x > upper_allowed) {
msg <- paste0(
"min ", colname, " is ", min_x, ", lower bound is ",
lower_allowed, ", max x is ", max_x, ", upper bound is ",
upper_allowed
)
} else if (min_x < lower_allowed) {
msg <- paste0(
"min ", colname, " is ", min_x, ", lower bound is ",
lower_allowed
msg <- NULL
if (min_x < lower_allowed) {
msg <- c(
msg,
"Smallest value of {.var {colname}} is {min_x}, \\
crossing the lower bound {lower_allowed}."
)
} else if (max_x > upper_allowed) {
msg <- paste0(
"max ", colname, " is ", max_x, ", upper bound is ",
upper_allowed
}
if (max_x > upper_allowed) {
msg <- c(
msg,
"Largest value of {.var {colname}} is {max_x}, \\
crossing the upper bound {upper_allowed}."
)
}
if (warn & !is.null(msg)) {
rlang::warn(msg)
} else if (!is.null(msg)) {
rlang::abort(msg)

if (!is.null(msg)) {
if (warn) {
cli::cli_warn(msg)
} else {
cli::cli_abort(msg)
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions R/ratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ step_ratio <-
skip = FALSE,
id = rand_id("ratio")) {
if (is_empty(denom)) {
rlang::abort(
paste0(
"Please supply at least one denominator variable specification. ",
"See ?selections."
cli::cli_abort(
c(
"!" = "{.arg denom} must select at least one variable.",
"i" = "See {.help [?selections](recipes::selections)} \\
for more information."
)
)
}
Expand Down
26 changes: 15 additions & 11 deletions R/regex.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ step_regex <- function(recipe,
skip = FALSE,
id = rand_id("regex")) {
if (!is_tune(pattern)) {
if (!is.character(pattern)) {
rlang::abort("`pattern` should be a character string")
}
if (length(pattern) != 1) {
rlang::abort("`pattern` should be a single pattern")
}
check_string(pattern)
}
valid_args <- names(formals(grepl))[-(1:2)]
if (any(!(names(options) %in% valid_args))) {
rlang::abort(paste0(
"Valid options are: ",
paste0(valid_args, collapse = ", ")
cli::cli_abort(c(
"x" = "The following elements of {.arg options} are not allowed:",
"*" = "{.val {setdiff(names(options), valid_args)}}.",
"i" = "Valid options are: {.val {valid_args}}."
))
}

terms <- enquos(...)
if (length(terms) > 1) {
rlang::abort("For this step, at most a single selector can be used.")
cli::cli_abort(c(
x = "For this step, only a single selector can be used.",
i = "The following {length(terms)} selectors were used: \\
{.var {as.character(terms)}}."
))
}

add_step(
Expand Down Expand Up @@ -124,7 +124,11 @@ prep.step_regex <- function(x, training, info = NULL, ...) {
check_type(training[, col_name], types = c("string", "factor", "ordered"))

if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort(c(
x = "The selector should select at most a single variable.",
i = "The following {length(col_name)} were selected: \\
{.and {.var {col_name}}}."
))
}

step_regex_new(
Expand Down
18 changes: 8 additions & 10 deletions R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ prep.step_relevel <- function(x, training, info = NULL, ...) {
# Check to make sure that no ordered levels are provided
order_check <- map_lgl(objects, attr, "is_ordered")
if (any(order_check)) {
rlang::abort(
"Columns contain ordered factors (which cannot be releveled) '",
x$ref_level, "': ",
paste0(names(order_check)[order_check], collapse = ", ")
offenders <- names(order_check)[order_check]
cli::cli_abort(
"Columns contain ordered factors (which cannot be releveled) \\
{.val {x$ref_level}}: {offenders}."
)
}

Expand All @@ -103,12 +103,10 @@ prep.step_relevel <- function(x, training, info = NULL, ...) {
y = x$ref_level
)
if (any(ref_check)) {
rlang::abort(
paste0(
"Columns must contain the reference level '",
x$ref_level, "': ",
paste0(names(ref_check)[ref_check], collapse = ", ")
)
offenders <- names(order_check)[!order_check]
cli::cli_abort(
"The following columns doesn't include required reference level \\
{.val {x$ref_level}}: {.var {offenders}}."
)
}

Expand Down
16 changes: 3 additions & 13 deletions R/relu.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ step_relu <-
skip = FALSE,
id = rand_id("relu")) {
if (!is_tune(shift)) {
if (!is.numeric(shift)) {
rlang::abort("Shift argument must be a numeric value.")
}
check_number_decimal(shift)
}
if (!is_tune(reverse)) {
if (!is.logical(reverse)) {
rlang::abort("Reverse argument must be a logical value.")
}
check_bool(reverse)
}
if (!is_tune(smooth)) {
if (!is.logical(smooth)) {
rlang::abort("Smooth argument must be logical value.")
}
check_bool(smooth)
}
if (reverse & prefix == "right_relu_") {
prefix <- "left_relu_"
Expand Down Expand Up @@ -178,10 +172,6 @@ print.step_relu <-


relu <- function(x, shift = 0, reverse = FALSE, smooth = FALSE) {
if (!is.numeric(x)) {
rlang::abort("step_relu can only be applied to numeric data.")
}

if (reverse) {
shifted <- shift - x
} else {
Expand Down
9 changes: 5 additions & 4 deletions R/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ step_sample <- function(recipe,
}

if (!is_tune(size)) {
if (!is.null(size) & (!is.numeric(size) || size < 0)) {
rlang::abort("`size` should be a positive number or NULL.")
}
check_number_decimal(size, min = 0, allow_null = TRUE)
}
if (!is_tune(replace)) {
if (!is.logical(replace)) {
rlang::abort("`replace` should be a single logical.")
cli::cli_abort(
"{.arg replace} should be a single logical, \\
not {.obj_type_friendly {replace}}."
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/selections.R
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,5 @@ local_current_info <- function(nested_info, frame = parent.frame()) {
#' @export
#' @rdname has_role
current_info <- function() {
cur_info_env %||% rlang::abort("Variable context not set")
cur_info_env %||% cli::cli_abort("Variable context not set.")
}
9 changes: 3 additions & 6 deletions R/string2factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,11 @@ step_string2factor <-
ordered = FALSE,
skip = FALSE,
id = rand_id("string2factor")) {

if (!is_tune(ordered)) {
if (!is.logical(ordered) || length(ordered) != 1) {
rlang::abort("`ordered` should be a single logical variable")
}
}
if ((!is.null(levels) & !is.character(levels)) | is.list(levels)) {
rlang::abort("`levels` should be NULL or a single character vector")
check_bool(ordered)
}
check_character(levels, allow_null = TRUE)

add_step(
recipe,
Expand Down
Loading