Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ReporteRs: Editing Existing Slides

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?

like image 290
user3390169 Avatar asked Feb 02 '15 19:02

user3390169


Video Answer


2 Answers

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

like image 197
cdeterman Avatar answered Sep 29 '22 14:09

cdeterman


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)
like image 28
Andri Signorell Avatar answered Sep 29 '22 14:09

Andri Signorell