Introduction

R Markdown is built into RStudio and allows you to create documents like HTML, PDF, and Word documents from R. With R Markdown, you can embed R code into your documents.

Why use R Markdown?

  • Turn work in R into more accessible formats
  • Incorporate R code and R plots into documents
  • R Markdown documents are reproducible – the source code gets rerun every time a document is generated, so if data change or source code changes, the output in the document will change with it.

Getting Started

  • Create a new R Markdown file in RStudio by going to
    File > New File > R Markdown…
  • Click the "presentation" tab
  • Enter a title, author, and select what kind of slideshow you ultimately want (this can all be changed later)

Getting Started

The beginning of an R Markdown file looks like this:

---
title: "Air Quality"
author: "JHU"
date: "May 17, 2016"
output: html_document
---

The new document you've created will contain example text and code below this – delete it for a fresh start.

Making Your First Slide

  • Title your first slide using two # signs:
    ## Insert Title Here
  • To make a slide without a title, use three asterisks:
    ***
  • You can add subheadings with more # signs:
    ### Subheading or #### Smaller Subheading
  • To add a new slide, just add another Title:
    ## New Slide Title

Adding Text

  • Add bullet points to a slide using a hyphen followed by a space:
    - bullet point
  • Add sub-points using four spaces and a plus sign:
        + sub-point
  • Add an ordered list by typing the number/letter:
    1. first point
        a) sub-sub-point
  • Add bullet points that appear one by one (on click) with:
    >- iterated bullet point

Formatting Text

Text Code in R Markdown
plain text plain text
italics *italics*
bold **bold**
link [link](http://www.jhsph.edu)
verbatim code `code here `

Embedding R Code

This is a chunk of R code in R Markdown:

```{r}
head(airquality)
```
The code gets run, and both the input and output are displayed.

head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

Embedding R Code

To hide the input code, use echo=FALSE.

```{r, echo=FALSE}
head(airquality)
```

##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

This can be useful for showing plots.

Embedding R Code

To show the input code only, use eval=FALSE.

```{r, eval=FALSE}
head(airquality)
```

head(airquality)

Embedding R Code

To run the code without showing input or output, use include=FALSE.

```{r, include=FALSE}
library(ggplot2)
```

Generating Slideshows

  • Click the Knit button at the top of the R Markdown document to generate your new document.
    • You may be asked to install required packages if you don't already have them installed – hit "Yes" and RStudio will install them for you
  • You can change the type of document generated by changing the output line in the header, or by selecting an output from the Knit button's pull-down menu.

Generating Slideshows

  • HTML: two options with different looks
    • output: ioslides_presentation
    • output: slidy_presentation
  • PDF: output: beamer_presentation
  • Note: You can specify multiple outputs at the beginning of the R Markdown file if you will need to generate multiple filetypes.

PDFs and LaTeX

  • To knit a PDF slideshow, you will need to install LaTeX on your computer
  • LaTeX is a typesetting system that is needed to convert R Markdown into formatted text for PDFs

Downloading and Installing LaTeX

  • LaTeX is free
  • LaTeX takes up a lot of space (almost ~2.6 GB download and takes up ~5 GB when installed)
  • Visit https://www.tug.org/begin.html to download LaTeX for your operating system
  • Depending on your internet connection, it may take a while to download due to its size

Conclusion