问题
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?
回答1:
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)
回答2:
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).
回答3:
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.
来源:https://stackoverflow.com/questions/28285380/r-reporters-editing-existing-slides