17.1 Template structure
R Markdown templates should be contained within an R package, which can be easily created from the menu File -> New Project
in RStudio (choose the project type to be “R Package”). If you are already familiar with creating R packages, you are certainly free to use your own favorite way to create a new package.
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. As a minimal example, inst/rmarkdown/templates/my_template
requires the following files:
template.yaml skeleton/skeleton.Rmd
The template.yaml
specifies how the template is displayed within the RStudio “From Template” dialog box. This YAML file must have a name
and a description
field. You can optionally specify create_dir: true
if you want a new directory to be created when the template is selected. As an example of the template.yaml
file:
name: My Template
description: This is my template
You can provide a brief example R Markdown document in skeleton.Rmd
, which will be opened in RStudio when the template is selected. We can add section titles, load commonly used packages, or specify default YAML parameters in this skeleton document. In the following example, we specify the default output format to bookdown::html_document2
, and select a default template flatly
:
---
title: "Untitled"
author: "Your Name"
output:
bookdown::html_document2:
toc: true
fig_caption: true
template: flatly
---
## Introduction
## Analysis
## Conclusions