Hi, I’m Sharon Machlis at IDG, here with Episode 68 of Do More With R: Generate PowerPoint
slides straight from R. Or, create HTML presentation slides.
There are lots of ways to share results of your R analysis: compiled reports, interactive
apps, even in the body of an email.
But sometimes, you want a slide presentation.
It’s easy to generate one directly from your R code – complete with charts and graphs
– right from an R Markdown document.
Let’s take a look.
To create a PowerPoint from R, you need three things on your system:
• PowerPoint or OpenOffice, • The rmarkdown package, version 1.9 or
later, and • Pandoc version 2.0.5 or newer.
Pandoc isn’t an R package.
It’s not R at all; it’s a separate piece of open-source software designed for file-format
conversions.
RStudio ships with Pandoc, so you probably have a version of it installed if you use
RStudio.
You can run rmarkdown’s pandoc_version() function to see if you’ve got it installed
and if so, what version.
If your pandoc is too old, try updating RStudio (or install pandoc directly from pandoc.org).
Next, create an R Markdown document.
If you do that from RStudio’s menu with File > New File > R Markdown, you’ll have
a few choices.
If you click on tthat second choice, Presentation, there’s an option right there for PowerPoint!
Here’s the result.
If I save that and then “knit” it by clicking the knit button, you’ll see what happens.
You can see there are slides here that show you some basics of how to mix text and plots
from R code in your markdown document.
So how did R know where to break the content into new slides?
The default is “the highest level of headline that’s not followed immediately by another
headline”.
In this case that’s headline level 2, all the slide titles starting with the two pound
signs.
If I were to change any of these headlines to one pound sign, then the slides would break
only at those.
Notice that if there’s too much content for a slide, content runs off the edge.
You don’t need to use that default rule, though.
You can set what headline level to divide your document into new slides.
That’s done in the doc’s YAML header, with the slide_level option.
But if you don’t want to fiddle with all that, you can divide content into new slides
manually with 3 dashes.
Make sure the 3 dashes are on a completely new line.
Now let’s look at an R Markdown file that’s a bit more interesting, at least for me.
Here I’m importing a file I wrote with a couple of functions to get latest forecast
data from the National Weather Service API.
(If you’re interested in details on those, check out this video’s associated InfoWorld
article – if you’re watching on YouTube, link is in the description).
Notice the R chunk options I’ve set: echo FALSE, so my R code doesn’t show up in my
slides; and also warning and message to FALSE, so those don’t show up in my slides either.
I’ve also set cacheing to TRUE, which I’d never do for a file I want to update regularly;
I just did that to save time on the video.
Then I added code to plot high and low temperature forecasts for 3 cities: New York, San Francisco,
and Honolulu.
And here I’ve got my R plots in a PowerPoint.
Every time I want an updated version, I just need to re-run the document.
I could even set this up to run automatically using Windows scheduler or launchd on a Mac.
If you want a different default font or font size for your slides, you can set those with
a reference PowerPoint document and add that in the YAML header.
I did that for these slides with a PowerPoint file I called CorporateStyle.
It’s a regular PowerPoint file and not a PowerPoint template; I went into the Master
Slide view and set some styles.
That’s View > Slide Master in the PowerPoint menu.
This is great if you specifically need a PowerPoint document.
One thing it lacks, though, is interactivity.
For example, you can’t roll over a graph and see underlying data.
You could if these were HTML slides, though.
There are a few ways to generate HTML slides from R. One of the most popular is with the
xaringan R package.
xaringan is built on top of the remark dot j-s JavaScript library.
It’s got a bit of a learning curve to do more than the basics, but the look and feel
is extremely customizable.
xaringan would need its own video series to cover all you can do with it (and honestly,
I’m not a power user yet).
This is a pretty basic xaringan file using mostly defaults.
3 dashes create a new slide.
That first slide after the title slide won’t display; it’s setting defaults for the other
slides.
Layout TRUE means “this is a slide setting up layout defaults.”
Class center sets everything – header text, graphics – centered unless I specifically
code it otherwise.
I’m using one of my favorite dataviz packages, echarts4r, to create a function to graph my
weather data.
Let me knit this file and open the results in
a new browser window.
There’s a ton more you can do with xaringan, from adding your own CSS file to creating
your own theme to adding animations.
I’ve included links to a few xaringan resources in this video’s associated InfoWorld article.
That’s it for this episode, thanks for watching!
For more R tips, head to the Do More With R page at bit-dot-l-y slash do more with R,
all lowercase except for the R. You can also find the Do More With R playlist on YouTube’s
IDG Tech Talk channel where you can subscribe so you never miss an episode.
Hope to see you next time.
Stay healthy and safe, everyone!