Intro to R Markdown

Clay Ford

Spring 2017

R Markdown

Note: this presentation was created with R Markdown in RStudio.

How it works

Simple R Markdown example

I did not insert this plot into my presentation. I simply included the R code that creates it. When I generated this presentation, the R code was executed and the plot was inserted into this slide.

Some extra detail about how it works

How to get started

  1. In RStudio, go to File…New File…R Markdown…
  2. Select Document, Presentation, Shiny, or From Template
  3. Enter Title, Author, select Output Format (eg, html, pdf) and select OK
  4. This opens a new Rmd file with sample text, markdown and R code included to get you started. Save it.
  5. Make changes to the Rmd file and click Knit to see your output. Repeat as often as you like.

Note: The “Shiny” and “From Template” options work a little differently which we’ll discuss.

A note about PDF files

Knitting your Rmd file

Activity 1

  1. Create a new folder called “rmd_workshop”
  2. In RStudio, start a new R Markdown file that will create an HTML “Document”
  3. Name it “Rmd Workshop” and make yourself the Author
  4. Save it as “rmd01” in the folder you created in step 1 (File…Save As)
  5. Click Knit and check out the result
  6. Go to the “rmd_workshop” folder and verify you now have an html file

The Rmd header


R Code chunks

Global chunk options

You may have noticed this at the top of the Rmd file:

This sets global chunk options.


Rmd files in RStudio

Activity 2

  1. Go to the bottom of the rmd01 file and enter ## Simulate Data
  2. Under the new header, type some text such as "Let's simulate some data"
  3. Under your text, insert a code chuck with Ctrl + Alt + I (Win) or Command + Option + I (Mac)
  4. Enter the following R code in your code chunk: x <- rnorm(100)
  5. Below the code chunk type “The proportion of observations greater than 0 is mean(x > 0)”, where mean(x > 0) is between `r and `.
  6. Click Knit


Markdown in presentations

Activity 3

  1. Go to File…New File…R Markdown…
  2. Start a new HTML (Slidy) presentation titled “Rmd Presentation” (or whatever you want)
  3. Save as “rmd02” in your workshop folder and click Knit
  4. Review the presentation then close
  5. Add a new slide called “R Functions”
  6. Enter some text as bullets using “> -
  7. Click Knit and try out the presentation

PDF and Word documents

Adding a table of contents

Activity 4

  1. Start a new R Markdown PDF Document titled “Rmd PDF report” (or whatever you want), Click OK
  2. Save as “rmd03” and click Knit
  3. Close the file and try make the image smaller so it fits on the first page; try setting fig.width and fig.height to 4 and 3, respectively
  4. Add the table of contents options
  5. Add a new section with ## and add an R code chunk: try head(iris, n = 10)
  6. Click Knit and examine PDF

R Markdown Reference Guide and Cheat Sheet

In addition to Rmd templates and the Markdown Quick Reference, RStudio provides quick access to an R Markdown Reference Guide and an R Markdown Cheat Sheet

R Markdown websites

R Markdown website steps

  1. Start a new RStudio Project in an existing or new directory
  2. Create a _site.yml file that contains the site architecture and navigation; see links on previous slide for example
  3. Create an index.Rmd file that will serve as a homepage
  4. Build other Rmd files as needed and link accordingly in the _site.yml file
  5. run rmarkdown::render_site() to build web site files
  6. web site files are created in a folder called _site

The HTML files in _site can be deployed as a stand-alone static website.

Using R Markdown Templates

Using Shiny with R Markdown

The state of R Markdown


Thanks for coming today

For help with data analysis, contact us:

Sign up for more workshops or see past workshops:

Register for the Research Data Services newsletter to be notified of future workshops: