library(tidyverse)
library(knitr)
mtcars <- mtcars

In this tutorial, we will provide an introduction to R Markdown. This is an amazing framework for creating fully reproducible reports while leveraging the power of the R programming language. The beauty of R Markdown is that you can write code, perform analysis, and view output in it. It can then be rendered into a document that can be shared with your audience of choice.

An excellent resource for R Markdown, for which this tutorial takes partial influence from, is Chapter 27 of the book “R for Data Science” by Hadley Wickham and Garrett Grolemund.

Additionally, we will be referencing the cheat sheet for R Markdown, which can be located here: https://rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf

We can divide the content into three categories: 1) The YAML header at the top with the title, author, date, and output information (separated by —); 2) the code chunks separated by ``` which can be run by clicking the green play button on the side; and 3) text, such as this.

Text in an R Markdown document can utilize markdown syntax. At first, you might have to rely fairly heavily on the cheat sheet along with some trial and error, but you will get used to it fairly often. Here are some examples:

You can create white space with this character:


You can create a block quote like this:

block quote

and you can create a faint horizontal rule this way:


Next, you can create headers - this is common when you want to separate your document into multiple sections.

Biggest header

Second biggest header

Third biggest header

Fourth biggest header

Fifth biggest header
Smallest header

We can embed equations inline in the following way: \(E = m * c^{2}\)

But perhaps most importantly, R code can be embedded in the following way. Suppose I want to return the mean of the “hp” variable from the “mtcars” dataset. When code is surrounded by back ticks and the letter r, this can be done. For example, the mean is 146.6875. As mentioned earlier, a new R chunk can be created by enclosing code by a pair of ```.

A list can be created like this:

  • unordered list
  • item 2
    • sub-item 1
    • sub-item 2
  1. ordered list
  2. item 2
  • sub-item 1
  • sub-item 2

A table can be created like this:

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

However, creating tables from R data gets particularly interesting. Consider these two for instance:

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
kable(head(mtcars))
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

A great degree of customizability is available through the “knitr” package, along with packages like

  • kableExtra
  • pander
  • formattable

This will be the subject for another tutorial.

Let’s conclude by looking at the cheat sheet.