Skip to content

hoist(): not possible to create a list_of column #998

@mgirlich

Description

@mgirlich

It seems that it is currently not possible to create a list_of column.

library(tidyr)
df <- tibble(
  character = c("Toothless", "Dory"),
  metadata = list(
    list(colors = "black"),
    list(colors = "blue")
  )
)

# Goal: colors should be a `list_of<chr>`
df %>% 
  hoist(metadata,
    "colors",
    .ptype = list(colors = vctrs::list_of(.ptype = character()))
  )
#> Error: Can't convert <character> to <list_of<character>>.

# --> add explicit transformation function via `.transform`
df %>% 
  hoist(metadata,
    "colors",
    .ptype = list(colors = vctrs::list_of(.ptype = character())),
    .transform = list(
      colors = ~ {
        vctrs::list_of(!!!.x, .ptype = character())
      })
  )
#> Error: Can't simplfy 'colors'; contains a nested list

# --> simplify = FALSE doesn't combine to list_of
df %>% 
  hoist(metadata,
    "colors",
    .transform = list(
      colors = ~ {
        vctrs::list_of(!!!.x, .ptype = character())
      }),
    .simplify = FALSE
  )
#> # A tibble: 2 x 2
#>   character colors         
#>   <chr>     <list>         
#> 1 Toothless <list<chr> [1]>
#> 2 Dory      <list<chr> [1]>

Created on 2020-07-16 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorrectangling 🗄️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