Skip to content

Commit e7aac41

Browse files
authored
Ensure unpack() is size stable in the zero col / one row case (#1190)
1 parent 1a73d35 commit e7aac41

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# tidyr (development version)
2+
3+
* `unpack()` now works correctly with data frame columns containing 1 row but
4+
0 columns (#1189).
25

36
* The `ptype` argument of `unnest()` now works as expected (#1158).
47

R/pack.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ unpack <- function(data, cols, names_sep = NULL, names_repair = "check_unique")
9898
check_present(cols)
9999
cols <- tidyselect::eval_select(enquo(cols), data)
100100

101+
size <- vec_size(data)
102+
101103
# Start from first principles to avoid issues in any subclass methods
102-
out <- new_data_frame(data, n = vec_size(data))
104+
out <- new_data_frame(data, n = size)
103105

104106
cols <- map2(out[cols], names(cols), check_unpack, names_sep = names_sep)
105107

@@ -108,7 +110,8 @@ unpack <- function(data, cols, names_sep = NULL, names_repair = "check_unique")
108110

109111
names(out) <- vec_as_names(names(out), repair = names_repair, repair_arg = "names_repair")
110112

111-
out <- as_tibble(out, .name_repair = "minimal")
113+
out <- tibble::new_tibble(out, nrow = size)
114+
112115
reconstruct_tibble(data, out)
113116
}
114117

tests/testthat/test-pack.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ test_that("can choose to add separtor", {
6969
out <- df %>% unpack(c(y, z), names_sep = "_")
7070
expect_named(out, c("x", "y_a", "z_a"))
7171
})
72+
73+
test_that("can unpack 1-row but 0-col dataframe (#1189)", {
74+
df <- tibble(x = tibble(.rows = 1))
75+
expect_identical(unpack(df, x), tibble::new_tibble(list(), nrow = 1L))
76+
})

0 commit comments

Comments
 (0)