Skip to content

Commit 454a4c8

Browse files
Improve unchop() error for incompatible ptype (#1478)
* Improve `unchop()` error for incompatible ptype * Update snapshot * Tweak test * Tweak formatting * Add a comment about something that confused me --------- Co-authored-by: Davis Vaughan <[email protected]>
1 parent c252a61 commit 454a4c8

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# tidyr (development version)
22

3+
* `unchop()` produces a more helpful error message when columns cannot be cast
4+
to `ptype` (@mgirlich, #1477).
5+
36
* `expand_grid()` gains a new `.vary` argument, allowing users to control
47
whether the first column varies fastest or slowest (#1543, @JamesHWade).
58

R/chop.R

+13-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,14 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
254254
col_sizes <- x_sizes[[i]]
255255

256256
if (!col_is_list) {
257+
# Optimize rare non list-cols
257258
if (!is_null(col_ptype)) {
258-
col <- vec_cast(col, col_ptype, x_arg = col_name, call = error_call)
259+
col <- vec_cast(
260+
x = col,
261+
to = col_ptype,
262+
x_arg = col_name,
263+
call = error_call
264+
)
259265
}
260266
out_cols[[i]] <- vec_slice(col, out_loc)
261267
next
@@ -267,7 +273,12 @@ df_unchop <- function(x, ..., ptype = NULL, keep_empty = FALSE, error_call = cal
267273
row_recycle <- col_sizes != sizes
268274
col[row_recycle] <- map2(col[row_recycle], sizes[row_recycle], vec_recycle, call = error_call)
269275

270-
col <- list_unchop(col, ptype = col_ptype)
276+
col <- list_unchop(
277+
x = col,
278+
ptype = col_ptype,
279+
error_arg = col_name,
280+
error_call = error_call
281+
)
271282

272283
if (is_null(col)) {
273284
# This can happen when both of these are true:

tests/testthat/_snaps/chop.md

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
Error in `chop()`:
1212
! `cols` is absent but must be supplied.
1313

14+
# incompatible ptype mentions the column (#1477)
15+
16+
Code
17+
unnest(df, data, ptype = list(data = integer()))
18+
Condition
19+
Error in `unnest()`:
20+
! Can't convert `data[[2]]` <character> to <integer>.
21+
1422
# incompatible sizes are caught
1523

1624
Code

tests/testthat/_snaps/unnest.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
(expect_error(unnest(df, x)))
3232
Output
3333
<error/vctrs_error_ptype2>
34-
Error in `list_unchop()`:
34+
Error in `unnest()`:
3535
! Can't combine `x[[1]]` <double> and `x[[2]]` <tbl_df>.
3636

3737
# unnest() advises on outer / inner name duplication

tests/testthat/test-chop.R

+8
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ test_that("`ptype = list()` uses list ptype", {
228228
)
229229
})
230230

231+
test_that("incompatible ptype mentions the column (#1477)", {
232+
df <- tibble(data = list(1, "2"))
233+
234+
expect_snapshot(error = TRUE, {
235+
unnest(df, data, ptype = list(data = integer()))
236+
})
237+
})
238+
231239
test_that("unchopping a bare empty list results in unspecified()", {
232240
df <- tibble(x = integer(), y = list())
233241
expect <- tibble(x = integer(), y = unspecified())

0 commit comments

Comments
 (0)