Skip to content

unnest_wider / _longer fails if list contains objects. #810

@ahmohamed

Description

@ahmohamed

Consider this example

m <- tibble::as_tibble(mtcars[1,]) %>% 
  mutate(ls_col=list(
    list(
      a=c(1:10), 
      b=lm(cyl~gear))
    )
  )

Where I have a nested column that has 2 elements, one of which is an object (lm in this case). When trying to unnest elements to columns (unnest_wider) or rows (unnest_longer), tidyr complains.

m %>% tidyr::unnest_wider(ls_col)
# Error: `x` must be a vector, not a `lm` object
# Call `rlang::last_error()` to see a backtrace

m %>% tidyr::unnest_longer(ls_col)
# Error: `x` must be a vector, not a `lm` object
# Call `rlang::last_error()` to see a backtrace

Strangely, we can get the correct behavior if we use plain unnest.

m %>% tidyr::unnest(ls_col)
# # A tibble: 2 x 12
# mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb ls_col      
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <named list>
#   1    21     6   160   110   3.9  2.62  16.5     0     1     4     4 <int [10]>  
#   2    21     6   160   110   3.9  2.62  16.5     0     1     4     4 <lm>    

I am not sure if this is intended behavior and whether there are possible workarounds.

Details ```r sessionInfo() # R Under development (unstable) (2019-11-04 r77367) # Platform: x86_64-pc-linux-gnu (64-bit) # Running under: Debian GNU/Linux 10 (buster) # # Matrix products: default # BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so # # locale: # [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C # [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 # [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C # [7] LC_PAPER=en_US.UTF-8 LC_NAME=C # [9] LC_ADDRESS=C LC_TELEPHONE=C # [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C # # attached base packages: # [1] stats graphics grDevices utils datasets methods base # # other attached packages: # [1] dplyr_0.8.3 tidyr_1.0.0 # # loaded via a namespace (and not attached): # [1] Rcpp_1.0.2 fansi_0.4.0 utf8_1.1.4 zeallot_0.1.0 # [5] crayon_1.3.4 assertthat_0.2.1 R6_2.4.0 lifecycle_0.1.0 # [9] backports_1.1.5 magrittr_1.5 pillar_1.4.2 cli_1.1.0 # [13] rlang_0.4.1 vctrs_0.2.0 glue_1.3.1 purrr_0.3.3 # [17] compiler_4.0.0 pkgconfig_2.0.3 tidyselect_0.2.5 tibble_2.1.3 ```

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