mgirlich / tibblify

Rectangle Nested Lists
https://mgirlich.github.io/tibblify/
GNU General Public License v3.0
67 stars 2 forks source link

Add `tib_multi()` #180

Open mgirlich opened 1 year ago

mgirlich commented 1 year ago

Which allows to collect multiple keys into a list. This is useful when objects "inherit" from a common type, i.e. they have some fields in common but some depend on the object type.

For example to convert this list

library(tibble)

list(
  list(id = 1, name = "a", a = 1, b = TRUE),
  list(id = 2, name = "b", c = 3),
  list(id = 3, name = "c", a = 2, b = FALSE),
  list(id = 4, name = "d", c = 5)
)

via this spec

tspec_df(
  tib_int("id"),
  tib_chr("name"),
  attributes = tib_multi(c("a", "b", "c"))
)

into this tibble

tibble(
  id = 1:4,
  name = letters[1:4],
  attributes = list(
    list(a = 1, b = TRUE),
    list(c = 3),
    list(a = 2, b = FALSE),
    list(c = 5)
  )
)
#> # A tibble: 4 × 3
#>      id name  attributes      
#>   <int> <chr> <list>          
#> 1     1 a     <named list [2]>
#> 2     2 b     <named list [1]>
#> 3     3 c     <named list [2]>
#> 4     4 d     <named list [1]>

Created on 2022-12-23 with reprex v2.0.2