How to Create Amazing PowerPoint Slides using R - Part 2 (3)
Now that we have a few basic tools for manipulating PowerPoint slides, let’s use the power of R to build really amazing slides. In this post we’ll get some data that we will use in Part 3 for constructing a PowerPoint slide with lots of (needless) animation, and some interaction.
You should have the same R/RStudio setup and the packages we loaded in Part 1
Let’s get started.
Let’s face it, PowerPoint isn’t going anywhere. Even if you use R (or Python) for data analysis, PowerPoint is how you distribute and communicate results, and learning how to create those decks as part of your R workflow can pay off. Beyond efficiency, and repeatability, programmatic access enables you to do things that just aren’t possible with point and click. In this tutorial, we’ll learn how to automate PowerPoint using R.
In this talk, S Anand uses Python and pywin32 to create some jaw dropping effects in Powerpoint, scraping data from IMDB and creating a PowerPoint slide using the data. RDCOMClient by Duncan Temple Lang allows you to do the same thing using R. It provides the ability to “access and control applications such as Excel, Word, Power Point, Web browsers etc.”
We will recreate some elements of S Anand’s talk, and a few other things, with a focus on interaction and animation. We’ll learn the basics of accessing methods and properties of PowerPoint VBA objects, scrape data on Clint Eastwood’s movies from IMDB, and use it to create a slide. In the end, we should have a cool, fun PowerPoint slide with Clint Eastwood’s filmography.
Gratuitous animation ahead
(Clearly PowerPoint has a powerful animation engine, and the object model allows you to programmatically manipulate almost everything. The Microsoft documentation, however, seems to be organized as a challenge, at least for beginners.)
Let’s get started.
##Setup
I’m using:
- Windows 7 (64bit)
- Office 2013 (64bit)
- R version 3.1.3
- RStudio verison 0.98
We’ll need a few packages.
- RDOCMClient by Duncan Temple Lang. COM client that allows us to manipulate PowerPoint through R.
- XML by Duncal Temp Lang. Parse XML files.
- rvest by Hadley Wickham. Web scraping simplified.
Start RStudio and Load the packages:
install.packages(c("RDCOMClient","XML","rvest"))
library("RDCOMClient")
library("XML")
library("rvest")
##RDCOMClient Basics Some RDCOM basics we’ll need to know:
- To start a new instance of an application:
COMCreate("xxx.Application")
- Executing methods takes the form:
comObj$methodName(arg1,arg2,arg3,...)
- Setting properties takes the form:
myObj[["Property"]] = TRUE
With these basic capabilities, we can access and manipulate all the objects exposed in the PowerPoint API.
Let’s create a new PowerPoint presentation from R
# Start up PowerPoint
pp <- COMCreate("Powerpoint.Application")
# Make the application visible
pp[["Visible"]] = 1
# Add a new presentation
presentation <- pp[["Presentations"]]$Add()
# The presentation is empty. Add a slide to it.
slide1 <- presentation[["Slides"]]$Add(1,ms$ppLayoutBlank)
The critical part is, of course, knowing what methods and properties are available. This is where the PowerPoint 2013 Developer Reference is handy, but not terribly user-friendly. (You can also get this information from the Object Browser in the VBA editor.)
To understand how to go from VBA to R, let’s recreate one of the basic examples in the documentation, Applying Animations to Shapes in Office 2010, which works with Office 2013 as well.
The VBA code:
Sub TestPickupAnimation()
With ActivePresentation.Slides(1)
Dim shp1, shp2, shp3 As Shape
' Create the initial shape and apply the animations.
Set shp1 = .Shapes.AddShape(msoShape12pointStar, 20, 20, 100, 100)
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectFadedSwivel, , msoAnimTriggerAfterPrevious
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectPathBounceRight, , msoAnimTriggerAfterPrevious
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectSpin, , msoAnimTriggerAfterPrevious
' Now create a second shape, and apply the same animation to it:
shp1.PickupAnimation
Set shp2 = .Shapes.AddShape(msoShapeHexagon, 100, 20, 100, 100)
shp2.ApplyAnimation
' And one more:
Set shp3 = .Shapes.AddShape(msoShapeCloud, 180, 20, 100, 100)
shp3.ApplyAnimation
End With
End Sub
The code and the PowerPoint file created are available from Github.
Coming up:
Part 2 - We’ll scrape some data using XML
and rvest
and create a dataset of Clint Eastwood’s movies.
Part 3 - Then some fun! We’ll use the data to play around with more advanced animation and interaction in PowerPoint.