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 .

5
  • 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 Commented Apr 11, 2016 at 9: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"] Commented Apr 11, 2016 at 9: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. Commented Apr 11, 2016 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() Commented Apr 11, 2016 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 Commented Nov 3, 2018 at 23:26

4 Answers 4

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" ) 
Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

Getting error : Error in add_slide(doc, layout = "Blank", master = "Office Theme") : could not find layout named "Blank" in master named "Office Theme"
1

eoffice may be another choice. with the command below:

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

Comments

0

I wanted to give an updated example using the officer package. You can batch create multiple slides of plots from a list of plots. Here is a working example that saves an output.pptx to your working directory.

#Load packages library(officer) library(ggplot2) #Here is an example list of plots plot_list<- list() plot_list[[1]] <- ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point() plot_list[[2]] <- ggplot(mtcars, aes(x=factor(gear))) + geom_bar() plot_list[[3]] <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot() # Create temporary .pptx file file <- tempfile(fileext = ".pptx") #Create a blank pptx object pptx <- read_pptx() #add slides to the pptx object in a loop for(i in 1:length(plot_list)){ pptx <- add_slide(pptx) pptx <- ph_with(pptx, value = plot_list[[i]], location = ph_location_fullsize() ) } #print ("save") the pptx object to the tempfile print(pptx, file) #move the tempfile to current directory from the tempfile location. destination_file <- file.path(getwd(), "output.pptx") file.rename(file, destination_file) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.