title: R: Function to export currently active R plot to Powerpoint/Word/LibreOffice
body: <p>Currently I always export my R graphs to Powerpoint in vector format using the excellent <code>ReporteRs</code> package, as in</p>
  
  <pre><code>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") 
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/LEJyU.png" alt="enter image description here"></p>
  
  <p>What I find inconvenient in this workflow is that I now have to wrap my plot commands in a function (with <code>print()</code> for <code>lattice</code> or <code>ggplot2</code> plots, or just <code>return(plot())</code> 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, <code>lattice</code> plot or <code>ggplot2</code> plot) and then define a function <code>export2PPT</code> that captures the output of your current graphics device as a function and passes it on to <code>ReporteRs</code> and <code>addPlot</code> to export your plot as Powerpoint (centered &amp; appropriately scaled), in analogy to function <code>dev.copy2pdf</code> that works in the same way to export to PDF? </p>
  
  <p>And once we have such a function, perhaps even <a href="https://stackoverflow.com/questions/16317706/redefining-a-function-in-an-r-package">modify the <code>grDevices</code> source code</a> of functions <a href="https://github.com/wch/r-source/blob/trunk/src/library/grDevices/R/windows/windows.R" rel="nofollow noreferrer">windows() and savePlot()</a> to have an additional command File...Save as Powerpoint if one was using the <code>windows()</code> device? Or is this something for the R Core people to do? </p>
  
  <p>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)</p>
  
  <p>Anybody who would know how to do this? </p>
  
  <p>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
  <a href="https://cran.r-project.org/web/packages/export/index.html" rel="nofollow noreferrer">https://cran.r-project.org/web/packages/export/index.html</a> and
  <a href="https://github.com/tomwenseleers/export" rel="nofollow noreferrer">https://github.com/tomwenseleers/export</a> for demo usage. The package now uses the officer package as a backend, as ReporteRs is deprecated...</p>