16

Is there a good way to use data from R and a package like ReporteRs to generate complete Powerpoints? I have about 900 slides to create. Our analysts currently follow this path:

DB --> SAS --> CSV --> PPTX (embedded graphics) (x900 times)

This is manual, open to lots of errors and slow.

Ideally, I would prefer:

DB --> R + ReporteRs --> PPTX (x1 time)

The issue is 2-fold. First, our clients (unreasonably) prefer PPTX over a web or even PDF format. Second, R graphics cannot be edited in PPTX, and are sometimes not ideally sized/formatted, especially in terms of axis text sizes. So is there a way to use R to create editable Powerpoint graphics, hyperlinked tables of contents, etc? If not that, is there at least a good set of ggplot2 templates to use with decent PPTX presentation formatting?

Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
Serban Tanasa
  • 3,592
  • 2
  • 23
  • 45
  • Have you seen this? https://support.rstudio.com/hc/en-us/articles/200486468-Authoring-R-Presentations – darwin Jan 09 '15 at 19:00
  • I use devEMF und emf(...) to get graphics, that I can modify in PPTX... but its not real automatic ... and it seems to be removed from CTAN :( http://cran.r-project.org/web/packages/devEMF/ ... Powerpoint only knows WMF and EMF I think (as vector format) – ckluss Jan 09 '15 at 19:18
  • @darwin, i know of it, but our clients are wedded to .PPTX for some reason – Serban Tanasa Jan 09 '15 at 19:34
  • To edit .pptx programmatically you could use vbs/vba. – Marek Jan 09 '15 at 19:53
  • 1
    The statement that PPT only can accept EMF and WMF as vector formats is incorrect. (It was developed on the Mac platform.) Look at `setEPS()` and `postscript()` documentation for methods of creating .eps files, which are accepted by PPT, vector mapped, and transparent at least in their "white" areas. I just tested for ability to use transparency in rgb colors and that worked on a Mac. – IRTFM Jan 09 '15 at 20:11
  • **Everything** is editable if you have the right tools. If they're really insisting on editing the figures inside the presentation, then you've got a fool for a client (or one who is incapable of trusting you). – Carl Witthoft Jan 09 '15 at 20:29
  • It's not **mandatory** that they be editable, if we can get the text formatting in graphs just right. If I need to, I will spend a few days designing a few ggplot2 theme. Before I do that, I just wanted to check that I'm not reinventing the wheel, that's all. – Serban Tanasa Jan 09 '15 at 20:56
  • Solution was staring me in the face all along! Thanks everyone for the comments nonetheless! – Serban Tanasa Jan 09 '15 at 21:44
  • The ReporteRs package is now deprecated and has been replaced by officer. I just created a utility package export to easily export lots of R graphs to Powerpoint, it just came out on CRAN - see https://cran.r-project.org/web/packages/export/index.html and https://github.com/tomwenseleers/export – Tom Wenseleers Nov 01 '18 at 07:48

2 Answers2

28

Solved. Turned out to be a severe case of "Not Reading the Manual." The solution is to use the ReporteRs R package AND read the manual. :)


The Manual:

addPlot {ReporteRs}
addPlot(doc, fun, pointsize = 12, vector.graphic = F, ...)
vector.graphic  
logical scalar, if TRUE, vector graphics are produced instead of PNG images.
SVG will be produced for bsdoc objects and DrawingML instructions for docx and
pptx objects.  
DrawingML instructions offer advantage to provide editable graphics
(forms and text colors , text     contents, moving and resizing is disabled).

The key paragraph: DrawingML instructions for [...] pptx objects. DrawingML instructions offer [the] advantage [of] provid[ing] editable graphics.

So simply set vector.graphic=TRUE and you're set.

I am now able in Powerpoint to edit graphics created in R: legends, axis text, all graphical symbols. Everything. This is Xmass come early! Thank you ReporteRs creators! I can now do in 3 hours what would have taken 300 before! Amazing.

Full worked out example below:

library( ReporteRs )
require( ggplot2 )
mydoc = pptx(  )
mydoc = addSlide( mydoc, slide.layout = "Title and Content" )
mydoc = addTitle( mydoc, "Plot examples" )
myplot = qplot(Sepal.Length, Petal.Length
, data = iris, color = Species
, size = Petal.Width, alpha = I(0.7)
)
# Add titles and then 'myplot'
mydoc = addPlot( mydoc, function( ) print( myplot ), vector.graphic=TRUE)  
writeDoc( mydoc, file = "~/CustomReport.pptx" )

Result: enter image description here

Community
  • 1
  • 1
Serban Tanasa
  • 3,592
  • 2
  • 23
  • 45
  • Upvote for demonstration. Took me a bit of debugging because I had a non-standard `iris` version in my workspace, but I was able to get a Mac version of R to write a Mac-readable .pptx file. The format is SVG, written by the `svg()`-function described on the 'cairo' page of the grDevices-package. – IRTFM Jan 31 '15 at 21:43
  • I'm not able to find any option on ReporteRs to create animation/transition/effects. It seems only to be able to create static content. – skan May 31 '16 at 17:11
  • 1
    @skan See [this article](https://support.rstudio.com/hc/en-us/articles/200714013-Slide-Transitions-and-Navigation). The output is not in pptx though. – Serban Tanasa Jun 01 '16 at 15:55
  • 1
    Maybe I didn't use the proper words. How can you create an slide like above but where the points (blue and green) appear one per one, every time you press a button? – skan Jun 02 '16 at 09:28
  • 1
    @ Skan: You can do this manually in powerpoint after exporting the ggplot2 object from R to *.pptx like Serban described. The exported image is fully editable. – Matthias Jul 15 '16 at 13:30
  • 1
    The ReporteRs package is now deprecated and has been replaced by officer. I just created a utility package export to easily export lots of R graphs to Powerpoint, it just came out on CRAN - see https://cran.r-project.org/web/packages/export/index.html and https://github.com/tomwenseleers/export – Tom Wenseleers Nov 01 '18 at 07:49
2

You can use my new export package that just came out on CRAN to easily export to Office (Word/Powerpoint) in native Office vector format, resulting in fully editable graphs, see https://cran.r-project.org/web/packages/export/index.html and https://github.com/tomwenseleers/export

For example:

install.packages("export")
library(export)

?graph2ppt
?graph2doc

library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))
graph2ppt(file="ggplot2_plot.pptx", width=7, height=5) 
graph2doc(file="ggplot2_plot.docx", width=7, height=5) 
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103