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 · 5 comments
Closed

unnest_wider / _longer fails if list contains objects. #810

ahmohamed opened this issue Nov 25, 2019 · 5 comments
Labels
bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames

Comments

@ahmohamed
Copy link

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

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 commented Nov 25, 2019

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

@hadley hadley added bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames labels Nov 27, 2019
@hadley hadley closed this as completed in 592a413 Nov 28, 2019
@riher
Copy link

riher commented Apr 2, 2023

This is not solved. (or is broken again?)

unnest_longer works now, unnest_wider does not. Code same as in the OP.

@tseyanglim
Copy link

I am having a similar issue - unnest and unnest_longer work, but unnest_wider gives the error:

Error in `unnest_wider()`:
ℹ In column: `pred_gr`.
ℹ In row: 1.
Caused by error in `list_sizes()`:
! `x$fit` must be a vector, not a <brmsfit> object.

hoist seems to work fine though.

(I just updated to tidyr 1.3.0, hoping the issue would be fixed, but no luck)

@hadley
Copy link
Member

hadley commented Apr 25, 2023

Please file a new issue with a reprex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames
Projects
None yet
Development

No branches or pull requests

4 participants