Export graphic window to powerpoint

Original by Reinie, 2018 

This summary note was Posted on

Export any graphics produced by base or ggplot by capturing the graphical window. Ideal for quick export when you need one graphic for your presentation.

The function
You need library("ReporteRs")

exportNOW  <- function(plotname="plot", scale=1, open=TRUE){

  captureplot = function() {p = invisible(recordPlot()); return(p)}
  p = captureplot()
  plotsize = dev.size()
  myplot = function(pl=p)print(pl)
  plname = paste0(plotname,".pptx")

  ReporteRs::pptx() %>% 
  ReporteRs::addSlide(slide.layout = 'Two Content')  %>%
  ReporteRs::addPlot(myplot, offx = 0.5, offy = 0.5, 
                   pointsize = 6,fontname_sans = "Arial", 
                   width=plotsize[1]*scale, height=plotsize[2]*scale) %>%
  ReporteRs::writeDoc(plname ) 

  if (open == TRUE) system2('open', args = plname, wait = FALSE);
}

It will save the plot intot plot.pptx by default and it will open powerpoint directly after calling it. You can name the file or rescale the output graphic

Exemple:
To export to plot.pptx.
exportNOW()
To export to myexportedplot.pptx with a rescaling of half the size and not opening powerpoint directly.
exportNOW("myexportedplot", scale = 0.5, open = F)

Sources:

  • For more advance export you can check out the Tom Wenseleers’ “export” code on github. There is no CRAN package available yet
  • There is also a discussion on stackoverflow on the subject