datawookie
datawookie

Reputation: 6554

Multiple sets of Shared Options in R Markdown

Is it possible to have multiple sets of shared options for R Markdown?

This is my problem: I have a folder with a bunch of markdown files. The files can be divided into two groups:

I would like to factor out common YAML code from each of these groups. Now I know that I can create a _output.yaml file which would capture common YAML, but I essentially need to have two of these files, one for each of the output formats.

I saw the use of pandoc_args suggested here and I gave it a try as follows:

---
title: Document Type 1
output:
  html_document:
    pandoc_args: './common-html.yaml'
---

and

---
title: Document Type 2
output:
  revealjs::revealjs_presentation:
    pandoc_args: './common-reveal.yaml'
---

However using this setup the options from the included YAML files don't get processed.

Any other suggestions would be appreciated!

Upvotes: 3

Views: 414

Answers (1)

Salim B
Salim B

Reputation: 2719

You can specify multiple output formats in the same _output.yaml file like this (just some example options):

html_document:
  self_contained: false
revealjs::revealjs_presentation:
  incremental: true

Then you have to render all output formats which cannot be done directly using the RStudio GUI. Instead you have to enter the following into the R console:

rmarkdown::render(input = "your.Rmd",
                  output_format = "all")

Ideally make sure that there's no output key in the YAML front matter of the .Rmd document itself. Otherwise the output options in _output.yaml file might get overridden. Unfortunately I couldn't find a comprehensive documentation of the exact behavior. Some of my observations so far:

  • Output options defined in the YAML front matter of the .Rmd document itself always override those specified in the shared options file _output.yaml.
  • Specifying an output format using the default option set (like pdf_document: default) in the YAML front matter of the .Rmd document itself completely overrides all options specified in _output.yaml. But if you don't explicitly specify the default options (like output: pdf_document; which is only possible for a single output format at once), the _output.yaml content is fully regarded.
  • If you have specified options for multiple output formats in _output.yaml, only the first one gets rendered when pressing the knit button in RStudio (even if you explicitly press knit to HTML/PDF/Word). You have to use rmarkdown::render(output_format = "all") to render the other formats, too.

Upvotes: 1

Related Questions