-
Notifications
You must be signed in to change notification settings - Fork 420
Fix internal error in pivot_wider()
#1614
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
Changes from 1 commit
55499fb
01c799d
8fc10ec
af69358
8fb9d9f
8cb5c10
2fd897a
8cfa97f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -246,6 +246,60 @@ test_that("`pivot_wider_spec()` requires empty dots", { | |
| }) | ||
| }) | ||
|
|
||
|
|
||
| test_that("doesn't crash when `id_cols` selects column removed by `names_from` (#1609)", { | ||
| local_options(lifecycle_verbosity = "quiet") | ||
|
|
||
| # Note how we have an "external vector" here. Ideally tidyselect would error | ||
| # on this, but for legacy reasons we currently allow it with a warning, and it | ||
| # produces a weird (but correct) tidyselect error | ||
| x <- c(1, 100, 200, 300) | ||
|
|
||
| df <- tibble( | ||
| x = x, | ||
| y = c(1, 2, 3, 4) | ||
| ) | ||
|
|
||
| # Should get tidyselect error, not internal error | ||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider( | ||
| df, | ||
| id_cols = x, | ||
| values_from = y, | ||
| names_from = x | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| test_that("doesn't crash when `id_cols` selects non-existent column (#1482)", { | ||
| df <- tibble(name = c("x", "y"), value = c(1, 2)) | ||
|
|
||
| # Should get tidyselect error, not internal error | ||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider( | ||
| df, | ||
| id_cols = c("non", "existent"), | ||
| names_from = name, | ||
| values_from = value | ||
| ) | ||
| }) | ||
|
|
||
| df2 <- tibble(y = c("a", "a", "b", "c"), z = c(21, 22, 23, 24)) | ||
|
|
||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider( | ||
| df2, | ||
| id_cols = all_of(c("a", "b", "c")), | ||
| names_from = y, | ||
| values_from = z | ||
| ) | ||
| }) | ||
|
|
||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider(df2, id_cols = 1:2, names_from = y, values_from = z) | ||
| }) | ||
| }) | ||
|
|
||
| # column names ------------------------------------------------------------- | ||
|
|
||
| test_that("names_glue affects output names", { | ||
|
|
@@ -885,63 +939,3 @@ test_that("`id_cols` compat behavior doesn't trigger if named `...` are supplied | |
| pivot_wider(df, ids = id) | ||
| }) | ||
| }) | ||
|
|
||
| # Tests for issue #1609 / #1482 ----------------------------------------------- | ||
|
|
||
| test_that("doesn't crash when `id_cols` selects column removed by `names_from` (#1609)", { | ||
| # Original issue scenario | ||
| x <- c(1, 2, 12, 31, 123, 2341) | ||
| df <- data.frame(x = x) | ||
|
|
||
| # This should produce a proper tidyselect error, not an internal error | ||
| expect_snapshot(error = TRUE, { | ||
| df %>% | ||
| dplyr::mutate(y = stringr::str_split(x, "")) %>% | ||
| unnest(cols = y) %>% | ||
| pivot_wider( | ||
| id_cols = x, | ||
| values_from = y, | ||
| names_from = x | ||
| ) | ||
| }) | ||
|
||
| }) | ||
|
|
||
| test_that("doesn't crash when `id_cols` selects non-existent column (#1482)", { | ||
| # Related issue scenario | ||
| df <- tibble(name = c("x", "y"), value = c(1, 2)) | ||
|
|
||
| # Should get tidyselect error, not internal error | ||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider( | ||
| df, | ||
| id_cols = c("non", "existent"), | ||
| names_from = name, | ||
| values_from = value | ||
| ) | ||
| }) | ||
|
|
||
| # Character vector case | ||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider( | ||
| df, | ||
| id_cols = c("a", "b", "c"), | ||
| names_from = name, | ||
| values_from = value | ||
| ) | ||
| }) | ||
|
||
|
|
||
| df2 <- tibble(y = c("a", "a", "b", "c"), z = c(21, 22, 23, 24)) | ||
|
|
||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider( | ||
| df, | ||
|
||
| id_cols = all_of(c("a", "b", "c")), | ||
| names_from = y, | ||
| values_from = z | ||
| ) | ||
| }) | ||
|
|
||
| expect_snapshot(error = TRUE, { | ||
| pivot_wider(df, id_cols = 1:2, names_from = y, values_from = z) | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want to see this warning in the snapshot output