Skip to content

Commit

Permalink
Merge pull request #1235 from tidymodels/cli-check_type
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Oct 13, 2023
2 parents fa20024 + 0a36bb7 commit 26a7ed8
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 36 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -8,6 +8,8 @@

* It is now documented that `step_spline_b()` can be made periodic. (#1223)

* When errors are thrown about wrongly typed input to steps, the offending variables and their types are now listed. (#1217)

# recipes 1.0.8

## Improvements
Expand Down
48 changes: 38 additions & 10 deletions R/misc.R
Expand Up @@ -425,24 +425,52 @@ check_type <- function(dat, quant = TRUE, types = NULL, call = caller_env()) {
if (is.null(types)) {
if (quant) {
all_good <- vapply(dat, is.numeric, logical(1))
label <- "numeric"
types <- "numeric"
} else {
all_good <- vapply(dat, is_qual, logical(1))
label <- "factor or character"
types <- "factor or character"
}
} else {
all_good <- purrr::map_lgl(get_types(dat)$type, ~ any(.x %in% types))
label <- glue::glue_collapse(types, sep = ", ", last = ", or ")
}

if (!all(all_good)) {
rlang::abort(
paste0(
"All columns selected for the step",
" should be ",
label,
"."
),
info <- get_types(dat)[!all_good, ]
classes <- map_chr(info$type, function(x) x[1])
counts <- vctrs::vec_split(info$variable, classes)
counts$count <- lengths(counts$val)
counts$text_len <- cli::console_width() - 18 - (counts$count > 1) -
nchar(counts$key) - (counts$count > 2)

# cli::ansi_collapse() doesn't work for length(x) == 1
# https://github.com/r-lib/cli/issues/590
variable_collapse <- function(x, width) {
x <- paste0("{.var ", x, "}")
if (length(x) == 1) {
res <- cli::ansi_strtrim(x, width = width)
} else if (length(x) == 2) {
res <- cli::ansi_collapse(
x, last = " and ", width = width, style = "head"
)
} else {
res <- cli::ansi_collapse(
x, width = width, style = "head"
)
}
res
}

problems <- glue::glue_data(
counts,
"{count} {key} variable{ifelse(count == 1, '', 's')} \\
found: {purrr::map2_chr(val, text_len, variable_collapse)}"
)
names(problems) <- rep("*", length(problems))

message <- "All columns selected for the step should be {.or {types}}."

cli::cli_abort(
c("x" = message, problems),
call = call
)
}
Expand Down
2 changes: 1 addition & 1 deletion R/ratio.R
Expand Up @@ -130,7 +130,7 @@ prep.step_ratio <- function(x, training, info = NULL, ...) {
col_names <- col_names[!(col_names$top == col_names$bottom), ]

check_type(
training[, c(col_names$top, col_names$bottom)],
training[, unique(c(col_names$top, col_names$bottom))],
types = c("double", "integer")
)

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/bin2factor.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_bin2factor()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, integer, or logical.
x All columns selected for the step should be double, integer, or logical.
* 1 factor variable found: `description`

---

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/count.md
Expand Up @@ -13,7 +13,8 @@
Condition
Error in `step_count()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 1 integer variable found: `rows`

# check_name() is used

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/_snaps/cut.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_cut()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `cat_var`

---

Expand All @@ -14,7 +15,8 @@
Condition
Error in `step_cut()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `cat_var`

# full_breaks_check will give warnings

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/dummy.md
Expand Up @@ -17,7 +17,8 @@
Condition
Error in `step_dummy()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 1 integer variable found: `price`

# tests for NA values in factor

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/dummy_multi_choice.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_dummy_multi_choice()`:
Caused by error in `prep()`:
! All columns selected for the step should be nominal, or logical.
x All columns selected for the step should be nominal or logical.
* 11 double variables found: `mpg`, `cyl`, `disp`, `hp`, ...

# check_name() is used

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/factor2string.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_factor2string()`:
Caused by error in `prep()`:
! All columns selected for the step should be factor, or ordered.
x All columns selected for the step should be factor or ordered.
* 2 string variables found: `w` and `x`

# empty printing

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/harmonic.md
Expand Up @@ -61,7 +61,8 @@
Condition
Error in `step_harmonic()`:
Caused by error in `prep()`:
! All columns selected for the step should be date, datetime, or numeric.
x All columns selected for the step should be date, datetime, or numeric.
* 1 factor variable found: `time_var`

# harmonic cycle_size length

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/impute_mean.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_impute_mean()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `Job`

# Deprecation warning

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/impute_median.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_impute_median()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `Job`

# Deprecation warning

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/_snaps/impute_roll.md
Expand Up @@ -6,7 +6,8 @@
Condition
Error in `step_impute_roll()`:
Caused by error in `prep()`:
! All columns selected for the step should be double.
x All columns selected for the step should be double.
* 1 date variable found: `day`

---

Expand All @@ -25,7 +26,8 @@
Condition
Error in `step_impute_roll()`:
Caused by error in `prep()`:
! All columns selected for the step should be double.
x All columns selected for the step should be double.
* 1 integer variable found: `x4`

# Deprecation warning

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/misc.md
Expand Up @@ -27,5 +27,5 @@
Code
conditionMessage(attr(res, "condition"))
Output
[1] "Error in `step_dummy()`:\nCaused by error in `prep()`:\n! All columns selected for the step should be string, factor, or ordered."
[1] "Error in `step_dummy()`:\nCaused by error in `prep()`:\nx All columns selected for the step should be string, factor, or ordered.\n* 11 double variables found: `mpg`, `cyl`, `disp`, `hp`, ..."

3 changes: 2 additions & 1 deletion tests/testthat/_snaps/novel.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_novel()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 4 double variables found: `Sepal.Length`, `Sepal.Width`, ...

---

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/num2factor.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_num2factor()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `w`

---

Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/ordinalscore.md
Expand Up @@ -5,7 +5,9 @@
Condition
Error in `step_ordinalscore()`:
Caused by error in `prep()`:
! All columns selected for the step should be ordered.
x All columns selected for the step should be ordered.
* 1 double variable found: `numbers`
* 1 factor variable found: `fact`

# empty printing

Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/_snaps/ratio.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_ratio()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `x5`

---

Expand All @@ -14,7 +15,8 @@
Condition
Error in `step_ratio()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `x5`

---

Expand All @@ -23,7 +25,8 @@
Condition
Error in `step_ratio()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `x5`

# check_name() is used

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/regex.md
Expand Up @@ -13,7 +13,8 @@
Condition
Error in `step_regex()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 1 integer variable found: `rows`

# check_name() is used

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/relevel.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_relevel()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 1 integer variable found: `sqft`

---

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/relu.md
Expand Up @@ -29,7 +29,8 @@
Condition
Error in `step_relu()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `val2`

# empty printing

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/string2factor.md
Expand Up @@ -5,7 +5,8 @@
Condition
Error in `step_string2factor()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 1 integer variable found: `n`

---

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/unknown.md
Expand Up @@ -19,7 +19,8 @@
Condition
Error in `step_unknown()`:
Caused by error in `prep()`:
! All columns selected for the step should be string, factor, or ordered.
x All columns selected for the step should be string, factor, or ordered.
* 1 integer variable found: `sqft`

---

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/window.md
Expand Up @@ -71,7 +71,8 @@
Condition
Error in `step_window()`:
Caused by error in `prep()`:
! All columns selected for the step should be double, or integer.
x All columns selected for the step should be double or integer.
* 1 factor variable found: `fac`

---

Expand Down

0 comments on commit 26a7ed8

Please sign in to comment.