11.3 Structure of R Markdown

  • YAML header specifying several document options such as the output format, author and date e.t.c.

  • text written with Markdown syntax,

  • R code chunks with the code that needs to be executed.

The basic structure of a R markdown is shown below:

rmarkdown_diagram

Other YAML options include the following:

  • fontsize to specify the font size e.t.c.

  • toc: true to include a table of contents at the start of the document

For example, the YAML below specifies an HTML document with font size 12pt, and includes a table of contents where 2 is the lowest level of headings. The date of the report is set to the current date by writing the inline R expression 2023-02-05.

---
title: "An R Markdown document"
author: "Paula Moraga"
date: "`r Sys.Date()`"
fontsize: 12pt
output:
  html_document:
    toc: true
    toc_depth: 2
---

To generate a document from the .Rmd file, we can use the Knit button in the RStudio IDE or use the render() function of the rmarkdown package. The render() function has an argument called output_format where we can select the format we want for the final document.