R Quick Tip: parameter re-use within rmarkdown YAML

[This article was first published on R – Locke Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Ever wondered how to make an rmarkdown title dynamic? Maybe, wanted to use a parameter in multiple locations? Maybe wanted to pass through a publication date? Advanced use of YAML headers can help!

Normally, when we write rmarkdown, we might use something like the basic YAML header that the rmarkdown template gives us.

---
title: "My report"
date: "18th April, 2017"
output: pdf_document
---

You may already know the trick about making the date dynamic to whatever date the report gets rendered on by using the inline R execution mode of rmarkdown to insert a value.

---
title: "My report"
date: "`r Sys.Date()`"
output: pdf_document
---

What you may not already know is that YAML fields get evaluated sequentially so you can use a value created further up in the params section, to use it later in the block.

---
params:
  dynamictitle: "My report"
  reportdate: !r Sys.Date()
output: pdf_document
title: "`r params$dynamictitle`"
date: "`r params$reportdate`"
---

By doing this, you can then pass parameter values into the render() function when you want to generate a report.

rmarkdown::render("MWE.Rmd", 
    params=list(dynamictitle="New",
                reportdate=Sys.Date()-1)
                )

Parameter re-use within rmarkdown enables you to dynamically generate vital metadata for the report and use values in multiple places within the document. Very handy!

The post R Quick Tip: parameter re-use within rmarkdown YAML appeared first on Locke Data. Locke Data are a data science consultancy aimed at helping organisations get ready and get started with data science.

To leave a comment for the author, please follow the link and comment on their blog: R – Locke Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)