Skip to content

Improve unchop() error for incompatible ptype #1478

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

Merged
merged 5 commits into from
Aug 28, 2024
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# tidyr (development version)

* `unchop()` produces a more helpful error message when columns cannot be cast
to `ptype` (@mgirlich, #1477).

* `expand_grid()` gains a new `.vary` argument, allowing users to control
whether the first column varies fastest or slowest (#1543, @JamesHWade).

Expand Down
15 changes: 13 additions & 2 deletions R/chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
col_sizes <- x_sizes[[i]]

if (!col_is_list) {
# Optimize rare non list-cols
if (!is_null(col_ptype)) {
col <- vec_cast(col, col_ptype, x_arg = col_name, call = error_call)
col <- vec_cast(
x = col,
to = col_ptype,
x_arg = col_name,
call = error_call
)
}
out_cols[[i]] <- vec_slice(col, out_loc)
next
Expand All @@ -267,7 +273,12 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
row_recycle <- col_sizes != sizes
col[row_recycle] <- map2(col[row_recycle], sizes[row_recycle], vec_recycle, call = error_call)

col <- list_unchop(col, ptype = col_ptype)
col <- list_unchop(
x = col,
ptype = col_ptype,
error_arg = col_name,
error_call = error_call
)

if (is_null(col)) {
# This can happen when both of these are true:
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/chop.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
Error in `chop()`:
! `cols` is absent but must be supplied.

# incompatible ptype mentions the column (#1477)

Code
unnest(df, data, ptype = list(data = integer()))
Condition
Error in `unnest()`:
! Can't convert `data[[2]]` <character> to <integer>.

# incompatible sizes are caught

Code
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/unnest.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
(expect_error(unnest(df, x)))
Output
<error/vctrs_error_ptype2>
Error in `list_unchop()`:
Error in `unnest()`:
! Can't combine `x[[1]]` <double> and `x[[2]]` <tbl_df>.

# unnest() advises on outer / inner name duplication
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ test_that("`ptype = list()` uses list ptype", {
)
})

test_that("incompatible ptype mentions the column (#1477)", {
df <- tibble(data = list(1, "2"))

expect_snapshot(error = TRUE, {
unnest(df, data, ptype = list(data = integer()))
})
})

test_that("unchopping a bare empty list results in unspecified()", {
df <- tibble(x = integer(), y = list())
expect <- tibble(x = integer(), y = unspecified())
Expand Down
Loading