R ReporteRs:编辑现有幻灯片

R ReporteRs: Editing Existing Slides

我有一个 pptx 格式的演示文稿,我需要用我用 R 脚本生成的图表经常更新它。我想自动替换图表,而不必多次在屏幕之间复制和粘贴。我一直在玩 ReporteRs 包,它看起来很有希望,但我不知道如何简单地替换演示文稿中已有的图表。 ReporteRs 上的所有文档都表明您必须添加一张新幻灯片,然后将您的图表放在该新幻灯片上。有没有办法说 'delete the graph on slide 7 and replace it with graph XXX?' ReporteRs 是最好的软件包?

使用 ReporteRs(以前的 R2DOCX),我相信您可以在创建 Word 文件时使用书签来定位和插入图,并且 [= =26=]PowerPoint。

您还应该查看 DescTools 包。很容易学,也很能干,比ReporteRs还简单。

您可以创建一个模板并在模板中完成所有 headers 和写作。然后,您可以在要插入 R 图的位置放置带有 Insert/Bookmarks 的书签。您必须将绘图保存到与书签同名的 R object 中。然后,每次重新运行代码时,DescTools 都会从模板开始并将绘图插入正确的位置。

此代码段通过从模板创建 "report" 来启动流程。

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

使用此工作流程,您可以随心所欲地移动 text-plus-plot-bookmarks 并在模板中的 Word 中进行文字处理,然后让 R 插入图表。我有代码将每个图放在一个列表中,最后 R 遍历列表并创建和插入每个图。

现在,你是否可以在 PowerPoint 中做类似的事情,我不知道。

根据ReporteRs文档,这应该比较简单。正如@lawyeR 所说,它是关于 'bookmarks' 的。您可以从软件包作者 here.

处找到示例

举个例子,几乎一字不差,link 代码类似于:

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

如下所述,维护者已修复该错误。您现在可以更换幻灯片。正如您所注意到的,这确实会替换整张幻灯片。虽然一开始有点不方便,但您可以轻松设置一个脚本,为幻灯片添加相同的标题、文本等,并且可以轻松地多次复制幻灯片。有了这个,您还可以在发生变化时快速更改任何文本(深思熟虑)。

尝试:

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)