Skip to content

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

Open
@DavisVaughan

Description

@DavisVaughan

Taken from: #997 (comment)

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.

    tidyr/R/rectangle.R

    Lines 197 to 201 in 511fc0f

    pluck <- purrr::pluck
    cols <- map(pluckers, function(idx) {
    map(x, ~ pluck(.x, !!!idx))
    })
    cols <- new_data_frame(cols, n = vec_size(.data))
  • Even if we fix the above point, hoist() isn't type stable due to purrr::pluck() behavior with empty elements #1203, would force pluck(<ptype>, ...) to always return NULL, which makes fixing the above point useless right now

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorrectangling 🗄️converting deeply nested lists into tidy data frames

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions