Skip to content

Commit 81a6678

Browse files
authored
Ensure ptype is used on non-list columns in unchop() (#1213)
* Ensure `ptype` is used on non-list columns in `unchop()` * NEWS bullet
1 parent fe4b59d commit 81a6678

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# tidyr (development version)
22

3+
* `unchop()` now respects `ptype` when unnesting a non-list column (#1211).
4+
35
* The `nest()` generic now avoids computing on `.data`, making it more
46
compatible with lazy tibbles (#1134).
57

R/chop.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,20 @@ df_unchop <- function(x, ..., ptype = list(), keep_empty = FALSE) {
209209

210210
for (i in seq_len_width) {
211211
col <- x[[i]]
212+
col_name <- names[[i]]
212213
col_is_list <- x_is_list[[i]]
213214

215+
col_ptype <- ptype[[col_name]]
216+
214217
if (!col_is_list) {
218+
if (!is_null(col_ptype)) {
219+
col <- vec_cast(col, col_ptype, x_arg = col_name)
220+
}
215221
out_cols[[i]] <- vec_slice(col, out_loc)
216222
next
217223
}
218224

219-
col_name <- names[[i]]
220-
col_ptype <- ptype[[col_name]] %||% attr(col, "ptype", exact = TRUE)
225+
col_ptype <- col_ptype %||% attr(col, "ptype", exact = TRUE)
221226

222227
# Drop to a bare list to avoid dispatch
223228
col <- unclass(col)

tests/testthat/test-chop.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ test_that("ptype overrides list-of ptype", {
189189
)
190190
})
191191

192+
test_that("ptype is utilized on non-list columns (#1211)", {
193+
df <- tibble(x = 1)
194+
195+
expect_identical(
196+
unchop(df, x, ptype = list(x = integer())),
197+
tibble(x = 1L)
198+
)
199+
})
200+
192201
test_that("the ptype must be a list", {
193202
expect_snapshot(error = TRUE, unchop(mtcars, mpg, ptype = 1))
194203
})

0 commit comments

Comments
 (0)