Skip to content

Commit 8ccee92

Browse files
authored
Merge pull request #529 from Dpananos/cli_misc
Adds cli_abort to R/misc.R
2 parents e489196 + ffd7b39 commit 8ccee92

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* The new `inner_split()` function and its methods for various resamples is for usage in tune to create a inner resample of the analysis set to fit the preprocessor and model on one part and the post-processor on the other part (#483, #488, #489).
44

5-
* Started moving error messages to cli (#499, #502). With contributions from @PriKalra (#523, #526, #528, #530, #531, #532), @Dpananos (#516, #517), and @JamesHWade (#518).
5+
* Started moving error messages to cli (#499, #502). With contributions from @PriKalra (#523, #526, #528, #530, #531, #532), @Dpananos (#516, #517, #529), and @JamesHWade (#518).
66

77
* Fixed example for `nested_cv()` (@seb09, #520).
88

R/misc.R

+29-32
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ make_splits <- function(x, ...) {
2424
#' data frame of analysis or training data.
2525
#' @export
2626
make_splits.default <- function(x, ...) {
27-
rlang::abort("There is no method available to make an rsplit from `x`.")
27+
cls <- class(x)
28+
cli_abort("No method for objects of class{?es}: {cls}")
2829
}
2930

3031
#' @rdname make_splits
@@ -47,15 +48,15 @@ make_splits.list <- function(x, data, class = NULL, ...) {
4748
make_splits.data.frame <- function(x, assessment, ...) {
4849
rlang::check_dots_empty()
4950
if (nrow(x) == 0) {
50-
rlang::abort("The analysis set must contain at least one row.")
51+
cli_abort("The analysis set must contain at least one row.")
5152
}
5253

5354
ind_analysis <- seq_len(nrow(x))
5455
if (nrow(assessment) == 0) {
5556
ind_assessment <- integer()
5657
} else {
5758
if (!identical(colnames(x), colnames(assessment))) {
58-
rlang::abort("The analysis and assessment sets must have the same columns.")
59+
cli_abort("The analysis and assessment sets must have the same columns.")
5960
}
6061
ind_assessment <- nrow(x) + seq_len(nrow(assessment))
6162
}
@@ -100,13 +101,13 @@ add_class <- function(x, cls) {
100101
strata_check <- function(strata, data) {
101102
if (!is.null(strata)) {
102103
if (!is.character(strata) | length(strata) != 1) {
103-
rlang::abort("`strata` should be a single name or character value.")
104+
cli_abort("{.arg strata} should be a single name or character value.")
104105
}
105106
if (inherits(data[, strata], "Surv")) {
106-
rlang::abort("`strata` cannot be a `Surv` object. Use the time or event variable directly.")
107+
cli_abort("{.arg strata} cannot be a {.cls Surv} object. Use the time or event variable directly.")
107108
}
108109
if (!(strata %in% names(data))) {
109-
rlang::abort(strata, " is not in `data`.")
110+
cli_abort("{strata} is not in {.arg data}.")
110111
}
111112
}
112113
invisible(NULL)
@@ -148,10 +149,8 @@ split_unnamed <- function(x, f) {
148149
#' @export
149150
#' @rdname get_fingerprint
150151
.get_fingerprint.default <- function(x, ...) {
151-
cls <- paste0("'", class(x), "'", collapse = ", ")
152-
rlang::abort(
153-
paste("No `.get_fingerprint()` method for this class(es)", cls)
154-
)
152+
cls <- class(x)
153+
cli_abort("No method for objects of class{?es}: {cls}")
155154
}
156155

157156
#' @export
@@ -192,16 +191,16 @@ reverse_splits <- function(x, ...) {
192191
#' @rdname reverse_splits
193192
#' @export
194193
reverse_splits.default <- function(x, ...) {
195-
rlang::abort(
196-
"`x` must be either an `rsplit` or an `rset` object"
194+
cli_abort(
195+
"{.arg x} must be either an {.cls rsplit} or an {.cls rset} object."
197196
)
198197
}
199198

200199
#' @rdname reverse_splits
201200
#' @export
202201
reverse_splits.permutations <- function(x, ...) {
203-
rlang::abort(
204-
"Permutations cannot have their splits reversed"
202+
cli_abort(
203+
"Permutations cannot have their splits reversed."
205204
)
206205
}
207206

@@ -253,18 +252,18 @@ reverse_splits.rset <- function(x, ...) {
253252
#' @export
254253
reshuffle_rset <- function(rset) {
255254
if (!inherits(rset, "rset")) {
256-
rlang::abort("`rset` must be an rset object")
255+
cli_abort("{.arg rset} must be an {.cls rset} object.")
257256
}
258257

259258
if (inherits(rset, "manual_rset")) {
260-
rlang::abort("`manual_rset` objects cannot be reshuffled")
259+
cli_abort("{.arg manual_rset} objects cannot be reshuffled.")
261260
}
262261

263262
# non-random classes is defined below
264263
if (any(non_random_classes %in% class(rset))) {
265264
cls <- class(rset)[[1]]
266-
rlang::warn(
267-
glue::glue("`reshuffle_rset()` will return an identical rset when called on {cls} objects")
265+
cli::cli_warn(
266+
"{.fun reshuffle_rset} will return an identical {.cls rset} when called on {.cls {cls}} objects."
268267
)
269268
if ("validation_set" %in% class(rset)) {
270269
return(rset)
@@ -274,10 +273,10 @@ reshuffle_rset <- function(rset) {
274273
rset_type <- class(rset)[[1]]
275274
split_arguments <- .get_split_args(rset)
276275
if (identical(split_arguments$strata, TRUE)) {
277-
rlang::abort(
278-
"Cannot reshuffle this rset (`attr(rset, 'strata')` is `TRUE`, not a column identifier)",
279-
i = "If the original object was created with an older version of rsample, try recreating it with the newest version of the package"
280-
)
276+
cli_abort(c(
277+
"Cannot reshuffle this rset ({.code attr(rset, 'strata')} is {.val TRUE}, not a column identifier)",
278+
i = "If the original object was created with an older version of rsample, try recreating it with the newest version of the package."
279+
))
281280
}
282281

283282
do.call(
@@ -297,8 +296,8 @@ non_random_classes <- c(
297296

298297
#' Get the split arguments from an rset
299298
#' @param x An `rset` or `initial_split` object.
300-
#' @param allow_strata_false A logical to specify which value to use if no
301-
#' stratification was specified. The default is to use `strata = NULL`, the
299+
#' @param allow_strata_false A logical to specify which value to use if no
300+
#' stratification was specified. The default is to use `strata = NULL`, the
302301
#' alternative is `strata = FALSE`.
303302
#' @return A list of arguments used to create the rset.
304303
#' @keywords internal
@@ -315,7 +314,7 @@ non_random_classes <- c(
315314
args <- names(formals(function_used_to_create))
316315
split_args <- all_attributes[args]
317316
split_args <- split_args[!is.na(names(split_args))]
318-
317+
319318
if (identical(split_args$strata, FALSE) && !allow_strata_false) {
320319
split_args$strata <- NULL
321320
}
@@ -361,10 +360,10 @@ get_rsplit.rset <- function(x, index, ...) {
361360
glue::glue("A value of {index} was provided.")
362361
)
363362

364-
rlang::abort(
363+
cli_abort(
365364
c(
366-
glue::glue("`index` must be a length-1 integer between 1 and {n_rows}."),
367-
x = msg
365+
"{.arg index} must be a length-1 integer between 1 and {n_rows}.",
366+
"*" = msg
368367
)
369368
)
370369
}
@@ -375,8 +374,6 @@ get_rsplit.rset <- function(x, index, ...) {
375374
#' @rdname get_rsplit
376375
#' @export
377376
get_rsplit.default <- function(x, index, ...) {
378-
cls <- paste0("'", class(x), "'", collapse = ", ")
379-
rlang::abort(
380-
paste("No `get_rsplit()` method for this class(es)", cls)
381-
)
377+
cls <- class(x)
378+
cli_abort("No method for objects of class{?es}: {cls}")
382379
}

tests/testthat/_snaps/make-splits.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# improper argument
2+
3+
Code
4+
make_splits("potato")
5+
Condition
6+
Error in `make_splits()`:
7+
! No method for objects of class: character
8+

tests/testthat/_snaps/misc.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@
44
reverse_splits(1)
55
Condition
66
Error in `reverse_splits()`:
7-
! `x` must be either an `rsplit` or an `rset` object
7+
! `x` must be either an <rsplit> or an <rset> object.
88

99
---
1010

1111
Code
1212
reverse_splits(permutes)
1313
Condition
1414
Error in `reverse_splits()`:
15-
! Permutations cannot have their splits reversed
15+
! Permutations cannot have their splits reversed.
1616

1717
---
1818

1919
Code
2020
reverse_splits(permutes$splits[[1]])
2121
Condition
2222
Error in `reverse_splits()`:
23-
! Permutations cannot have their splits reversed
23+
! Permutations cannot have their splits reversed.
2424

2525
# reshuffle_rset is working
2626

2727
Code
2828
reshuffle_rset(rset_subclasses[[non_random_classes[[i]]]])
2929
Condition
3030
Warning:
31-
`reshuffle_rset()` will return an identical rset when called on sliding_index objects
31+
`reshuffle_rset()` will return an identical <rset> when called on <sliding_index> objects.
3232
Output
3333
# Sliding index resampling
3434
# A tibble: 49 x 2
@@ -52,7 +52,7 @@
5252
reshuffle_rset(rset_subclasses[[non_random_classes[[i]]]])
5353
Condition
5454
Warning:
55-
`reshuffle_rset()` will return an identical rset when called on sliding_period objects
55+
`reshuffle_rset()` will return an identical <rset> when called on <sliding_period> objects.
5656
Output
5757
# Sliding period resampling
5858
# A tibble: 7 x 2
@@ -72,7 +72,7 @@
7272
reshuffle_rset(rset_subclasses[[non_random_classes[[i]]]])
7373
Condition
7474
Warning:
75-
`reshuffle_rset()` will return an identical rset when called on sliding_window objects
75+
`reshuffle_rset()` will return an identical <rset> when called on <sliding_window> objects.
7676
Output
7777
# Sliding window resampling
7878
# A tibble: 49 x 2
@@ -96,7 +96,7 @@
9696
reshuffle_rset(rset_subclasses[[non_random_classes[[i]]]])
9797
Condition
9898
Warning:
99-
`reshuffle_rset()` will return an identical rset when called on rolling_origin objects
99+
`reshuffle_rset()` will return an identical <rset> when called on <rolling_origin> objects.
100100
Output
101101
# Rolling origin forecast resampling
102102
# A tibble: 45 x 2
@@ -120,7 +120,7 @@
120120
reshuffle_rset(rset_subclasses[[non_random_classes[[i]]]])
121121
Condition
122122
Warning:
123-
`reshuffle_rset()` will return an identical rset when called on validation_time_split objects
123+
`reshuffle_rset()` will return an identical <rset> when called on <validation_time_split> objects.
124124
Output
125125
# Validation Set Split (0.75/0.25)
126126
# A tibble: 1 x 2
@@ -134,7 +134,7 @@
134134
reshuffle_rset(rset_subclasses[[non_random_classes[[i]]]])
135135
Condition
136136
Warning:
137-
`reshuffle_rset()` will return an identical rset when called on validation_set objects
137+
`reshuffle_rset()` will return an identical <rset> when called on <validation_set> objects.
138138
Output
139139
# A tibble: 1 x 2
140140
splits id
@@ -143,15 +143,16 @@
143143

144144
---
145145

146-
Cannot reshuffle this rset (`attr(rset, 'strata')` is `TRUE`, not a column identifier)
146+
Cannot reshuffle this rset (`attr(rset, 'strata')` is "TRUE", not a column identifier)
147+
i If the original object was created with an older version of rsample, try recreating it with the newest version of the package.
147148

148149
---
149150

150-
`manual_rset` objects cannot be reshuffled
151+
`manual_rset` objects cannot be reshuffled.
151152

152153
---
153154

154-
`rset` must be an rset object
155+
`rset` must be an <rset> object.
155156

156157
# get_rsplit()
157158

@@ -160,7 +161,7 @@
160161
Condition
161162
Error in `get_rsplit()`:
162163
! `index` must be a length-1 integer between 1 and 1.
163-
x A value of 3 was provided.
164+
* A value of 3 was provided.
164165

165166
---
166167

@@ -169,7 +170,7 @@
169170
Condition
170171
Error in `get_rsplit()`:
171172
! `index` must be a length-1 integer between 1 and 1.
172-
x Index was of length 2.
173+
* Index was of length 2.
173174

174175
---
175176

@@ -178,13 +179,13 @@
178179
Condition
179180
Error in `get_rsplit()`:
180181
! `index` must be a length-1 integer between 1 and 1.
181-
x A value of 1.5 was provided.
182+
* A value of 1.5 was provided.
182183

183184
---
184185

185186
Code
186187
get_rsplit(warpbreaks, 1)
187188
Condition
188189
Error in `get_rsplit()`:
189-
! No `get_rsplit()` method for this class(es) 'data.frame'
190+
! No method for objects of class: data.frame
190191

tests/testthat/test-make-splits.R

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ test_that("cannot create a split from dataframes with different columns", {
5454
})
5555

5656
test_that("improper argument", {
57-
expect_error(make_splits("potato"), "There is no method available to")
57+
expect_snapshot(error = TRUE, {
58+
make_splits("potato")
59+
})
5860
})

0 commit comments

Comments
 (0)