22

Currently I always export my R graphs to Powerpoint in vector format using the excellent ReporteRs package, as in

library(ReporteRs)
myplot = function() print(qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7)))
doc = pptx()
doc = addSlide(doc, slide.layout = "Blank") 
doc = addPlot( doc, myplot, vector.graphic = TRUE, fontname="Arial",
               offx = 0.1*dim(doc)$slide.dim["width"], offy = 0.05*dim(doc)$slide.dim["height"], 
               width = 0.8*dim(doc)$slide.dim["width"], height = 0.9*dim(doc)$slide.dim["height"])
writeDoc( doc, "plot.pptx") 

enter image description here

What I find inconvenient in this workflow is that I now have to wrap my plot commands in a function (with print() for lattice or ggplot2 plots, or just return(plot()) for base R plots), and that I don't like having to manually specify the offsets and size (I usually prefer to just get a nearly page filling, centered graph in my slide). I was just wondering if instead it wouldn't be possible to make your plot first (base R plot, lattice plot or ggplot2 plot) and then define a function export2PPT that captures the output of your current graphics device as a function and passes it on to ReporteRs and addPlot to export your plot as Powerpoint (centered & appropriately scaled), in analogy to function dev.copy2pdf that works in the same way to export to PDF?

And once we have such a function, perhaps even modify the grDevices source code of functions windows() and savePlot() to have an additional command File...Save as Powerpoint if one was using the windows() device? Or is this something for the R Core people to do?

Especially for use in the classroom this would be super handy I think, given the dominance of MS Office/LibreOffice. (as Powerpoint is much easier to edit than PDF, allowing one to easily make small changes in the final layout of R graphs, and vector output is at least as good as PDF, plus transparency is also fully supported - for PDF I find that import in Inkscape is often a bit buggy, unless one uses Adobe Illustrator)

Anybody who would know how to do this?

EDIT: In the meantime I found the solution, and have wrapped export functions to export the currently active R plot to powerpoint in a package on CRAN, see https://cran.r-project.org/web/packages/export/index.html and https://github.com/tomwenseleers/export for demo usage. The package now uses the officer package as a backend, as ReporteRs is deprecated...

3
  • For editing in Inkscape, I'd usually export in .svg. For the second part of your question, are you talking about RGui, RStudio or something else? Jul 4, 2015 at 20:29
  • Thanks for pointing that out - I was under the impression that .svg did not support transparency/alpha channels, but apparently now it does... Definitely seen cases where svg export also gets screwed up at times though... And still prefer editing in Powerpoint, especially for simple things. Second part of my question was about how one could implement a File...Save As Powerpoint tab using the function below, either in the plot tab of RStudio, or the interactive window you get when you use windows() and then make your plot in that (under Windows) Jul 4, 2015 at 20:57
  • I second the suggestion that an "export to Powerpoint" button or a simple-to-use function would be handy. I know many people that are more familiar with Powerpoint than Illustrator, Inkscape etc (and who prefer to make all the fine adjustments in mouse-driven software), and I'm sure this function would see plenty of use.
    – lukeholman
    Jul 5, 2015 at 16:59

2 Answers 2

37

Just made a new CRAN package export that allows one to export an R plot in the currently active graphics device to either Powerpoint or Word (or LibreOffice Impress/Writer) in editable (DrawingML) vector format as a one-liner, with full support for transparency, etc, see https://cran.r-project.org/web/packages/export/index.html and demo at https://github.com/tomwenseleers/export

For example:

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

Example to export lattice plot:

library(effects)
fit=lm(prestige ~ type + income*education, data=Prestige)
plot(Effect(c("income", "education"), fit),multiline=T, ci.style="bands")
graph2ppt(file="effect plot.pptx", width=7, height=5)

enter image description here

which after right clicking in PPT on "ungroup" can be seen to be nicely in vector format:

enter image description here

From Word or PPT these graphs also export perfectly to (vector format) PDF by using File...Save as...PDF, and it is much easier to make minor tweaks to the layout than is possible by editing directly in the PDF.

If the R Core or RStudio people would like to include this functionality please do - would be super handy for use in the classroom I think, given the dominance and widespread use of the Office suite!

5
  • Maybe it's because this is an old post, but I'm still struggling with that. I tried your solutions and I get Error in addPlot.docx(doc, myplot, vector.graphic = vector.graphic, fontname = fontname, : argument 4 matches multiple formal arguments
    – edd
    Mar 23, 2018 at 9:00
  • 1
    @TomWenseleers is it possible to submit your package to a CRAN repository ? You probably would have to change the name as export is maybe a reserved name, to like tomexport or TWexport. or Qwickexport. I think it would be beneficial for those who can not install from github (like me..hey hey)
    – R. Prost
    Apr 25, 2018 at 7:20
  • 1
    Just to let you all know that these export functions are now out on CRAN, see cran.r-project.org/web/packages/export/index.html Nov 1, 2018 at 7:35
  • Package ‘export’ was removed from the CRAN repository.
    – dpel
    Sep 29, 2020 at 11:06
  • 1
    @dpel Export is back up on CRAN now: cran.r-project.org/web/packages/export/index.html Dec 7, 2022 at 17:00
1

You can try the eoffice package which also available in CRAN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.