Creating a Presentation with R

In this post I’ll look at creating a presentation using the R ecosystem. I’ve used beamer before, and I love it, but I haven’t used the knitr R package yet. Incidentally, the creator of knitr, Yihui Xie, does not like beamer. This is fine, I have been wrong about technology before–I recall thinking in college that facebook was for losers and that it would never catch on. Anyway, Yihui’s work is really impressive and I strongly suggest checking it out.

Creating an HTML SLideshow

First, install John MacFarlane’s pandoc. (From here on out I’ll assume you’re using a Debian-style linux distribution.) You can install pandoc over the command line using apt-get.

sudo apt-get install pandoc

From here, you need to install and load knitr and markdown in RStudio.

install.packages("knitr")
install.packages("markdown")
library(knitr)
library(markdown)

Once this is accomplished, you need to create an .Rmd file that will contain your markdown and R code. The R code will be delimited by three backticks and feature an “r” in curly braces. You may use RStudio to create an .Rmd document by clicking File > New File > R Markdown… This will guide you through several options. You’ll want to create an HTML document using slidy.

Alternatively, you can write something up in your favorite test editor that looks something like this,

---
title: "Example"
author: "Connor Johnson"
date: "08/31/2014"
output: slidy_presentation
---

## Slide One

- Foo
- Bar
- Baz

---

## Slide Two

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

---

## Slide Three

```{r, echo=FALSE}
plot(cars)
```

Once this is saved as example.Rmd or something, we can go to the RStudio command line and knit the R markdown file (.Rmd) file into a regular markdown file (.md). The next line will use the pandoc utility to convert the markdown file into an HTML file.

knit("example.Rmd")
system("pandoc -s -t slidy example.md -o example.html")

Once that’s done, you’ll have a nice HTML slideshow.

Creating a PDF Slideshow

First, we’ll need to download some things for latex. This is what I recommend,

sudo apt-get install texlive-latex-base
sudo apt-get install texlive-latex-extra
sudo apt-get install texlive-latex-recommended
sudo apt-get install texlive-fonts-recommended
sudo apt-get install texlive-xetex

Then we can run the following from the RStudio terminal,

knit("example.Rmd")
system("pandoc -t beamer example.md -o example.pdf")

More information on creating slideshows using pandoc may be found here.

One thought on “Creating a Presentation with R”

  1. I tried the example but received an error message. May I ask you if you know how to resolve it.

    Warning message:
    running command ‘pandoc -s -t slidy presentationTry.md -o presentationTry.html’ had status 127

Comments are closed.