4

I found a function here to create a ppt with a slide for a plot created in R. Here is the link to that function: R: Function to export currently active R plot to Powerpoint/Word/LibreOffice

I would like my program to add several slide (containing one plot each).

I currently use : export2ppt(file="plot.pptx") But I can't figure out how I add a second plot to the same file .

Community
  • 1
  • 1
user2506015
  • 125
  • 1
  • 6
  • `addPlot(` which used in your fucntion . added new list with plot , so you can use it more than one times if wont to add additional plot – Batanichek Apr 11 '16 at 09:55
  • here you create doc -- you need it only one time per file `if (type=="PPT") {doc = pptx();doc = addSlide(doc, slide.layout = "Blank");pagesize = dim(doc)$slide.dim} else {doc = docx();pagesize = dim(doc)$page-dim(doc)$margins[c(4,3)]} pageaspectr = pagesize["width"]/pagesize["height"]` – Batanichek Apr 11 '16 at 09:57
  • I saw that but thought that reusing the function export2ppt(file="plot.pptx") a second time will add a new slide. Instead, it overrides the first pptx created with one new with one new graph inside. – user2506015 Apr 11 '16 at 13:29
  • yes because in your function you create doc every time. so you need to edit fucntion or use it like `doc=pptx()` then `addSlide()`+`addPlot()` for each your plot and at end `writeDoc()` – Batanichek Apr 11 '16 at 13:52
  • The answer below is outdated, as ReporteRs has been removed from CRAN and is superseded by officer. I just made a new package export built on top of officer that easily allows one to export several graphs to a single Powerpoint presentation – Tom Wenseleers Nov 03 '18 at 23:26

3 Answers3

4

Try for example

library(ReporteRs)
doc =pptx( ) # create pptx
doc=addSlide(doc,"Title and Content") # add slide
doc<-addTitle(doc,"first") # add title
fun_1<-function(){
  plot(mpg ~ wt,  data = mtcars)
}
doc <- addPlot(doc, fun= fun_1,vector.graphic =FALSE )  # add plot

doc=addSlide(doc,"Title and Content") # add slide
doc<-addTitle(doc,"Second") # add title

fun_2<-function(){
  plot(mpg ~ cyl,  data = mtcars)
}
doc <- addPlot(doc, fun= fun_2,vector.graphic =FALSE ) # add plot
writeDoc(doc, "r-2.pptx" )
Batanichek
  • 7,761
  • 31
  • 49
  • 1
    I had to install from github because I couldn't find it on CRAN. However, when I am trying to use the code, I got this warning/error: `Please consider using package officer instead of package ReporteRs. Java <= 1.8 is needed for this package but not available` and `Function pptx is deprecated, please use officer::read_pptx() instead.Error in pptx() : java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException` – emr2 Nov 11 '21 at 10:48
2

The answer below is outdated, as ReporteRs has been removed from CRAN and is superseded by officer. I just made a new package export built on top of officer that easily allows one to export several graphs to a single Powerpoint presentation using the graph2ppt() command and the append=TRUE option, e.g. to produce a presentation with 2 slides :

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="plots.pptx", width=6, height=5) 
qplot(Sepal.Width, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="plots.pptx", width=6, height=5, append=TRUE) 
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
  • Getting error : Error in add_slide(doc, layout = "Blank", master = "Office Theme") : could not find layout named "Blank" in master named "Office Theme" – Soumya Boral Dec 21 '19 at 08:27
1

eoffice may be another choice. with the command below:

install.packages("eoffice")
topptx(file="plots.pptx", width=6, height=5,append=T)
bioguo
  • 61
  • 4