Export several plots from R into ppt

2020-07-20 04:26发布

问题:

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 .

回答1:

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" )


回答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) 


回答3:

eoffice may be another choice. with the command below:

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