yaml_list: Create a valid, opinionated yaml list for insertion into a...

View source: R/utils-yaml.R

yaml_listR Documentation

Create a valid, opinionated yaml list for insertion into a whisker template

Description

Create a valid, opinionated yaml list for insertion into a whisker template

Usage

yaml_list(thing)

Arguments

thing

a vector or list

Value

a character vector

We want to manipulate our config file from the command line AND preserve comments. Unfortunately, the yaml C library does not parse comments and it makes things difficult to handle. At the moment we have a hack where we use whisker templates for these, but the drawback for whisker is that it does not know how to handle lists, so it concatenates them with commas:

x <- c("a", "b", "c")
hx <- list(hello = x)
cat(yaml::as.yaml(hx)) # representation in yaml
#> hello:
#> - a
#> - b
#> - c
cat(whisker::whisker.render("hello: {{hello}}", hx)) # messed up whisker
#> hello: a,b,c

Moreover, we want to indicate that a yaml list is not a single key/value pair so we want to enforce that we have

key:
- value1

and not

key: value1

This converts the elements to a yaml list before it enters whisker and makes sure that the values are clearly lists.

hx[["hello"]] <- sandpaper:::yaml_list(hx[["hello"]])
cat(whisker::whisker.render("hello: {{hello}}", hx)) # good whisker
#> hello: 
#> - a
#> - b
#> - c

Note

there IS a better solution than this hack, but for now, we will keep what we are doing because it's okay for our purposes: https://github.com/rstudio/blogdown/issues/560

Examples

x <- c("a", "b", "c")
hx <- list(hello = x)
cat(yaml::as.yaml(hx)) # representation in yaml
cat(whisker::whisker.render("hello: {{hello}}", hx)) # messed up whisker
hx[["hello"]] <- sandpaper:::yaml_list(hx[["hello"]])
cat(whisker::whisker.render("hello: {{hello}}", hx)) # good whisker

zkamvar/sandpaper documentation built on Nov. 8, 2024, 5:36 p.m.