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

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

Adding Text

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

Generating Slideshows

PDFs and LaTeX

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

For more information about R Markdown visit http://rmarkdown.rstudio.com/