tidyr icon indicating copy to clipboard operation
tidyr copied to clipboard

`hoist()` on a zero length list-of col should utilize the ptype

Open DavisVaughan opened this issue 4 years ago • 2 comments

Taken from: https://github.com/tidyverse/tidyr/issues/997#issuecomment-984865044

library(dplyr)
library(tidyr)

# no hoist column is returned at all
mtcars %>%
  group_nest(cyl) %>% # this gives us a list-of `data` col
  slice(0) %>%
  hoist(data, "mpg")
# A tibble: 0 x 2
# ... with 1 variable: cyl <dbl>

It ideally should instead return:

mtcars %>%
  group_nest(cyl) %>%
  slice(0) %>%
  hoist(data, "mpg")
# A tibble: 0 x 2
# ... with 2 variables: cyl <dbl>, mpg <dbl>

This currently doesn't work for two reasons:

  • This double map() should take into account the fact that x might be a list-of col, in which case applying the plucker to each list element could always result in the same type of plucked result. If the list elements are list-ofs or data.frames, then I think every plucked result should always have the type of pluck(<ptype>, ...), but this isn't fully thought through. https://github.com/tidyverse/tidyr/blob/511fc0f17ce3f0e6985e46918ecca2176fb95033/R/rectangle.R#L197-L201
  • Even if we fix the above point, https://github.com/tidyverse/tidyr/issues/1203, would force pluck(<ptype>, ...) to always return NULL, which makes fixing the above point useless right now

DavisVaughan avatar Dec 02 '21 18:12 DavisVaughan

#1203 seems fixed by dev purrr now

DavisVaughan avatar Oct 18 '22 15:10 DavisVaughan

A few other cases to think about related to this

library(tidyr)
library(vctrs)

tibble(bar = list()) %>%
  hoist(bar, 'baz')
#> # A tibble: 0 × 0

tibble(bar = list_of(.ptype = integer())) %>%
  hoist(bar, 'baz')
#> # A tibble: 0 × 0

tibble(bar = list_of(.ptype = data_frame(baz = integer()))) %>%
  hoist(bar, 'baz')
#> # A tibble: 0 × 0

Created on 2022-12-21 with reprex v2.0.2.9000

DavisVaughan avatar Dec 21 '22 20:12 DavisVaughan