I have old code that used to work using tidyr::unnest_wider() to unnest a nested named list into their own columns; however, it no longer works. Instead I get an error saying x$name_of_list must be a vector, not a <non-vector> object, where my non-vector objects include <mcpfit> and <patchwork/gg/ggplot> objects. It seems like they tried to address this issue here, but it still doesn't work using tidyr v. 1.3.0.
I couldn't easily create a reproducible example from my own use case. But I'll use the example listed in the Github issue link above in hopes that this will work for my use case as well.
library(tidyverse)
m <-
tibble::as_tibble(mtcars[1,]) %>%
mutate(ls_col=list(
list(
a=c(1:10),
b=lm(cyl~gear))
)
)
m2 <-
m %>%
unnest_wider(ls_col)
I am looking for EITHER an alternative data.table or base R solution OR a tidyverse workaround (e.g., remove the non-vector objects from the nested list and then use tidyr::unnest_wider()). tidyr::unnest() seems to work, but then I don't know how to pivot the column containing the lists into their own columns (R crashes every time I try something like this).
You can specify
strict = TRUE.Why?
The
strictargument defaults toFALSE, and in this state,unnest_widerwill convert zero-length typed objects likenumeric()orcharacter()in your list toNA, which can be helpful in converting lists with zero-length items into a typed column, for example:Whereas with
strict = TRUE, type is strictly preserved, which means in this case we end up with a list column:The default
strict = FALSEcan come in handy in some circumstances, since it can help rearranging complex lists with some empty items (as in parsing certain json structures). To achieve this,unnest_wideruses the functionvctrs::list_sizes, (via the non-exported functionelt_to_wide), which will throw an error if the list contains non-vector items:I wouldn't call this behaviour a bug as such, but it's a bit unintuitive and feels like we are using
strict = TRUEfor a reason other than its design rationale. However, it does work here.Created on 2023-08-04 with reprex v2.0.2