Conditional reference_doc / use of "if" in R Markdown YAML

Is it possible to put an 'if' statement into an RMarkdown file? I have to produce two documents which have the same content, but use different PowerPoint template files (different backgrounds/images/fonts/etc). I am struggling to find a way to do this with minimal code. It seems that YAML doesn't have an 'if' function due to its nature. The alternative is to set a variable in the main R script, and then try and use that in the Rmd file YAML:

reference_doc: "`r ref_doc`" 

However, this doesn't work - it just gets passed as-is to the pandoc command:

"C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS My_Data_Pack.knit.md --to pptx --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc54e45caae7a.pptx --reference-doc "`r ref_doc`" 

Another alternative I tried was having two different files with hard-coded YAML files, and then pulling all of the common R Markdown code in from another file:

---
title: "`r pres_title`"
author: Me
output: 
  powerpoint_presentation:
    reference_doc: "`r ref_doc`" 
date: "`r format(Sys.Date(), '%d %B %Y')`"
--- 

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(ggplot2)
library(ggrepel)
library(knitr)
library(tidyr)
library(flextable)
library(ggpubr)
library(forcats)
library(zoo)

set_flextable_defaults(
  font.family = "Arial", font.size = 8.5, 
  border.color = "gray", big.mark = "")

source("main_output_stuff.R")

But that doesn't work, because main_output_stuff.R isn't a proper R file, and that's sort of inherently not how the system was built to work.

Is there an easy way to do what I want? Or indeed a not-easy way? :slightly_smiling_face: Many thanks.

The reference_doc parameter should point to the path of the PPT template. You need to create two separate template files, each with a different style.

We already have two template files; the issue was choosing conditionally which one to use.

We resolved it with the following in the script:

ref_doc <= paste0(some_variable,".pptx")

rmarkdown::render(
    "Data_Pack.Rmd",
    output_file = "blah",
    output_dir = "Outputs/",
    params = list(reference_doc = ref_doc)
  )

and this in the Rmarkdown:

output: 
  powerpoint_presentation:
    reference_doc:  !expr ref_doc

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.