Repository: tidyverse/tidyr

Issue #475: Return tibble when nesting
Created: 2018-07-17T15:33:02Z
Currently, mtcars %>% group_by(am, cyl) %>% nest() returns a tibble whereas mtcars %>% nest(-am, -cyl) returns a data.frame with list-cols, which doesn't print well.
Since tibble is already a dependency of tidyr, I imagine that it could be as simple as adding as_tibble to the beginning of nest.data.frame but perhaps there's a more elegant method?
Coercing the output to a tibble results in a list-col of data.frames, whereas coercing the input returns a list-col of tibbles:
library(dplyr)
library(tidyr)

mtcars %>% nest(-am, -cyl) %>% as_tibble()
#> # A tibble: 6 x 3
#>     cyl    am data                 
#>   <dbl> <dbl> <list>               
#> 1    6.    1. <data.frame [3 x 9]> 
#> 2    4.    1. <data.frame [8 x 9]> 
#> 3    6.    0. <data.frame [4 x 9]> 
#> 4    8.    0. <data.frame [12 x 9]>
#> 5    4.    0. <data.frame [3 x 9]> 
#> 6    8.    1. <data.frame [2 x 9]>

mtcars %>% as_tibble() %>% nest(-am, -cyl)
#> # A tibble: 6 x 3
#>     cyl    am data             
#>   <dbl> <dbl> <list>           
#> 1    6.    1. <tibble [3 x 9]> 
#> 2    4.    1. <tibble [8 x 9]> 
#> 3    6.    0. <tibble [4 x 9]> 
#> 4    8.    0. <tibble [12 x 9]>
#> 5    4.    0. <tibble [3 x 9]> 
#> 6    8.    1. <tibble [2 x 9]>

Duplicate issue #487: difference in performance between group_by + nest and nest(-var)
Created: 2018-08-14T15:49:38Z
Hello there,
I am working on a reasonably large dataframe (40k obs, 40 variables string and numeric), and I thought these two dplyr calls would be equivalent:
mydata_full %>% 
  group_by(mygroup) %>% nest()


which works
# A tibble: 10 x 2
   mygroup     data                 
   <chr>     <list>               
 1 group1  <tibble [2,893 x 38]>
 2 group2  <tibble [2,893 x 38]>
 3 group3  <tibble [2,893 x 38]>
 

however, running
mydata_full %>% 
nest(-mygroup)


takes a loooong time and the output in the console does not show the list-column, but rather the contents of the cells directly (so its hard to read anything!!)
I am happy to work on a reproducible example, but that is not easy. Meanwhile, are there any obvious reasons why this bug would appear?
Thanks!

Closing comment (2019-01-19T16:11:10Z): I think this smaller reprex illustrates the basic problem:
library(tidyr)

N <- 5
M <- 1e3
list <- setNames(rep(list(rep(c(NA, "x"), M * 2)), N), paste0("X", seq_len(N)))
df <- as.data.frame(list, stringsAsFactors = FALSE)

df %>% dplyr::group_by(X1) %>% nest() %>% str()
#> Classes 'tbl_df', 'tbl' and 'data.frame':    2 obs. of  2 variables:
#>  $ X1  : chr  NA "x"
#>  $ data:List of 2
#>   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 2000 obs. of  4 variables:
#>   .. ..$ X2: chr  NA NA NA NA ...
#>   .. ..$ X3: chr  NA NA NA NA ...
#>   .. ..$ X4: chr  NA NA NA NA ...
#>   .. ..$ X5: chr  NA NA NA NA ...
#>   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 2000 obs. of  4 variables:
#>   .. ..$ X2: chr  "x" "x" "x" "x" ...
#>   .. ..$ X3: chr  "x" "x" "x" "x" ...
#>   .. ..$ X4: chr  "x" "x" "x" "x" ...
#>   .. ..$ X5: chr  "x" "x" "x" "x" ...
df %>% nest(-X1) %>% str()
#> 'data.frame':    2 obs. of  2 variables:
#>  $ X1  : chr  NA "x"
#>  $ data:List of 2
#>   ..$ :'data.frame': 2000 obs. of  4 variables:
#>   .. ..$ X2: chr  NA NA NA NA ...
#>   .. ..$ X3: chr  NA NA NA NA ...
#>   .. ..$ X4: chr  NA NA NA NA ...
#>   .. ..$ X5: chr  NA NA NA NA ...
#>   ..$ :'data.frame': 2000 obs. of  4 variables:
#>   .. ..$ X2: chr  "x" "x" "x" "x" ...
#>   .. ..$ X3: chr  "x" "x" "x" "x" ...
#>   .. ..$ X4: chr  "x" "x" "x" "x" ...
#>   .. ..$ X5: chr  "x" "x" "x" "x" ...
Created on 2019-01-19 by the reprex package (v0.2.1.9000)
So I'm pretty sure this is a duplicate of #475