Skip to content

Commit cf85be3

Browse files
Merge pull request #153 from tidymodels/use-cli
use cli functions
2 parents e8c9b21 + cc90505 commit cf85be3

30 files changed

+95
-105
lines changed

DESCRIPTION

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ BugReports: https://github.com/tidymodels/themis/issues
2121
Depends:
2222
R (>= 3.6),
2323
recipes (>= 1.1.0.9000)
24-
Imports:
24+
Imports:
25+
cli,
2526
gower,
2627
lifecycle (>= 1.0.3),
2728
dplyr,
@@ -48,4 +49,4 @@ Config/testthat/edition: 3
4849
Encoding: UTF-8
4950
LazyData: true
5051
Roxygen: list(markdown = TRUE)
51-
RoxygenNote: 7.3.1
52+
RoxygenNote: 7.3.2

R/adasyn.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#'
3838
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
3939
#' columns `terms` and `id`:
40-
#'
40+
#'
4141
#' \describe{
4242
#' \item{terms}{character, the selectors or variables selected}
4343
#' \item{id}{character, id of this step}
@@ -151,7 +151,7 @@ step_adasyn_new <-
151151
prep.step_adasyn <- function(x, training, info = NULL, ...) {
152152
col_name <- recipes_eval_select(x$terms, training, info)
153153
if (length(col_name) > 1) {
154-
rlang::abort("The selector should select at most a single variable")
154+
cli::cli_abort("The selector should select at most a single variable.")
155155
}
156156

157157
if (length(col_name) == 1) {

R/adasyn_impl.R

+5-10
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@
3232
#' res <- adasyn(circle_numeric, var = "class", over_ratio = 0.8)
3333
adasyn <- function(df, var, k = 5, over_ratio = 1) {
3434
if (length(var) != 1) {
35-
rlang::abort("Please select a single factor variable for `var`.")
35+
cli::cli_abort("Please select a single factor variable for {.arg var}.")
3636
}
3737

3838
var <- rlang::arg_match(var, colnames(df))
3939

4040
if (!(is.factor(df[[var]]) | is.character(df[[var]]))) {
41-
rlang::abort(glue("`{var}` should be a factor or character variable."))
41+
cli::cli_abort("{.var {var}} should be a factor or character variable.")
4242
}
4343

4444
if (length(k) != 1) {
45-
rlang::abort("`k` must be length 1.")
45+
cli::cli_abort("The {.arg k} argument must be length 1.")
4646
}
4747

4848
if (k < 1) {
49-
rlang::abort("`k` must be non-negative.")
49+
cli::cli_abort("The {.arg k} argument must be non-negative.")
5050
}
5151

5252
predictors <- setdiff(colnames(df), var)
@@ -83,12 +83,7 @@ adasyn_impl <- function(df, var, k = 5, over_ratio = 1, call = caller_env()) {
8383
minority <- data_mat[!min_class_in, , drop = FALSE]
8484

8585
if (nrow(minority) <= k) {
86-
rlang::abort(
87-
glue(
88-
"Not enough observations of '{min_names[i]}' to perform ADASYN."
89-
),
90-
call = call
91-
)
86+
cli::cli_abort("Not enough observations of {.val {min_names[i]}} to perform ADASYN.", call = call)
9287
}
9388

9489
tmp_df <- as.data.frame(

R/bsmote.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#'
6060
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
6161
#' columns `terms` and `id`:
62-
#'
62+
#'
6363
#' \describe{
6464
#' \item{terms}{character, the selectors or variables selected}
6565
#' \item{id}{character, id of this step}
@@ -183,7 +183,7 @@ step_bsmote_new <-
183183
prep.step_bsmote <- function(x, training, info = NULL, ...) {
184184
col_name <- recipes_eval_select(x$terms, training, info)
185185
if (length(col_name) > 1) {
186-
rlang::abort("The selector should select at most a single variable")
186+
cli::cli_abort("The selector should select at most a single variable.")
187187
}
188188

189189
if (length(col_name) == 1) {

R/bsmote_impl.R

+8-7
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@
5757
#' res <- bsmote(circle_numeric, var = "class", all_neighbors = TRUE)
5858
bsmote <- function(df, var, k = 5, over_ratio = 1, all_neighbors = FALSE) {
5959
if (length(var) != 1) {
60-
rlang::abort("Please select a single factor variable for `var`.")
60+
cli::cli_abort("Please select a single factor variable for {.arg var}.")
6161
}
6262

6363
var <- rlang::arg_match(var, colnames(df))
6464

6565
if (!(is.factor(df[[var]]) | is.character(df[[var]]))) {
66-
rlang::abort(glue("{var} should be a factor or character variable."))
66+
cli::cli_abort("{.var {var}} should be a factor or character variable.")
6767
}
6868

6969
if (length(k) != 1) {
70-
rlang::abort("`k` must be length 1.")
70+
cli::cli_abort("{.arg k} must be length 1.")
7171
}
7272

7373
if (k < 1) {
74-
rlang::abort("`k` must be non-negative.")
74+
cli::cli_abort("The {.arg k} argument must be non-negative.")
7575
}
7676

7777
predictors <- setdiff(colnames(df), var)
@@ -100,9 +100,10 @@ bsmote_impl <- function(df, var, k = 5, over_ratio = 1, all_neighbors = FALSE) {
100100
)
101101

102102
if (sum(danger_ids) <= k) {
103-
rlang::abort(glue(
104-
"Not enough danger observations of '{min_names[i]}' to perform BSMOTE."
105-
))
103+
cli::cli_abort(
104+
"Not enough danger observations of {.val {min_names[i]}} to perform
105+
BSMOTE."
106+
)
106107
}
107108

108109
if (all_neighbors == FALSE) {

R/downsample.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#'
5454
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
5555
#' columns `terms` and `id`:
56-
#'
56+
#'
5757
#' \describe{
5858
#' \item{terms}{character, the selectors or variables selected}
5959
#' \item{id}{character, id of this step}
@@ -179,7 +179,7 @@ prep.step_downsample <- function(x, training, info = NULL, ...) {
179179
}
180180

181181
if (length(col_name) > 1) {
182-
rlang::abort("The selector should select at most a single variable")
182+
cli::cli_abort("The selector should select at most a single variable.")
183183
}
184184

185185
if (length(col_name) == 0) {

R/misc.R

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ check_na <- function(data, step, call = caller_env()) {
88
na_cols <- vapply(data, function(x) any(is.na(x)), FUN.VALUE = logical(1))
99
if (any(na_cols)) {
1010
cols <- paste(names(na_cols)[na_cols], collapse = ", ")
11-
rlang::abort(
12-
glue(
13-
"Cannot have any missing values. NAs found ind: {cols}."
14-
),
11+
cli::cli_abort(
12+
"Cannot have any missing values. NAs found in {cols}.",
1513
call = call
1614
)
1715
}
1816
}
1917

2018
check_2_levels_only <- function(data, col_name, call = caller_env()) {
2119
if (length(levels(data[[col_name]])) != 2) {
22-
rlang::abort(glue("`{col_name}` must only have 2 levels."), call = call)
20+
cli::cli_abort("The {.code {col_name}} must only have 2 levels.", call = call)
2321
}
2422
}
2523

@@ -28,14 +26,14 @@ check_numeric <- function(dat) {
2826
label <- "numeric"
2927

3028
if (!all(all_good)) {
31-
rlang::abort("All columns for this function should be numeric.")
29+
cli::cli_abort("All columns for this function should be numeric.")
3230
}
3331
invisible(all_good)
3432
}
3533

3634
check_column_factor <- function(data, column, call = caller_env()) {
3735
if (!is.factor(data[[column]])) {
38-
rlang::abort(glue("`{column}` should be a factor variable."), call = call)
36+
cli::cli_abort("{.code {column}} should be a factor variable.", call = call)
3937
}
4038
}
4139

R/nearmiss.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#'
4141
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
4242
#' columns `terms` and `id`:
43-
#'
43+
#'
4444
#' \describe{
4545
#' \item{terms}{character, the selectors or variables selected}
4646
#' \item{id}{character, id of this step}
@@ -159,7 +159,7 @@ prep.step_nearmiss <- function(x, training, info = NULL, ...) {
159159
col_name <- recipes_eval_select(x$terms, training, info)
160160

161161
if (length(col_name) > 1) {
162-
rlang::abort("The selector should select at most a single variable")
162+
cli::cli_abort("The selector should select at most a single variable.")
163163
}
164164

165165
if (length(col_name) == 1) {

R/nearmiss_impl.R

+5-9
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@
3232
#' res <- nearmiss(circle_numeric, var = "class", under_ratio = 1.5)
3333
nearmiss <- function(df, var, k = 5, under_ratio = 1) {
3434
if (length(var) != 1) {
35-
rlang::abort("Please select a single factor variable for `var`.")
35+
cli::cli_abort("Please select a single factor variable for {.arg var}.")
3636
}
3737

3838
var <- rlang::arg_match(var, colnames(df))
3939

4040
if (!(is.factor(df[[var]]) | is.character(df[[var]]))) {
41-
rlang::abort(glue("`{var}` should be a factor or character variable."))
41+
cli::cli_abort("{.var {var}} should be a factor or character variable.")
4242
}
4343

4444
if (length(k) != 1) {
45-
rlang::abort("`k` must be length 1.")
45+
cli::cli_abort("The {.arg k} argument must have length 1.")
4646
}
4747

4848
if (k < 1) {
49-
rlang::abort("`k` must be non-negative.")
49+
cli::cli_abort("The {.arg k} argument must be non-negative.")
5050
}
5151

5252
predictors <- setdiff(colnames(df), var)
@@ -69,11 +69,7 @@ nearmiss_impl <- function(df, var, ignore_vars, k = 5, under_ratio = 1) {
6969
not_class <- subset_to_matrix(df_only, var, names(classes)[i], FALSE)
7070

7171
if (nrow(not_class) <= k) {
72-
rlang::abort(
73-
glue(
74-
"Not enough danger observations of '{names(classes)[i]}' to perform NEARMISS."
75-
)
76-
)
72+
cli::cli_abort("Not enough danger observations of {.val {names(classes)[i]}} to perform NEARMISS.")
7773
}
7874

7975
dists <- RANN::nn2(

R/rose.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#'
5353
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
5454
#' columns `terms` and `id`:
55-
#'
55+
#'
5656
#' \describe{
5757
#' \item{terms}{character, the selectors or variables selected}
5858
#' \item{id}{character, id of this step}
@@ -168,7 +168,7 @@ step_rose_new <-
168168
prep.step_rose <- function(x, training, info = NULL, ...) {
169169
col_name <- recipes_eval_select(x$terms, training, info)
170170
if (length(col_name) > 1) {
171-
rlang::abort("The selector should select at most a single variable")
171+
cli::cli_abort("The selector should select at most a single variable.")
172172
}
173173
if (length(col_name) == 1) {
174174
check_column_factor(training, col_name)

R/smote.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#'
4545
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
4646
#' columns `terms` and `id`:
47-
#'
47+
#'
4848
#' \describe{
4949
#' \item{terms}{character, the selectors or variables selected}
5050
#' \item{id}{character, id of this step}
@@ -157,7 +157,7 @@ step_smote_new <-
157157
prep.step_smote <- function(x, training, info = NULL, ...) {
158158
col_name <- recipes_eval_select(x$terms, training, info)
159159
if (length(col_name) > 1) {
160-
rlang::abort("The selector should select at most a single variable")
160+
cli::cli_abort("The selector should select at most a single variable.")
161161
}
162162

163163
if (length(col_name) == 1) {

R/smote_impl.R

+5-10
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@
4040
#' res <- smote(circle_numeric, var = "class", over_ratio = 0.8)
4141
smote <- function(df, var, k = 5, over_ratio = 1) {
4242
if (length(var) != 1) {
43-
rlang::abort("Please select a single factor variable for `var`.")
43+
cli::cli_abort("Please select a single factor variable for {.arg var}.")
4444
}
4545

4646
var <- rlang::arg_match(var, colnames(df))
4747

4848
if (!(is.factor(df[[var]]) | is.character(df[[var]]))) {
49-
rlang::abort(glue("`{var}` should be a factor or character variable."))
49+
cli::cli_abort("{.var {var}} should be a factor or character variable.")
5050
}
5151

5252
if (length(k) != 1) {
53-
rlang::abort("`k` must be length 1.")
53+
cli::cli_abort("The {.arg k} must be length 1.")
5454
}
5555

5656
if (k < 1) {
57-
rlang::abort("`k` must be non-negative.")
57+
cli::cli_abort("{.arg k} must be non-negative.")
5858
}
5959

6060
predictors <- setdiff(colnames(df), var)
@@ -80,12 +80,7 @@ smote_impl <- function(df, var, k, over_ratio, call = caller_env()) {
8080
minority <- as.matrix(minority_df[names(minority_df) != var])
8181

8282
if (nrow(minority) <= k) {
83-
rlang::abort(
84-
glue(
85-
"Not enough observations of '{min_names[i]}' to perform SMOTE."
86-
),
87-
call = call
88-
)
83+
cli::cli_abort("Not enough observations of {.val {min_names[i]}} to perform SMOTE.", call = call)
8984
}
9085

9186
synthetic <- smote_data(minority, k = k, n_samples = samples_needed[i])

R/smotenc.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#'
4747
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
4848
#' columns `terms` and `id`:
49-
#'
49+
#'
5050
#' \describe{
5151
#' \item{terms}{character, the selectors or variables selected}
5252
#' \item{id}{character, id of this step}
@@ -143,7 +143,7 @@ step_smotenc_new <-
143143
prep.step_smotenc <- function(x, training, info = NULL, ...) {
144144
col_name <- recipes_eval_select(x$terms, training, info)
145145
if (length(col_name) > 1) {
146-
rlang::abort("The selector should select at most a single variable")
146+
cli::cli_abort("The selector should select at most a single variable.")
147147
}
148148

149149
if (length(col_name) == 1) {

R/smotenc_impl.R

+9-8
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,23 @@ smotenc <- function(df, var, k = 5, over_ratio = 1) {
4747
# the input variables need to be numeric and contain no NA values
4848

4949
if (length(var) != 1) {
50-
rlang::abort("Please select a single factor variable for `var`.")
50+
cli::cli_abort("Please select a single factor variable for {.arg var}.")
5151
}
5252

5353
var <- rlang::arg_match(var, colnames(df))
5454

5555
if (!(is.factor(df[[var]]) | is.character(df[[var]]))) {
56-
rlang::abort(paste0(var, " should be a factor or character variable."))
56+
cli::cli_abort(
57+
"{var} should be {.cls factor} or {.cls character} variable."
58+
)
5759
}
5860

5961
if (length(k) != 1) {
60-
rlang::abort("`k` must be length 1.")
62+
cli::cli_abort("The {.arg k} must have length 1.")
6163
}
6264

6365
if (k < 1) {
64-
rlang::abort("`k` must be non-negative.")
66+
cli::cli_abort("The {.arg k} argument must be non-negative.")
6567
}
6668

6769
check_na(select(df, -all_of(var)))
@@ -99,10 +101,9 @@ smotenc_impl <- function(df, var, k, over_ratio) {
99101

100102
# Ensure that we have more minority isntances than desired neighbors
101103
if (nrow(minority) <= k) {
102-
rlang::abort(paste0(
103-
"Not enough observations of '", min_names[i],
104-
"' to perform SMOTE."
105-
))
104+
cli::cli_abort(
105+
"Not enough observations of {.var {min_names[i]}} to perform SMOTE."
106+
)
106107
}
107108

108109
# Run the smote algorithm (minority data, # of neighbors, # of sampeles needed)

R/tomek.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#'
3939
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
4040
#' columns `terms` and `id`:
41-
#'
41+
#'
4242
#' \describe{
4343
#' \item{terms}{character, the selectors or variables selected}
4444
#' \item{id}{character, id of this step}
@@ -139,7 +139,7 @@ step_tomek_new <-
139139
prep.step_tomek <- function(x, training, info = NULL, ...) {
140140
col_name <- recipes_eval_select(x$terms, training, info)
141141
if (length(col_name) > 1) {
142-
rlang::abort("The selector should select at most a single variable")
142+
cli::cli_abort("The selector should select at most a single variable.")
143143
}
144144

145145
if (length(col_name) == 1) {

0 commit comments

Comments
 (0)