I'm trying to generate the following YAML structure from tabular data:
- name: Josiah Carberry
roles:
- investigation: lead
- data curation: supporting
I'm struggling with the structure of the roles key. It's basically an array of dictionaries which would translate to a list of tibbles/data frames in R.
My issue is that I can't figure out how to store such lists of tibbles in a way that will produce the same output as in the example above.
The calls to jsonlite confuse me, I think all you're trying to do is change the outer tibble from column major to row major, this can be done with purrr::transpose():
Thanks, that looks promising. I probably simplified the example a bit too much and using your solution doesn't work for more complex cases that use a combination of dictionaries, arrays and nested arrays of dictionaries. E.g.:
library(tibble)
tibble(
id = paste0("id", 1:3),
name = tibble(
given = c("John", "David", "Tom"),
family = c("Smith", "Brown", "Williams"),
),
roles = list(
list(tibble(writing = "lead"), tibble(supervision = "supporting")),
list(tibble(writing = "equal")),
list(tibble(writing = "supporting"), tibble(supervision = "lead"))
)
) |>
purrr::transpose() |>
yaml::as.yaml(indent.mapping.sequence = TRUE) |>
cat()
#> Warning: Element 2 must be length 3, not 2
#> - id: id1
#> name:
#> - John
#> - David
#> - Tom
#> roles:
#> - writing: lead
#> - supervision: supporting
#> - id: id2
#> name:
#> - Smith
#> - Brown
#> - Williams
#> roles:
#> - writing: equal
#> - id: id3
#> name: ~
#> roles:
#> - writing: supporting
#> - supervision: lead
- id: id1
name:
given: John
family: Smith
roles:
- writing: lead
- supervision: supporting
- id: id2
name:
given: David
family: Brown
roles:
- writing: equal
- id: id3
name:
given: Tom
family: Williams
roles:
- writing: supporting
- supervision: lead
I can tweak the data above to have something closer to what I want, but at the moment I can't get it work with all the different data types I need.
The conversion to JSON allows me to handle that variety of data as well as missing values.
This is originally for a package I'm developing. You can see a real life example of the typical YAML generated by the package here.