Search code examples
quarto

Pass params to quarto as a separate file


1. params in file index.qmd.

---
title: '`r params$dataset`'
format: html
params:
  dataset: "iris"
---

This is the {{< meta params.dataset >}} dataset.

```{r,results="asis",echo=FALSE,comment=""}
cat(paste("This is the",params$dataset),"dataset")
```

```{r}
head(get(params$dataset))
```

quarto render index.qmd

This works as intended.

enter image description here

But, I want to move the params to a separate file and that doesn't work.

2a. params in _metadata.yml

params is moved to a separate file _metadata.yml which looks like this:

params:
  dataset: "iris"

index.qmd remains the same.

quarto render index.qmd --execute-params _metadata.yml

This fails with this error:

Error:
! object 'params' not found
Execution halted

2b. params in _metadata.yml

Same as above, but _metadata.yml looks like this (added ---):

---
params:
  dataset: "iris"
---

quarto render index.qmd --execute-params _metadata.yml

Now, it's a different error.

ERROR: YAMLError: expected a single document in the stream, but found more 

YAMLError: expected a single document in the stream, but found more 
    at load (file:///opt/quarto/bin/quarto.js:11691:11)

3a. params in _metadata.yml

metadata-files is added to index.qmd

---
title: '`r params$dataset`'
format: html
metadata-files:
  - _metadata.yml
---

```{r,results="asis",echo=FALSE,comment=""}
cat(paste("This is the",params$dataset),"dataset")
```

```{r}
head(get(params$dataset))
```

_metadata.yml like this:

params:
  dataset: "iris"

quarto render index.qmd

Error:
! object 'params' not found
Execution halted

3b. params in _metadata.yml

Same as above, but _metadata.yml like this:

---
params:
  dataset: "iris"
---

quarto render index.qmd

Error reading metadata file from _metadata.yml

ERROR: YAMLException: expected a single document in the stream, but found more
YAMLException: expected a single document in the stream, but found more

quarto 1.3.433


Solution

  • Thanks to the quarto developers for the solution. In addition to the params file, default params must be defined in the qmd. The params file must be in pure yaml. Metadata variables must be defined separately.

    The example below works:

    index.qmd

    ---
    title: '`r params$dataset`'
    format: html
    params:
      dataset: "iris"
    dataset: iris
    ---
    
    This is the {{< meta dataset >}} dataset.
    
    ```{r,results="asis",echo=FALSE,comment=""}
    cat(paste("This is the",params$dataset),"dataset")
    ```
    
    ```{r}
    head(get(params$dataset))
    ```
    
    

    params.yml

    dataset: "mtcars"
    

    quarto render index.qmd --execute-params params.yml

    enter image description here