I have a few Rmd documents that all have the same YAML frontmatter except for the title. How can I keep this frontmatter in one file and have it used for all the documents? It is getting rather large and I don't want to keep every file in step every time I tweak the frontmatter.
I want to still
- use the Knit button/Ctrl+Shift+K shortcut in RStudio to do the compile
- keep the whole setup portable: would like to avoid writing a custom output format or overriding
rstudio.markdownToHTML
(as this would require me to carry around a.Rprofile
too)
Example
common.yaml:
author: me
date: "`r format (Sys.time(), format='%Y-%m-%d %H:%M:%S %z')`"
link-citations: true
reference-section-title: References
# many other options
an example document
----
title: On the Culinary Preferences of Anthropomorphic Cats
----
I do not like green eggs and ham. I do not like them, Sam I Am!
Desired output:
The compiled example document (ie either HTML or PDF), which has been compiled with the metadata in common.yaml
injected in. The R code in the YAML (in this case, the date) would be compiled as a bonus, but it is not necessary (I only use it for the date which I don't really need).
Options/Solutions?
I haven't quite got any of these working yet.
- With rmarkdown one can create a
_output.yaml
to put common YAML metadata, but this will put all of that metadata underoutput:
in the YAML so is only good for options underhtml_document:
andpdf_document:
, and not for things like author, date, ... write a knitr chunk to import the YAML, e.g.
---- title: On the Culinary Preferences of Anthropomorphic Cats ```{r echo=F, results='asis'} cat(readLines('common.yaml'), sep='\n') ``` ---- I do not like green eggs and ham. I do not like them, Sam I Am!
This works if I
knitr('input.Rmd')
and thenpandoc
the output, but not if I use the Knit button from Rstudio (which I assume callsrender
), because this parses the metadata first before running knitr, and the metadata is malformed until knitr has been run.- Makefile: if I was clever enough I could write a Makefile or something to inject
common.yaml
intoinput.Rmd
, then runrmarkdown::render()
, and somehow hook it up to the Knit button of Rstudio, and perhaps somehow save this Rstudio configuration into the.Rproj
file so that the whole thing is portable without me needing to edit.Rprofile
too. But I'm not clever enough.
EDIT: I had a go at this last option and hooked up a Makefile to the Build command (Ctrl+Shift+B). However, this will build the same target every time I use it via Ctrl+Shift+B, and I want to build the target that corresponds with the Rmd file I currently have open in the editor [as for Ctrl+Shift+K].