Create a Slideshow (PowerPoint) with R, Knitr, Pandoc, and Slidy

How to create a R slideshow (PowerPoint) with R, Knitr, Pandoc, and Slidy

This page will show you how to create a HTML R slideshow (like a PowerPoint but viewed through your internet browser) using R. If you’re interested in sharing reproducible research this is a must-have function.

Step 1: Install Pandoc

Go to https://code.google.com/p/pandoc/downloads/list and follow the instructions to install Pandoc on your computer.

Step 2: Install Knitr and Markdown

Open R or RStudio and install the packages Knitr and Markdown on your computer by running the following code:

# Install knitr
install.packages("knitr")
install.packages("markdown")

Step 3: Create a .Rmd File Containing Your Analysis

Open RStudio and click File then New then R Markdown. Then click File then Save As. Enter My_Analysis.Rmd as the name of the file and click Save. Note that RStudio will allow you to have spaces in the name but this will prevent later steps from working. Use an underscore instead.

When the new markdown (.Rmd) file is created it very helpfully is already populated with an example (the below code). Note the basic structure – the markdown file is both R code and Knitr code. Any code between the sets of three apostrophes is R code, any code outside of the sets of three apostrophes is Knitr. The R code tells R what to do and the Knitr code creates the HTML file.

Title
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

When you click the **Knit HTML** button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plots(cars)
```

Step 4: Modify the .Rmd File to Produce More Than One Slide

If you based a slideshow on the .Rmd file above it would only have one slide. This wouldn’t be a very good slideshow or a very good example. A page break is added with a blank line, then three dashes, then a blank line.

Add a blank line, then three dashes, then a blank line to your .Rmd file just after the first sentence and again just before “You can also embed plots…”. This will create a HTML slideshow with three slides. Your .Rmd file should look like this:

Title
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

---

When you click the **Knit HTML** button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

---

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plots(cars)
```

Step 5: Create a .R File to Run the .Rmd File

In RStudio click File then New then R Script to create a new .R file. This file will be used to tell Pandoc to create a HTML slideshow based on your .Rmd file. Paste the following code into this R file. Then click File then Save As. Enter any name you want and click Save. I saved this file as Build_Report.R

  • Be sure to change the below code to specify the correct working directory and filenames.
  • Don’t use space in your file names or it won’t work.
# Set working directory
setwd("C:/Documents and Settings/name")

# Load packages
require(knitr)
require(markdown)

# Create slides
knit("My_Analysis.Rmd")
system("pandoc -s -t slidy My_Analysis.md -o My_Analysis.html")

Step 6: Produce HTML and PDF Output Files with R

In RStudio, run all of the code in your Build_Report.R file. This will read the My_Analysis.Rmd file, use that to create a My_Analysis.md file, use the .md to create a My_Analysis.html file. You just made a slideshow with R!

Your slideshow should look like this:

R Slideshow 1

R Slideshow 2

R Slideshow 3

For another example of a slideshow, see Jared Knowles and my Introduction to R Programming presentation: https://dl.dropbox.com/u/1811289/RBootcamp/INTRO_TO_R_PROGRAMMING_SECTOR_67.html#(1)

I’ll cover how to format HTML files on a separate page and link to it here once I do.

There’s a lot more on Knitr here: http://yihui.name/knitr/ and here http://cran.r-project.org/web/packages/knitr/index.html.

There’s a lot more on Pandoc here: http://johnmacfarlane.net/pandoc/.

There’s a lot more on Markdown here: http://daringfireball.net/projects/markdown/.

Here’s a great video introductory video from Yihui, the creator of Knitr. Note that this video only explains how to use R and Knitr to make HTML files and does not cover slideshows.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.

JM

2 thoughts on “Create a Slideshow (PowerPoint) with R, Knitr, Pandoc, and Slidy

Leave a Reply

Your email address will not be published. Required fields are marked *