markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
450 stars 32 forks source link

Handle empty data frame case to be consistent with tidyr::unnest #531

Closed roboton closed 2 years ago

roboton commented 2 years ago

Currently tidytable::unnest. throws an error when dealing with an empty tibble:

> tidytable::unnest.(tibble::tibble(foo = list()), foo)
Error in .l[[1]] : subscript out of bounds
> tidyr::unnest(tibble::tibble(foo = list()), foo)
# A tibble: 0 × 1
# … with 1 variable: foo <???>

Closes #530 which raises this issue.

markfairbanks commented 2 years ago

Hey @roboton - thanks for the PR!

As I look into this, it looks like tidyr::unnest() actually returns an "unspecified" vector for foo.

library(tidytable, warn.conflicts = FALSE)

df <- tidytable(id = integer(), foo = list())

tidyr::unnest(df, foo) %>%
  pull.(foo) %>%
  class()
#> [1] "vctrs_unspecified"

In this PR unnest.() returns a list for foo:

devtools::load_all(".")
#> ℹ Loading tidytable

df <- tidytable(id = integer(), foo = list())

unnest.(df, foo)
#> # A tidytable: 0 × 2
#> # … with 2 variables: id <int>, foo <list>

I think having it return an empty logical() column would probably be the way to go instead.

It looks like vctrs has special handling for vctrs_unspecified data, but data.table won't. But it looks like vctrs somewhat treats empty logical() and vctrs_unspecified the same. So that won't mess with any data.table code if we use an empty logical() column.

I'm not sure the best way to do this yet - but I'm guessing we'll need to do the check inside of the internal unnest_col() defined here.

markfairbanks commented 2 years ago

Thanks again for the PR 😄

markfairbanks commented 2 years ago

I just realized I forgot to ask - would you like to be added to the contributor list? Basically your name would show up here and here under the authors as a contributor.

roboton commented 2 years ago

Sure, and if there are ways you think I can be helpful to tidytable please feel free to reach out. I think it's a super important project for the R community.

Thanks for reaching out and including me!

On Wed, Sep 7, 2022 at 8:13 AM Mark Fairbanks @.***> wrote:

I just realized I forgot to ask - would you like to be added to the contributor list? Basically your name would show up here https://cran.r-project.org/web/packages/tidytable/index.html and here https://cran.r-project.org/web/packages/tidytable/tidytable.pdf under the authors as a contributor.

— Reply to this email directly, view it on GitHub https://github.com/markfairbanks/tidytable/pull/531#issuecomment-1238739610, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOLJS5BODJHLYPKDUBYLB3V47F2LANCNFSM53GBTJLA . You are receiving this because you were mentioned.Message ID: @.***>

markfairbanks commented 2 years ago

What's a good first and last name for me to use?

roboton commented 2 years ago

Oh, "Robert On" works just fine. Thanks for checking.

On Wed, Sep 7, 2022 at 11:45 PM Mark Fairbanks @.***> wrote:

What's a good first and last name for me to use?

— Reply to this email directly, view it on GitHub https://github.com/markfairbanks/tidytable/pull/531#issuecomment-1239488305, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOLJS2LLZ2DXGAGEXJJ5ALV5CS6TANCNFSM53GBTJLA . You are receiving this because you were mentioned.Message ID: @.***>