An Introduction to ymlthis

ymlthis is a package to help you write YAML metadata for R Markdown documents and related tools like blogdown, bookdown, and pkgdown. YAML serves two main roles: to pass information to be printed in the document, such as the title and author, and to control how the document is rendered, anything from the output type (HTML vs. PDF) and typesetting options to what should happen with files created while rendering. See the YAML: An Overview for a more general introduction to YAML and the YAML Fieldguide for YAML field documentation.

Writing YAML with ymlthis

ymlthis makes it easy to write valid YAML by handling the syntax and documenting the majority of YAML fields in one place. yml() creates a yml object that, by default, sets the author name and date. ymlthis uses name options in devtools and usethis (options(devtools.name = "name") and options(usethis.full_name = "name"), respectively), as well as the whoami package, to try to find the author’s name. By default, the date is set to `r format(Sys.Date())`, which will update the date when knitting.

yml()

---author: Malcolm Barrettdate:'`r format(Sys.Date())`'---

Functions that start with yml_*() take and return yml objects, so they can be used to add or modify YAML fields. For instance, to add a title, use yml_title()

An essential use of YAML in R Markdown is to set the output type. By default, the output YAML field looks like

output: html_document

which calls rmarkdown::html_document() when knitting. All of the nested fields passed to output come from the output function you are using (so you can check ?pkg::output_function() to see what the options are). ymlthis writes and validates output YAML by parsing calls to these functions. We can, for example, nest R Markdown’s pdf_document() inside yml_output():

YAML fields can come from a variety of places: R Markdown itself, the output function, Pandoc (for example, as options used in LaTeX), and custom templates. Many of these fields are documented in yml_*() functions.

You can also use yml_toplevel() to append any YAML fields not covered in ymlthis.

Parameterized reports

Parameterized reports are a powerful feature of R Markdown that lets you specify variables in the YAML metadata used in your report, letting you re-use reports for a variety of settings. To set parameters, you use the params YAML field and access the parameter in your report by calling params$variable_name. Basic YAML for parameters looks like this

params:country:"Turkey"

Calling params$country in the R Markdown document will thus return "Turkey". yml_params() will write this for you:

You can also fill in parameters using a Shiny app with the “Knit with Parameters” button in the RStudio IDE or by passing "ask" to the params argument in rmarkdown::render(). While R Markdown will try to guess the Shiny widget type by the value of the parameter, you can specify them yourself with ymlthis functions that start with shiny_*().

If you have an existing .bib file, but you want to specify it in the YAML, instead, you can convert it with bib2yml(). For instance, if you wrote a .bib file for the packages used in your document with knitr::write_bib(), bib2yml() will translate the file to YAML you can include directly in the metadata.

R Markdown extensions

Packages that extend R Markdown, such as bookdown and blogdown, also extend the YAML used to make documents. One of the main ways that other packages introduce YAML is through new output functions. xaringan, for instance, doesn’t use custom YAML outside of its output function, so ymlthis supports it out-of-box. Passing xaringan::moon_reader() to yml_output() will write the YAML for you.

ymlthis can process any output function. Other packages also add additional YAML; ymlthis supports six R Markdown packages: bookdown, blogdown, pkgdown, pagedown, rticles, and distill. ymlthis also supports YAML for writing scheduled emails with RStudio Connect. You can find functions supporting these packages with the patterns yml_packagename_*() and packagename_*() (rsconnect, in the case of RStudio Connect functions). Functions that start with yml_packagename_*() document YAML fields and return yml objects, while functions that start with packagename_*() are helper functions. There are also a few template functions. pkgdown_template(), for instance, will assemble a full template for _pkgdown.yml and return it as a yml object, and blogdown_template() will return YAML templates matching your Hugo theme.

Sources of YAML in R Markdown Extension

package

output function

top-level YAML

bookdown

✓

✓

blogdown

✓

✓

pkgdown

✓

✓

pagedown

✓

✓

rticles

✓

✓

distill

✓

✓

learnr

✓

✗

xaringan

✓

✗

revealjs

✓

✗

flexdashboard

✓

✗

R Markdown workflows

ymlthis prints YAML cleanly to the console such that you could copy and paste it into an R Markdown document. However, ymlthis also provides a number of other workflows.

YAML add-in

The easiest way is to use the Shiny add-in included with ymlthis; the add-in supports the most commonly used R Markdown options, citations, LaTeX options, and parameterized reports. You can then export the YAML to an R Markdown file, YAML file, or have it placed on your clipboard.

For more flexibility or to use R Markdown extension packages, you’ll have to write code, as we’ve been doing in this vignette. Your main options are to write the YAML to a file or to place it on your clipboard.

Writing to files

use_rmarkdown(), use_yml_file(), and friends will take a yml object and write to a file for you. Passing yml objects to use_rmarkdown() is often the simplest approach: it will translate the yml object into YAML and open a new R Markdown file for you with the metadata already written.

ymlthis also supports YAML files. For instance, using pkgdown_template() %>% use_pkgdown_yml() will write the YAML for your pkgdown site to _pkgdown.yml based on your package directory. ymlthis also has some helper functions for working with Pandoc templates and highlighting themes.

As an example, let’s set up the navbar YAML for an R Markdown website and then pass it to use_navbar_yml().

In addition to writing to files, you can save YAML to your .Rprofile using options(ymlthis.defaults = "{YAML}"). yml() will return the stored YAML by default. If you pass a yml object to use_yml_defaults(), the code to do this will be placed on your clipboard, which you should paste into your .Rprofile. Open that file with usethis::edit_r_profile(). You can also set a default for body text for use_rmarkdown() by setting the ymlthis.rmd_body() option, e.g. options(ymlthis.rmd_body = "{your text}") (or take advantage of use_rmd_defaults()). Together with specifying default YAML, use_rmarkdown() also serves as an ad-hoc way to make R Markdown templates.

Placing YAML on your clipboard

A final way to use ymlthis is to put YAML on your clipboard to paste into a file or other setting. To place rendered YAML on your clipboard, simply pass a yml object to use_yml().

Additional ymlthisuse_*() functions

function

action

use_yml()

Place yml on your clipboard

use_yml_defaults()

Place code on your clipboard to save yml to your default options

use_pandoc_highlight_style()

Write pandoc theme templates to a .theme file

use_pandoc_template()

Write pandoc templates to a file

Working with yml objects

Under the hood, yml objects are lists of types that match the corresponding YAML and are only converted to YAML when printing or writing to files. Because yml objects are just lists, you can work with them as such. ymlthis also includes several purrr-like functions for working with yml objects. Let’s define a yml object that has a fairly comprehensive set of options: