Search code examples
rpowerpointreporters

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?


Solution

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