Chapter 17 Document Templates

When you create a new R Markdown document from the RStudio menu File -> New File -> R Markdown, you will see a default example document (a template) in the RStudio editor. In fact, you can create custom document templates by yourself, which can be useful if you need to create a particular type of document frequently or want to customize the appearance of the final report. The rticles package in Chapter 13 is a good example of custom templates for a range of journals. Some additional examples of where a template could be used include:

  • Creating a company branded R Markdown template that includes a logo and branding colors.

  • Updating the default YAML settings to include standard fields for title, author, date, or default output options.

  • Customizing the layout of the output document by adding additional fields to the YAML metadata. For example, you can add a department field to be included within your title page.

Once created, templates are easily accessed within RStudio, and will appear within the “New R Markdown” window as shown in Figure 17.1.

Selecting R Markdown templates within RStudio.

FIGURE 17.1: Selecting R Markdown templates within RStudio.

This chapter explains how to create templates and share them within an R package. If you would like to see some real-world examples, you may check out the source package of rticles (https://github.com/rstudio/rticles). The rmarkdown::html_vignette format is also a relatively simple example (see both its R source code and the template structure). In addition, Michael Harper has kindly prepared more examples in the repository https://github.com/dr-harper/example-rmd-templates.