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

use cli in check_nominal_type() #1245

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -617,17 +617,13 @@ check_nominal_type <- function(x, lvl) {
was_factor <- fac_ref_cols[!(fac_ref_cols %in% fac_act_cols)]

if (length(was_factor) > 0) {
rlang::warn(
paste0(
" There ",
ifelse(length(was_factor) > 1, "were ", "was "),
length(was_factor),
ifelse(length(was_factor) > 1, " columns ", " column "),
"that ",
ifelse(length(was_factor) > 1, "were factors ", "was a factor "),
"when the recipe was prepped:\n ",
paste0("'", was_factor, "'", collapse = ", "),
".\n This may cause errors when processing new data."
cli::cli_warn(
c(
"!" = "There {?w/was/were} {length(was_factor)} column{?s} that \\
{?was a factor/were factors} when the recipe was prepped: \\
",
"*" = "{.and {.var {was_factor}}}",
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
"i" = "This may cause errors when processing new data."
)
)
}
Expand Down
22 changes: 16 additions & 6 deletions tests/testthat/_snaps/nomial_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
check_nominal_type(te, rec$orig_lvls)
Condition
Warning:
There were 2 columns that were factors when the recipe was prepped:
'city', 'zip'.
This may cause errors when processing new data.
! There were 2 columns that were factors when the recipe was prepped:
* `city` and `zip`
i This may cause errors when processing new data.

# missing single factor

Code
check_nominal_type(te, rec$orig_lvls)
Condition
Warning:
! There was 1 column that was a factor when the recipe was prepped:
* `city`
i This may cause errors when processing new data.

# missing factors with skipping

Code
check_nominal_type(te, rec$orig_lvls)
Condition
Warning:
There were 2 columns that were factors when the recipe was prepped:
'city', 'zip'.
This may cause errors when processing new data.
! There were 2 columns that were factors when the recipe was prepped:
* `city` and `zip`
i This may cause errors when processing new data.

12 changes: 6 additions & 6 deletions tests/testthat/_snaps/stringsAsFactors.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
rec1_as_str <- bake(rec1, new_data = as_str)
Condition
Warning:
There were 2 columns that were factors when the recipe was prepped:
'fact', 'ord'.
This may cause errors when processing new data.
! There were 2 columns that were factors when the recipe was prepped:
* `fact` and `ord`
i This may cause errors when processing new data.

# strings_as_factors = TRUE

Code
rec2_as_str <- bake(rec2, new_data = as_str)
Condition
Warning:
There were 2 columns that were factors when the recipe was prepped:
'fact', 'ord'.
This may cause errors when processing new data.
! There were 2 columns that were factors when the recipe was prepped:
* `fact` and `ord`
i This may cause errors when processing new data.

17 changes: 17 additions & 0 deletions tests/testthat/test-nomial_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ test_that("missing factors", {
expect_snapshot(check_nominal_type(te, rec$orig_lvls))
})

test_that("missing single factor", {
tr <-
Sacramento_fac %>%
select(city) %>%
slice(1:500)
te <-
Sacramento_chr %>%
select(city) %>%
slice(501:932)

rec <-
recipe( ~ ., data = tr) %>%
prep(training = tr)

expect_snapshot(check_nominal_type(te, rec$orig_lvls))
})

test_that("missing factors with skipping", {
tr <-
Sacramento_fac %>%
Expand Down
Loading