Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tib_multi() #180

Open
mgirlich opened this issue Dec 23, 2022 · 0 comments
Open

Add tib_multi() #180

mgirlich opened this issue Dec 23, 2022 · 0 comments
Milestone

Comments

@mgirlich
Copy link
Owner

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

@mgirlich mgirlich added this to the 0.4 milestone Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant