4.3 Access the document metadata in R code

When an Rmd document is compiled, all of its metadata in the YAML section will be stored in the list object rmarkdown::metadata. For example, rmarkdown::metadata$title gives you the title of the document. You can use this metadata object in your R code, so that you do not need to hard-code information that has been provided in the YAML metadata. For example, when you send an email with the blastula package (Iannone and Cheng 2020) within an Rmd document, you may use the title of the document as the email subject, and get the sender information from the author field:

  1. ---
  2. title: An important report
  3. author: John Doe
  4. email: john@example.com
  5. ---
  6. We have done an important analysis, and want to email
  7. the results.
  8. ```{r}
  9. library(rmarkdown)
  10. library(blastula)
  11. smtp_send(
  12. ...,
  13. from = setNames(metadata$email, metadata$author),
  14. subject = metadata$title
  15. )
  16. ```

References

Iannone, Richard, and Joe Cheng. 2020. Blastula: Easily Send HTML Email Messages. https://github.com/rich-iannone/blastula.