Regardless of the tools used for data analysis, normally the way to display the results is a Word document or a PowerPoint presentation.
In this post, we will create a PowerPoint presentation and insert a series of graphics and text programmatically, using the OfficeR and rvg packages together. We will also take advantage of the occasion to present (for those who do not know) the ‘Pipe’ operator, very useful when nesting functions.
The OfficeR package allows you to access and manipulate Word documents and PowerPoint presentations, while rvg allows us to insert the R visualizations as Office objects that we can manipulate from the document.
Let us create a blank PowerPoint template, without slides. Then, using R, we will add slides with text and graphics.
Warning: the purpose of this post is not to produce a ‘nice’ presentation, but to show the functionality.
We will need to have the following packages installed:
These packages will install others because they have dependencies. We load the packages:
library(officer) library(magrittr) library(ggplot2) library(rvg)
We will load in the R session the PowerPoint template that we saved before, and we will check the information of the ‘layout’ of the rpptx object with ‘layout_summary’, this function is also part of the OfficeR package and gives us information about the design of the slides.
my_presentation <- read_pptx("C:/BlogR/template.pptx") layout_summary(my_presentation)
This gives us:
layout master 1 Title Slide Office Theme 2 Title and Vertical Text Office Theme 3 Vertical Title and Text Office Theme 4 Title and Content Office Theme 5 Section Header Office Theme 6 Two Content Office Theme 7 Comparison Office Theme 8 Title Only Office Theme 9 Blank Office Theme 10 Content with Caption Office Theme 11 Picture with Caption Office Theme
If we wanted to change something in the master template.pptx, we should open the PowerPoint presentation and in ‘Slide Master’ in ‘View’, ‘Master Layout’ change the designs:
Next, we will define a series of variables with the content that we are going to include in the presentation:
title1 <- "Summary" title2 <- "Chart from image" title3 <- "Editable chart using barplot()" intro <- "In this post we are playing around to create and edit a Powerpoint presentation using R" text1 <- "This visualization is embeded from an image file" text2 <- "Now, this is an editable visualization created using barplot()" footer <- "SolidQ 2017"
Parenthesis before continuing: The ‘Pipe’!
Before moving on, we will comment on the operator ‘%>%’ so that the code is clearly understood below. This operator belongs to the magrittr package and allows us to nest the code. To see it clearly, the best is a simple example:
Suppose I want to solve y = cos (log (x)), we would solve this as:
y <- x %>% log() %>% cos()
As we see it is a way of nesting the code by writing ‘from the inside out’ and sometimes it can be much more comfortable to write, and of course, to read.
If we look at the pipe in R we will find multiple references to the F# language, but if you have not had the fortune to use this language (as is the case of a server), it may sound more ‘|’ of Shell or Terminal, where commands are nested.
Now that we know%>% we are going to add a slide to our presentation:
Let us review the flow that can be seen in the code above.
– We read and pass the template.pptx file to the my_presentation object.
– We add a slide with add_slide; we tell it to use the ‘Title Slide’ layout and the ‘Office Theme’ master.
– With ph_with_text, we add the text of type ‘ctrTitle’, that is the title.
– In the same way, we add a subtitle ‘subTitle’ and a footer ‘ftr’
Continuing in the same way we will add more slides with text and graphics, highlight ph_with_img () to which we pass the image path and ph_with_vg () to which we pass the R graphic directly. We will use random data as an example, because in this R session, we do not have any sample dataset loaded, but it helps us to illustrate the idea.
my_presentation <- # Load template read_pptx("C:/BlogR/template.pptx") %>% # Add a slide add_slide(layout="Title Slide", master="Office Theme") %>% # Add title (ctrTitle) ph_with_text(type = "ctrTitle", str = "My first PPT made in R!!") %>% # Add subtitle (subTitle) ph_with_text(type = "subTitle", str = "A simple way to embed R visualizations in Office!!") %>% ph_with_text(type = "ftr", str = footer ) %>% # Summary Slide, add text using variables: add_slide(layout = "Title and Content", master = "Office Theme") %>% ph_with_text(type = "title", index = 1, str = title1) %>% ph_with_text(type="body",str = intro ) %>% ph_with_text(type = "ftr", str = footer ) %>% ph_with_text(type = "sldNum", str = "1" ) %>% ph_with_text(type = "dt", str = format(Sys.Date(),"%B %d,%Y")) %>% # Slide with Chart from an image: add_slide(layout = "Content with Caption", master = "Office Theme") %>% ph_with_text(type = "title", index = 1, str = title2) %>% ph_with_text(type = "body", index = 2,str = text1) %>% ph_with_img(type = "body", index = 1, src = "C:/BlogR/visualization.PNG") %>% ph_with_text(type = "ftr", str = footer ) %>% ph_with_text(type = "sldNum", str = "2" ) %>% ph_with_text(type = "dt", str = format(Sys.Date(),"%B %d,%Y")) %>% # Slide with chart from R add_slide(layout = "Content with Caption", master = "Office Theme") %>% ph_with_text(type = "title", index = 1, str = title3) %>% ph_with_text(type = "body", index = 2,str = text2) %>% ph_with_vg(code = barplot(sample(1:100,15),xlab="Day",ylab="SaleAmount"), type = "body") %>% ph_with_text(type = "ftr", str = footer ) %>% ph_with_text(type = "sldNum", str = "3" ) %>% ph_with_text(type = "dt", str = format(Sys.Date(),"%B %d,%Y")) %>% print( target = "C:/BlogR/officer_sample.pptx") %>% invisible()
And in particular in the last slide, we will have a graphic that is editable from PowerPoint.
This is all for today, this is just another resource among the ‘self-documenting’ capabilities offered by R.
It is up to the consumer how to take advantage of these functionalities.
We would like to thank the sources for the content of this blogpost: