Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnest_wider / _longer fails if list contains objects. #810

Closed
ahmohamed opened this issue Nov 25, 2019 · 2 comments
Closed

unnest_wider / _longer fails if list contains objects. #810

ahmohamed opened this issue Nov 25, 2019 · 2 comments
Labels
bug rectangling 🗄️

Comments

@ahmohamed
Copy link

@ahmohamed ahmohamed commented Nov 25, 2019

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.

```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 ```
@ahmohamed
Copy link
Author

@ahmohamed ahmohamed commented Nov 25, 2019

The issue seems to be affecting hoist as well.

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

@hadley
Copy link
Member

@hadley hadley commented Nov 25, 2019

I think the root cause might be the same as #806

@hadley hadley added bug rectangling 🗄️ labels Nov 27, 2019
@hadley hadley closed this as completed in 592a413 Nov 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug rectangling 🗄️
Projects
None yet
Development

No branches or pull requests

2 participants