6

I have a presentation in pptx format that I need to update frequently with graphs that I generate with an R script. I would like to automate the replacement of the graphs without having to copy and paste between screens a whole bunch of times. I have been playing with the ReporteRs package and it seems promising but I cannot figure out how to simply replace the graphs that are already in the presentation. All of the documentation on ReporteRs indicates that you have to add a new slide and then place your graphs on that new slide. Is there a way to say, 'delete the graph on slide 7 and replace it with graph XXX?' Is ReporteRs the best package for this?

user3390169
  • 1,015
  • 1
  • 11
  • 34

3 Answers3

3

According to the ReporteRs documentation, this should be relatively simple. As @lawyeR says, it is with respect to 'bookmarks'. You can find examples from the package author here.

As an example, nearly verbatim, from that link the code would be similar to this:

mydoc = pptx(template = 'examples/pp_simple_example.pptx' )

myplot = qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))

# This is the important line, note the 'bookmark' pertaining to slide
mydoc = addSlide( mydoc, slide.layout = 'Title and Content', bookmark=2)

# change title
mydoc = addTitle( mydoc, 'my new graph')

# add the plot
mydoc = addPlot( mydoc, print, x = myplot )

# save changes to new file
writeDoc( mydoc, 'examples/pp_replacement.pptx' )

As mentioned below, the maintainer has fixed the bug. You can now replace slides. As you note, this does replace the entire slide. Although a little inconvenient at the start, you can easily set up a script to add the same title, text, etc. to the slide and you can easily replicate the slide many times. With this, you can also rapidly change any of the text if something changes (food for thought).

cdeterman
  • 19,630
  • 7
  • 76
  • 100
  • This code replaces my second slide with a blank slide that has myplot on it. Am I doing something wrong? I was thinking that it would insert myplot into what I already had on slide 2 or replace the picture that is on slide 2. – user3390169 Feb 03 '15 at 00:17
  • @cdeterman your code is ok, it was an issue in ReporteRs. The fix is on github. And no, the extra line is not necessary – David Gohel Feb 03 '15 at 12:24
  • @user3390169 the method replace the whole slide with a new slide. – David Gohel Feb 03 '15 at 12:26
  • @user3390169, do my edits and additional comments address your concerns? – cdeterman Feb 03 '15 at 14:57
  • 1
    @cdeterman unfortunately not, I was hoping to keep the text on a slide that already exists and just change the graphs when I need to. Your code seems to replace the whole slide with a blank slide that has my graph on it. – user3390169 Feb 03 '15 at 15:45
  • 1
    @David Gohel is it possible to do this what I was asking with reporteRs? – user3390169 Feb 03 '15 at 15:45
  • @user3390169 unfortunately no – David Gohel Feb 03 '15 at 15:51
  • @user3390169, I understand you are looking for the perfect convenience here but it shouldn't be too difficult to create the script to recreate the text as I mention above. Perhaps the selective replacement will eventually be added to ReporteRs if David wants to. Until then, this is the best you can do unless you contribute the functionality to the package yourself. – cdeterman Feb 03 '15 at 15:58
2

Try:

library(DescTools)

# create a new PP instance
pp <- GetNewPP()

# create your plt and insert into pp
barplot(1:5)
pic <- PpPlot(width=10, height=5, pp=pp)

# add a new slide
PpAddSlide()

# new plot on new slide, just to make it difficult to go back
barplot(10:5)
pic2 <- PpPlot(width=10, height=5, pp=pp)


# get a collection of slides
slides <- pp[["ActivePresentation"]][["Slides"]]

# maybe convenient to go back to slide 1
slides$Item(1)$Select()

# get a handle to slide 1
slide1 <- slides$Item(1)

# get handle to any pic on the slide
pic1 <- slide1[["Shapes"]]$Item(1)

# delete it
pic1$Delete()

# create and insert a new one
barplot(rep(1,5), col="red")
PpPlot(width=10, height=5, pp=pp)
Andri Signorell
  • 1,279
  • 12
  • 23
  • 1
    Thanks Andri, this gets me most of the way there. Is there a place where all the attributes of [["Shapes"]] can be found? I got lucky and used pic1$Height() to find the height of the picture before deleting it but I cannot figure out how to find the xy position. – user3390169 Feb 04 '15 at 18:26
  • Well, in PP there's no macro recorder, so you have to explore the vba object model on foot. Click on the menu view/macros, then create a new macro and press F2. This will bring up the object model. But for setting the position you can use the x and y arguments in PpPlot(). See ?PpPlot for other additional arguments. – Andri Signorell Feb 04 '15 at 20:01
1

With ReporteRs (formerl R2DOCX), I believe you can use bookmarks when creating Word files to locate and insert plots, and there may be an equivalent in PowerPoint.

You should also look at the DescTools package. It is pretty easy to learn and is quite capable, in fact easier than ReporteRs.

You can create a template and do all your headers and writing in the template. You can then place bookmarks with Insert/Bookmarks where you want R plots to be inserted. You have to save your plots to an R object named the same as the bookmark is named. Then, each time you rerun your code, DescTools starts with the template and inserts the plots in the right places.

This snippet starts the process by creating "report" from the template.

library(DescTools)
library(RDCOMClient) 
report <- GetNewWrd(template="C:/Users/[your path to the template.docx")

With this workflow, you can move around text-plus-plot-bookmarks all you want and do the word processing in Word in the template, then have R insert the plots. I have code that puts each plot in a list and at the end R runs through the list and both creates and inserts each of the plots.

Now, whether you can do something similar in PowerPoint, I don't know.

lawyeR
  • 7,488
  • 5
  • 33
  • 63