Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a multilevel dictionary in the YAML metadata of R Markdown?

While using a single level list/dictionary for parameterizing an rmarkdown document works:

---
params:
  first_level: ~
---

```{r}
params
```

and knitting returns the expected

## $first_level
## NULL

I'm unable to use multi-level list/dictionaries as knitting

---
params:
  first_level:
    second_level: ~
---

```{r}
params
```

produces Error: no value field specified for YAML parameter 'first_level' Execution halted, where I would expect

## $first_level
## $first_level$second_level
## NULL

Is there really only a single level list supported or what am I screwing up?

As I commented below, the expected output can be achieved using

---
params:
  first_level: !r list(second_level = NULL)
---

```{r}
params
```

But why use yaml then at all in place of a parametrizing code block?

like image 382
balin Avatar asked Oct 29 '22 23:10

balin


1 Answers

params is a special field for R Markdown, and you must use one of the two ways to specify the value of a parameter: if the value is not a list (e.g., a scalar), you can specify it using the normal YAML syntax; however, if it is a list, R Markdown expects a sub-field named value, and the value must be specified in this sub-field. In your case, you must use a value field, e.g.,

---
params:
  first_level:
    value:
      second_level: ~
---

```{r}
params
```

That is currently by design.

like image 164
Yihui Xie Avatar answered Nov 09 '22 11:11

Yihui Xie