1

For a quarto document with multiple supported output formats, how can I make quarto parameters that are conditional on the active format?

e.g. Suppose I wanted to have one plot background color for HTML output, and a different color for powerpoint? Other applications could be changing character sizes, line weights, etc.

My first thought was ty try placing the params YAML block within each format specifier, but that doesn't work.

the following params.qmd document fails rendering with an error "object 'params' not found"

---
title: "format-driven parameters"
format: 
  html:
    params: 
      plot_bg: "mistyrose"
  pptx:
    params:
      plot_bg: "lightblue"
---


```{r}
#| warning: false
#| message: false
library(ggplot2)
ggplot(mtcars, aes(x=drat, y=mpg)) +
  geom_point() +
  theme_minimal() +
  theme(plot.background = element_rect(fill = params$plot_bg, linetype = "blank"))
```
mac
  • 3,137
  • 1
  • 28
  • 42
  • 3
    Related: https://stackoverflow.com/questions/75571053/how-to-detect-the-format-of-output-in-a-currently-executing-knitr – MrFlick Apr 14 '23 at 18:53
  • 1
    [This answer](https://stackoverflow.com/a/75105006/10858321) might be of help. – shafee Apr 14 '23 at 19:02

1 Answers1

0

a bit of a workaround, as it doesn't directly use the YAML parameters for configuration, but achieves the same result in this simple case:

---
title: "format-driven parameters"
format: 
  html: default
  pptx: default
---


```{r}
#| warning: false
#| message: false
library(ggplot2)

output_format <- knitr::pandoc_to()

if(is.null(output_format)){ #NULL e.g. when running interactively in IDE
  extra_theme <- theme(plot.background=element_rect(fill = "white", linetype = "blank"))
}else if(output_format=="html"){
  extra_theme <- theme(plot.background=element_rect(fill = "mistyrose", linetype = "blank"))
} else if(output_format=="pptx"){
  extra_theme <- theme(plot.background=element_rect(fill = "lightblue", linetype = "blank"))
} else {
  extra_theme <- theme(plot.background=element_rect(fill = "white", linetype = "blank"))
}


ggplot(mtcars, aes(x=drat, y=mpg)) +
  geom_point() +
  theme_minimal() +
  extra_theme
```

approach based on this answer

mac
  • 3,137
  • 1
  • 28
  • 42