3 Creating Rmd Templates

3.1 ⭐️Overview

This file contains notes on creating Rmd templates for RStudio.

  1. In RStudio, click on the “new file” button.
  2. Click “R Markdown…”

  1. Click “From Template” in the left sidebar menu.
  2. A list of Rmd templates appears in the “Template:” list.

You can create your own templates and add them to this menu, which can be really handy! Below, I describe how to do that.

Important: Rmd templates can only be added to RStudio’s template list by adding the Rmd template to a package and then installing that package on your computer. Read Hadley Wickham and Jenny Bryan’s R Packages book for an overview of creating R packages.

3.3 Create a package

See R Packages and/or my R packages notes.

3.4 Add a template to your package

Step 1. Create directory for template

Templates are located within the inst/rmarkdown/templates directory of a package. This structure can be generated automatically with the use_rmarkdown_template() function from the usethis package. It is possible to contain multiple templates in a single package, with each template stored in a separate sub-directory.

Step 2.

For example, the “No Output” Rmd template can be viewed at: https://github.com/brad-cannell/templates/blob/master/inst/rmarkdown/templates/no_output/skeleton/skeleton.Rmd

Step 3. Change the name of the folder in inst/rmarkdown/templates from template-name. For example, no_output.

Step 4. Open the folder you just renamed and update template.yaml.

  • The value you pass to the name: parameter will determine how the template is listed in RStudio’s template list. For example, name: No Output.

  • The value you pass to the description: parameter appear as a tool tip when you hover your mouse over the template’s name in RStudio’s template list. For, example, Brad Cannell's template for an Rmd file with no output.

Step 5. Create (or copy and paste) your Rmd template file into skeleton/skeleton.Rmd.

🗒Side Note: If you already have a template in your package, you can create another one by pasting usethis::use_rmarkdown_template() in the console again and following the steps above.