Skip to content

Commit

Permalink
Merge pull request #153 from tidymodels/use-cli
Browse files Browse the repository at this point in the history
use cli functions
  • Loading branch information
EmilHvitfeldt authored Oct 29, 2024
2 parents e8c9b21 + cc90505 commit cf85be3
Show file tree
Hide file tree
Showing 30 changed files with 95 additions and 105 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ BugReports: https://github.com/tidymodels/themis/issues
Depends:
R (>= 3.6),
recipes (>= 1.1.0.9000)
Imports:
Imports:
cli,
gower,
lifecycle (>= 1.0.3),
dplyr,
Expand All @@ -48,4 +49,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
4 changes: 2 additions & 2 deletions R/adasyn.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -151,7 +151,7 @@ step_adasyn_new <-
prep.step_adasyn <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort("The selector should select at most a single variable.")
}

if (length(col_name) == 1) {
Expand Down
15 changes: 5 additions & 10 deletions R/adasyn_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
#' res <- adasyn(circle_numeric, var = "class", over_ratio = 0.8)
adasyn <- function(df, var, k = 5, over_ratio = 1) {
if (length(var) != 1) {
rlang::abort("Please select a single factor variable for `var`.")
cli::cli_abort("Please select a single factor variable for {.arg var}.")
}

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

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

if (length(k) != 1) {
rlang::abort("`k` must be length 1.")
cli::cli_abort("The {.arg k} argument must be length 1.")
}

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

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

if (nrow(minority) <= k) {
rlang::abort(
glue(
"Not enough observations of '{min_names[i]}' to perform ADASYN."
),
call = call
)
cli::cli_abort("Not enough observations of {.val {min_names[i]}} to perform ADASYN.", call = call)
}

tmp_df <- as.data.frame(
Expand Down
4 changes: 2 additions & 2 deletions R/bsmote.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -183,7 +183,7 @@ step_bsmote_new <-
prep.step_bsmote <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort("The selector should select at most a single variable.")
}

if (length(col_name) == 1) {
Expand Down
15 changes: 8 additions & 7 deletions R/bsmote_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@
#' res <- bsmote(circle_numeric, var = "class", all_neighbors = TRUE)
bsmote <- function(df, var, k = 5, over_ratio = 1, all_neighbors = FALSE) {
if (length(var) != 1) {
rlang::abort("Please select a single factor variable for `var`.")
cli::cli_abort("Please select a single factor variable for {.arg var}.")
}

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

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

if (length(k) != 1) {
rlang::abort("`k` must be length 1.")
cli::cli_abort("{.arg k} must be length 1.")
}

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

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

if (sum(danger_ids) <= k) {
rlang::abort(glue(
"Not enough danger observations of '{min_names[i]}' to perform BSMOTE."
))
cli::cli_abort(
"Not enough danger observations of {.val {min_names[i]}} to perform
BSMOTE."
)
}

if (all_neighbors == FALSE) {
Expand Down
4 changes: 2 additions & 2 deletions R/downsample.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -179,7 +179,7 @@ prep.step_downsample <- function(x, training, info = NULL, ...) {
}

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

if (length(col_name) == 0) {
Expand Down
12 changes: 5 additions & 7 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ check_na <- function(data, step, call = caller_env()) {
na_cols <- vapply(data, function(x) any(is.na(x)), FUN.VALUE = logical(1))
if (any(na_cols)) {
cols <- paste(names(na_cols)[na_cols], collapse = ", ")
rlang::abort(
glue(
"Cannot have any missing values. NAs found ind: {cols}."
),
cli::cli_abort(
"Cannot have any missing values. NAs found in {cols}.",
call = call
)
}
}

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

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

if (!all(all_good)) {
rlang::abort("All columns for this function should be numeric.")
cli::cli_abort("All columns for this function should be numeric.")
}
invisible(all_good)
}

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

Expand Down
4 changes: 2 additions & 2 deletions R/nearmiss.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -159,7 +159,7 @@ prep.step_nearmiss <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)

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

if (length(col_name) == 1) {
Expand Down
14 changes: 5 additions & 9 deletions R/nearmiss_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
#' res <- nearmiss(circle_numeric, var = "class", under_ratio = 1.5)
nearmiss <- function(df, var, k = 5, under_ratio = 1) {
if (length(var) != 1) {
rlang::abort("Please select a single factor variable for `var`.")
cli::cli_abort("Please select a single factor variable for {.arg var}.")
}

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

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

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

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

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

if (nrow(not_class) <= k) {
rlang::abort(
glue(
"Not enough danger observations of '{names(classes)[i]}' to perform NEARMISS."
)
)
cli::cli_abort("Not enough danger observations of {.val {names(classes)[i]}} to perform NEARMISS.")
}

dists <- RANN::nn2(
Expand Down
4 changes: 2 additions & 2 deletions R/rose.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -168,7 +168,7 @@ step_rose_new <-
prep.step_rose <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort("The selector should select at most a single variable.")
}
if (length(col_name) == 1) {
check_column_factor(training, col_name)
Expand Down
4 changes: 2 additions & 2 deletions R/smote.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -157,7 +157,7 @@ step_smote_new <-
prep.step_smote <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort("The selector should select at most a single variable.")
}

if (length(col_name) == 1) {
Expand Down
15 changes: 5 additions & 10 deletions R/smote_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@
#' res <- smote(circle_numeric, var = "class", over_ratio = 0.8)
smote <- function(df, var, k = 5, over_ratio = 1) {
if (length(var) != 1) {
rlang::abort("Please select a single factor variable for `var`.")
cli::cli_abort("Please select a single factor variable for {.arg var}.")
}

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

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

if (length(k) != 1) {
rlang::abort("`k` must be length 1.")
cli::cli_abort("The {.arg k} must be length 1.")
}

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

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

if (nrow(minority) <= k) {
rlang::abort(
glue(
"Not enough observations of '{min_names[i]}' to perform SMOTE."
),
call = call
)
cli::cli_abort("Not enough observations of {.val {min_names[i]}} to perform SMOTE.", call = call)
}

synthetic <- smote_data(minority, k = k, n_samples = samples_needed[i])
Expand Down
4 changes: 2 additions & 2 deletions R/smotenc.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -143,7 +143,7 @@ step_smotenc_new <-
prep.step_smotenc <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort("The selector should select at most a single variable.")
}

if (length(col_name) == 1) {
Expand Down
17 changes: 9 additions & 8 deletions R/smotenc_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ smotenc <- function(df, var, k = 5, over_ratio = 1) {
# the input variables need to be numeric and contain no NA values

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

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

if (!(is.factor(df[[var]]) | is.character(df[[var]]))) {
rlang::abort(paste0(var, " should be a factor or character variable."))
cli::cli_abort(
"{var} should be {.cls factor} or {.cls character} variable."
)
}

if (length(k) != 1) {
rlang::abort("`k` must be length 1.")
cli::cli_abort("The {.arg k} must have length 1.")
}

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

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

# Ensure that we have more minority isntances than desired neighbors
if (nrow(minority) <= k) {
rlang::abort(paste0(
"Not enough observations of '", min_names[i],
"' to perform SMOTE."
))
cli::cli_abort(
"Not enough observations of {.var {min_names[i]}} to perform SMOTE."
)
}

# Run the smote algorithm (minority data, # of neighbors, # of sampeles needed)
Expand Down
4 changes: 2 additions & 2 deletions R/tomek.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is retruned with
#' columns `terms` and `id`:
#'
#'
#' \describe{
#' \item{terms}{character, the selectors or variables selected}
#' \item{id}{character, id of this step}
Expand Down Expand Up @@ -139,7 +139,7 @@ step_tomek_new <-
prep.step_tomek <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
if (length(col_name) > 1) {
rlang::abort("The selector should select at most a single variable")
cli::cli_abort("The selector should select at most a single variable.")
}

if (length(col_name) == 1) {
Expand Down
Loading

0 comments on commit cf85be3

Please sign in to comment.