How do you access multi-level (sub-key) values of params in Rmarkdown?

In a parameterized R Markdown report, how do you access parameters with multiple sub-key values? For example, from https://bookdown.org/yihui/rmarkdown/params-knit.html the following yaml is used

---
title: My Document
output: html_document
params:
  year:
    label: "Year"
    value: 2017
    input: slider
    min: 2010
    max: 2018
    step: 1
    sep: ""

However, nothing except for params$year$value seems to be passed to the main body of the report.

Knitting the following shows that only the value is in the params list, but none of the other entries are available. Note the final missing back tick in the Rmarkdown code if copy/pasting to RStudio

---
title: "My Document"
output: html_document
params:
  minimum: 
    label: "Minimum:"
    value: 100
    input: slider
    min: 0
    max: 1000
---

```{r}
print(params)
``

Knitting the above results in:

image

I would expect there to be more entries available. When you read the same yaml in with yaml::read_yaml(), you get the following:

$title
[1] "My Document"

$output
[1] "html_document"

$params
$params$minimum
$params$minimum$label
[1] "Minimum:"

$params$minimum$value
[1] 100

$params$minimum$input
[1] "slider"

$params$minimum$min
[1] 0

$params$minimum$max
[1] 1000

Is it possible to access params$minimum$input, params$minimum$max, etc. from the R Markdown doc?

Hi @r_alanb: Could you say a little about what you like to use these values for for? That might help folks understand the context better.

Hi @dromano
Of course. I have a R markdown document setup to read in a few files and write some language based on a few inputs. It works as is, but I just want to organize my yaml differently.

Example that works:

---
title: My Document
output: html_document
params:
  file1: "file1.csv"
  file2: "file2.csv"
  var1: 2020
  var2: "January"
---

<!--```{r}
#x <- read.csv(params$file1)
#y <- read.csv(params$file2)
# do stuff with x and y
```-->

A paragraph using `r params$var1` and `r params$var2`. 

Again, mainly I want to organize the yaml in a nested form, but the following does not work, even though it is valid yaml.

---
title: My Document
output: html_document
params:
  files: 
    file1: "file1.csv"
    file2: "file2.csv"
  vars:  
    var1: 2020
    var2: "January"
---

<!--```{r}
#x <- read.csv(params$file1)
#y <- read.csv(params$file2)
# do stuff with x and y
```-->

A paragraph using `r params$vars$var1` and `r params$vars$var2`. 

If you fix that missing ` and comment out reading in the files that don't exist, you get the following error:

Error: no value field specified for YAML parameter 'files'
Execution halted

Thanks, @r_alanb: I can't get your working example to work without adding the missing `, so I'm little puzzled trying to understand why it works for you. Could you confirm that copying and pasting it into a new .Rmd file works for you, too?

Thanks for your time helping @dromano. It did work for me if I added the back tick and commented out the read.csv() lines. So I've gone ahead and commented out the entire R code block so it should work with a straight copy/paste.

Hi @r_alanb,

I think I figured out a way to do what you want, but slightly differently, by using !r to insert R commands in the yaml header:

---
title: My Document
output: html_document
params:
  files: !r  list(file1 = 'file1.csv', file2 = 'file2.csv')
---


```{r}
params$files$file1
```

A paragraph using `r params$files$file1`.

Is this what you meant?

Thanks @dromano

That will definitely work.

However, I guess the question is more why can't you specify multi-level values for params?

I see, and sorry to have misunderstood: I'd love to know where to find the code responsible for making the parameters available, but I'm pretty sure debug() won't be any help. I'll keep an eye out, though, to see if any light gets shed on the question...

No problem, thank you for your time and responses, @dromano!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.