Home > Software design >  Quarto: Parsing an expression to chunk labels
Quarto: Parsing an expression to chunk labels

Time:11-12

I am trying to pass an expression to a YAML chunk label in Quarto using R. I know how to do this for knitr chunk options in R Markdown (and I also know that this works well in Quarto). However, I would like to know if this is possible using the current labeling idiom.

For example, with the following source...

enter image description here

...I can generate some output that has figure captions as well as cross-references.

enter image description here

To be clear, using the normal chunk options, I want to use the second chunk like this:

```{r}
#| label: fig-plot-cars
#| fig-cap = <cap>
#| echo: false
plot(cars)  
```

where <cap> is the variable created earlier (or any expression) that returns a string for the figure caption.

CodePudding user response:

You could use !expr in your fig-cap: to parse an R expression like this:

---
title: ""
format: html
---

```{r}
#| label: caption
#| include: false
cap <- 'Displacement vs Speed'
```

See @fig-plot-cars.

```{r fig.cap = cap}
#| label: fig-plot-cars
#| fig-cap: !expr cap
#| echo: false
plot(cars)  
```

Output:

enter image description here

  • Related