Skip to content

unnest_wider fails on certain types #1504

@ArthurAndrews

Description

@ArthurAndrews

I've been struggling to unnest_wider certain results in a list column. I don't know why some types (tibbles) are correctly unnested into list columns, but others (lm) are not. Any advice? Or is this a bug?

For the third example here, I expect unnest_wider should create a list column of lm objects.

library(tidyverse)
packageVersion("tidyr")
#> [1] '1.3.0'

# works
x <- list(a = 1, b = "b")
tibble(y = list(x, x)) |> unnest_wider(y)
#> # A tibble: 2 × 2
#>       a b    
#>   <dbl> <chr>
#> 1     1 b    
#> 2     1 b

# works
x <- list(a = 1, b = "b", c = tibble(z = 1:3))
tibble(y = list(x, x)) |> unnest_wider(y)
#> # A tibble: 2 × 3
#>       a b     c               
#>   <dbl> <chr> <list>          
#> 1     1 b     <tibble [3 × 1]>
#> 2     1 b     <tibble [3 × 1]>

# doesn't work
x <- list(a = 1, b = "b", c = lm(mpg ~ wt, mtcars))
tibble(y = list(x, x)) |> unnest_wider(y)
#> Error in `unnest_wider()`:
#> ℹ In column: `y`.
#> ℹ In row: 1.
#> Caused by error in `list_sizes()`:
#> ! `x$c` must be a vector, not a <lm> object.
#> Backtrace:
#>      ▆
#>   1. ├─tidyr::unnest_wider(tibble(y = list(x, x)), y)
#>   2. │ └─tidyr:::col_to_wide(...)
#>   3. │   ├─tidyr:::with_indexed_errors(...)
#>   4. │   │ └─rlang::try_fetch(...)
#>   5. │   │   └─base::withCallingHandlers(...)
#>   6. │   └─purrr::map(...)
#>   7. │     └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   8. │       ├─purrr:::with_indexed_errors(...)
#>   9. │       │ └─base::withCallingHandlers(...)
#>  10. │       ├─purrr:::call_with_cleanup(...)
#>  11. │       └─tidyr (local) .f(.x[[i]], ...)
#>  12. │         └─tidyr:::elt_to_wide(...)
#>  13. │           └─vctrs::list_sizes(x)
#>  14. └─vctrs:::stop_scalar_type(`<fn>`(`<lm>`), "x$c", `<env>`)
#>  15.   └─vctrs:::stop_vctrs(...)
#>  16.     └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)

Created on 2023-06-26 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementrectangling 🗄️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