17.1 Template structure
R Markdown templates should be contained within an R package, which can be easily created from the menuFile -> 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 theinst/rmarkdown/templates directory of a package. This structure can be generated automatically with theuse_rmarkdown_template() function from theusethis 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.yamlskeleton/skeleton.RmdThetemplate.yaml specifies how the template is displayed within the RStudio “From Template” dialog box. This YAML file must have aname and adescription field. You can optionally specifycreate_dir: true if you want a new directory to be created when the template is selected. As an example of thetemplate.yaml file:
name: My Templatedescription: This is my templateYou can provide a brief example R Markdown document inskeleton.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 tobookdown::html_document2, and select a default templateflatly:
---title:"Untitled"author:"Your Name"output: bookdown::html_document2:toc:truefig_caption:truetemplate: flatly---## Introduction## Analysis## Conclusions