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

rsample fix for rlang::abort as cli_abort for 506 #523

Merged
merged 8 commits into from
Sep 9, 2024
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Started moving error messages to cli (#499, #502).

* Error improvements via cli by @PriKalra (#523).

## Bug fixes

* `vfold_cv()` now utilizes the `breaks` argument correctly for repeated cross-validation (@ZWael, #471).
Expand Down
32 changes: 16 additions & 16 deletions R/initial_validation_split.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,26 @@ initial_validation_split <- function(data,

check_prop_3 <- function(prop, call = rlang::caller_env()) {
if (!is.numeric(prop)) {
rlang::abort("`prop` needs to be numeric.", call = call)
cli_abort("{.arg prop} needs to be numeric.", call = call)
}
if (any(is.na(prop))) {
rlang::abort("`prop` cannot contain `NA`.", call = call)
cli_abort("{.arg prop} cannot contain `NA`.", call = call)
}
if (any(is.null(prop))) {
rlang::abort("`prop` cannot contain `NULL`.", call = call)
cli_abort("{.arg prop} cannot contain `NULL`.", call = call)
}
if (length(prop) != 2L) {
rlang::abort(
"`prop` needs to contain the proportions for training and validation.",
cli_abort(
"{.arg prop} needs to contain the proportions for training and validation.",
call = call
)
}
if (any(!(prop > 0)) | any(!(prop < 1))) {
rlang::abort("Elements of `prop` need to be in (0, 1).", call = call)
cli_abort("Elements of {.arg prop} need to be in (0, 1).", call = call)
}
if (!(sum(prop) > 0 ) | !(sum(prop) < 1) ) {
rlang::abort(
"The sum of the proportions in `prop` needs to be in (0, 1).",
cli_abort(
"The sum of the proportions in {.arg prop} needs to be in (0, 1).",
call = call
)
}
Expand Down Expand Up @@ -303,8 +303,8 @@ validation <- function(x, ...) {
#' @rdname initial_validation_split
validation.default <- function(x, ...) {
cls <- class(x)
cli::cli_abort(
"No method for objects of class{?es}: {cls}"
cli_abort(
"No method for objects of class{?es}: {.cls {cls}}"
)
}

Expand All @@ -321,18 +321,18 @@ validation.initial_validation_split <- function(x, ...) {
#' @export
#' @keywords internal
analysis.initial_validation_split <- function(x, ...) {
rlang::abort(
"The initial validation split does not contain an analysis set.",
i = "You can access the training data with `training()`."
cli_abort(
c("The initial validation split does not contain an analysis set.",
"i" = "You can access the training data with {.fun training}.")
)
}

#' @export
#' @keywords internal
assessment.initial_validation_split <- function(x, ...) {
rlang::abort(
"The initial validation split does not contain an assessment set.",
i = "You can access the testing data with `testing()`."
cli_abort(
c("The initial validation split does not contain an assessment set.",
"i" = "You can access the testing data with {.fun testing}.")
)
}

Expand Down
10 changes: 5 additions & 5 deletions R/make_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ check_prop <- function(prop, replace) {
((prop <= 1 && replace) || (prop < 1 && !replace))
acceptable_prop <- acceptable_prop && prop > 0
if (!acceptable_prop) {
rlang::abort(
"`prop` must be a number between 0 and 1.",
cli_abort(
"{.arg prop} must be a number between 0 and 1.",
call = rlang::caller_env()
)
}
Expand Down Expand Up @@ -345,13 +345,13 @@ validate_group <- function(group, data, call = rlang::caller_env()) {
}

if (is.null(group) || !is.character(group) || length(group) != 1) {
rlang::abort(
"`group` should be a single character value for the column that will be used for splitting.",
cli_abort(
"{.arg group} should be a single character value for the column that will be used for splitting.",
call = call
)
}
if (!any(names(data) == group)) {
rlang::abort("`group` should be a column in `data`.", call = call)
cli_abort("{.arg group} should be a column in {.arg data}.", call = call)
}

group
Expand Down
4 changes: 2 additions & 2 deletions R/mc.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mc_complement <- function(ind, n) {
mc_splits <- function(data, prop = 3 / 4, times = 25,
strata = NULL, breaks = 4, pool = 0.1) {
if (!is.numeric(prop) | prop >= 1 | prop <= 0) {
rlang::abort("`prop` must be a number on (0, 1).")
cli_abort("{.arg prop} must be a number on (0, 1).")
}

n <- nrow(data)
Expand Down Expand Up @@ -244,7 +244,7 @@ group_mc_splits <- function(data, group, prop = 3 / 4, times = 25, strata = NULL

all_assessable <- purrr::map(split_objs, function(x) nrow(assessment(x)))
if (any(all_assessable == 0)) {
rlang::abort(
cli_abort(
c(
"Some assessment sets contained zero rows",
i = "Consider using a non-grouped resampling method"
Expand Down
6 changes: 3 additions & 3 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ nested_cv <- function(data, outside, inside) {

inner_cl <- cl[["inside"]]
if (!is_call(inner_cl)) {
abort(
"`inside` should be a expression such as `vfold()` or ",
"bootstraps(times = 10)` instead of an existing object.",
cli_abort(
"{.arg inside} should be a expression such as {.code vfold()} or
{.code bootstraps(times = 10)} instead of an existing object."
)
}
inside <- map(outside$splits, inside_resample, cl = inner_cl, env = env)
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/initial_validation_split.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Condition
Error in `analysis()`:
! The initial validation split does not contain an analysis set.
i You can access the training data with `training()`.

---

Expand All @@ -13,6 +14,7 @@
Condition
Error in `assessment()`:
! The initial validation split does not contain an assessment set.
i You can access the testing data with `testing()`.

# basic split stratified

Expand Down Expand Up @@ -49,6 +51,7 @@
Condition
Error in `analysis()`:
! The initial validation split does not contain an analysis set.
i You can access the training data with `training()`.

---

Expand All @@ -57,6 +60,7 @@
Condition
Error in `assessment()`:
! The initial validation split does not contain an assessment set.
i You can access the testing data with `testing()`.

# check_prop_3() works

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/nesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
Code
nested_cv(mtcars, outside = vfold_cv(), inside = folds)
Condition
Error in `list2()`:
! Argument 3 can't be empty.
Error in `nested_cv()`:
! `inside` should be a expression such as `vfold()` or `bootstraps(times = 10)` instead of an existing object.

# printing

Expand Down
Loading