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

Description Usage Arguments Value Note Examples

View source: R/utils-yaml.R

Description

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

Usage

1
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
1
2
3
4
## hello:
## - a
## - b
## - c
cat(whisker::whisker.render("hello: {{hello}}", hx)) # messed up whisker
1
## 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

1
2
key:
- value1

and not

1
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
1
2
3
4
## 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

1
2
3
4
5
6
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. 25, 2021, 1:16 p.m.