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

switch check_type() to use cli package #1235

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the unlikely event that the single variable name is too large, i used cli::ansi_strtrim to truncate the variable by itself

} 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)}"
simonpcouch marked this conversation as resolved.
Show resolved Hide resolved
)
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
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/bin2factor.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.
* 8 factor variables found: `x5`, `x5`, `x5`, `x5`, ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happening here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved with a call to unique() 0a36bb7 (#1235)


# check_name() is used

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/regex.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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