R Function Of The Week : Writing Editable R objects to PowerPoint
This Article Contains
This week at Statistics Without Borders, we discuss exporting editable objects to Microsoft PowerPoint. Yes, you read that right: editable objects. Let me be transparent up front, editable objects does not mean a PowerPoint graph but what it does mean is vector objects that can be manipulated. We will be leveraging the
officer packages available on the CRAN repository.
Use case for rvg and
When pooled together, these packages make Microsoft application files much easier to manipulate and write to.
- The rvg package converts R objects to Vector Graphic Devices that can be interpreted by MS Excel, Word, and PowerPoint.
- The officer package allows manipulation of MS Files from the R interface.
If you are familiar with the Knitr approach, you will notice that object handling is very different using this method.
Before we can get to writing output, we have to ensure our PowerPoint is set up in a way that makes it easy to manipulate in R. Therefore, we will begin by installing the R packages, then pivot to PowerPoint set up and finally, wrap up the exercise by shifting back to R.
Let’s begin by installing both packages into our R environment like so:
##install packages from CRAN
One of my favorite things about R is that if a package is available on the CRAN repository, installation is a breeze (usually). Once we have the packages set up, let’s move on to the PowerPoint file portion. Typically with MS Word or MS Excel, writing objects tends to be relatively straightforward (a topic for another week) but with PowerPoint, R requires a little bit more guidance. This is because PowerPoint usually has stricter layouts. In any case, if you are trying to automate a presentation for example, it’s best to have things as exact as possible.
Let’s proceed by:
- Creating a sample PowerPoint with a Title slide and a Content slide
- Renaming the object on our content slide where our plot output will go
- Reading the sample PowerPoint into R and viewing available options
- Creating a graph using
ggplot()and placing it into our Content Slide
Sample PowerPoint Setup
I created a very simple presentation by opening up PowerPoint and creating a new Title slide. I then created a content slide by going up to the ‘Insert’ tab, hitting the ‘New Slide’ button and selecting the ‘Content with Caption’ layout option.
The content layout creates placeholders we can reference. Once we name the placeholders, we can tell R to place our object of interest into our placeholder.
Placeholders also assist with sizing objects because the object adopts the size of the placeholder so we do not have to specify exact dimensions. Technically, we can even create our own blank slide and add in placeholders as we see fit. Here is a screenshot of what my content slide lookslike with the default ‘Title and Content’ layout and the ‘Integral’ Office theme:
Renaming Placeholder Objects
Let’s say we want to add our R object into the container currently on the right side of our content slide and want to rename it for ease of use. For sake of thoroughness, we will rename this object in the slide master.
Open up the slide master by navigating to the ‘View’ section of the PowerPoint ribbon, then click the ‘slide master’ button. The screen should automatically change to the Master View. There may be several slides within this view that we did not see in our presentation; this is expected.
To rename our placeholder, let’s navigate to our slide if we are not already there and turn our attention towards the ‘Home’ section of the ribbon.
- Locate the ‘Arrange’ dropdown in the ‘Drawing’ section (in the ‘Home’ tab, 5th from the left) and click.
- In the ‘Arrange’ drop down, click ‘selection pane.’ A sidebar pops up on the right with some object names.
- Let’s select our container and double click on the automatically highlighted selection in the sidebar. The highlighted text in the sidebar is the current name of the object. Let’s now rename our container to ‘R Placeholder.’
Once you have typed in a name for the object, you can simply close out of the sidebar and Master View. Ensure the PowerPoint is saved in a convenient location. We are now done with the MS PowerPoint section and can shift back over to R.
Viewing the Presentation File in R Using Officer
In this section, we will load up our packages and read our presentation into R. Once we have done this, we can view the properties of our file to get a feel for how
Officer works. Next, we will identify the name of our content slide and briefly go over what some of the properties mean. If you are already familiar with the Officer package, feel free to skip to this section!
- Read in pptx file using
Officerpackage. Note, this will create a list object with identifying information regarding the PowerPoint
- Explore the presentation’s relevant attributes using
## Library loads
library(officer)## read in ppt
my_ppt <- read_pptx('C:/Users/Nanwe/OneDrive/Desktop/My R PowerPoint.pptx')
View(my_ppt %>% officer::layout_properties())
Running the second line in the ‘read in ppt’ section will display a DataFrame similar to the screenshot below. As you can see, this DataFrame is quite large. This is why giving our placeholder object a distinct name is super helpful. We can easily call/manipulate our placeholder without having to sift through the large list.
There are 11 columns in this particular presentation’s layout properties. These columns describe attributes of different objects within the presentations’ master slides. For this exercise, we will focus on the 2 that are most relevant to us.
- ph_label: Each object has a ‘placeholder label’ associated with it which can be found in this column. This is the column that will have our placeholder’s given name ‘R Placeholder.’ This is the name that R will use to place the object into a container. Each label has its own size and location on the PowerPoint slide.
- name: The name column contains the default layout names of the objects within our presentation. While the column is called ‘name,’ I personally consider this as more of a description because it clearly describes each object. This column is a great reference point when there are lots of labels to keep track of.
Let’s save this list into a DataFrame so we can easily search for our placeholder using dplyr’s
library(dplyr)## save to df
df <- my_ppt %>% officer::layout_properties()## Find Placeholder in df
df %>% filter(name=='R Placeholder')
Creating a Plot and Inserting it into the Presentation
We have finally made it to the fun part!
Let’s load up R’s default ‘mtcars’ dataset and create a simple line graph using ggplot2. We will plot a car’s weight against mpg and then insert that into our presentation.
## motor cars data set
data('mtcars')## Plotting weight against mpg
my_lg <- ggplot(mtcars, aes(x=wt, y=mpg)) + # initialize
geom_line() + # add line
xlab('weight') + ylab('miles per gallon') # axis title## view plot
Here is what our plot looks like:
Next, we will:
- Use the
ph_with()function to tell R where to place
my_lg. The function expects 2 arguments; An object value which can either be code to create a graph or the name of an existing ggplot object, and a location in the form of dimensions to place that R object.
- Use the
ph_location_label()function to tell R exactly where we want our object to go by referencing the name of our container.
- Wrap our ggplot object within the
dml()function from the rvg package. This call will convert the plot into a vector graphic that we can edit in PowerPoint.
- Lastly, using the
past0()functions, we will specify a name for our file and save it into our working directory.
## insert into ppt
my_ppt <- my_ppt %>% ph_with(dml(ggobj=my_lg), location = ph_location_label(ph_label = 'R Placeholder' ))## save/download ppt
print(my_ppt, paste0('my_R_presentation.pptx'))#NOTE: The 'ggobj=' argument must be specified within the dml call #so R recognizes we are passing an EXISTING ggplot object, not #trying to create one.
And we’re done! Just a few lines of code and we have a set of editable objects in our PowerPoint.
While our graph does not translate to a graph in PowerPoint, it is a set of editable objects. This means fonts, colors, sizes, borders etc. can be manipulated as needed. While not perfect, this approach offers a lot more flexibility than creating
.png outputs and copying and pasting them into a final presentation.
Meet the Author
Neha Anwer is a data science professional with domain experience in the financial advisory field. She has been a volunteer at Statistics Without Borders for over a year and most recently joined the SWB Marketing & Communications team. In her spare time, Neha enjoys traveling, curling up with a fiction read, and spending time outdoors.