简体   繁体   English

使用 R 包 OFFICER 和 RVG 将 plot 从 R 导出到 pptx 文件时无法指定幻灯片大小

[英]Unable to specify slide size when exporting a plot from R to a pptx file using R packages OFFICER and RVG

I have created a plot in R, and exported it as a vector object in a powerpoint slide (pptx file), using the "rvg" and "officer" packages (see code below).我在 R 中创建了一个 plot,并将其导出为矢量 object 在 powerpoint 幻灯片(pptx 文件)中,使用“rvg”和“officer”包(下面的代码)

I would like to define the dimensions (height and width) of the slides in the pptx create by the code, but I cannot see an option to do this.我想在代码创建的 pptx 中定义幻灯片的尺寸(高度和宽度),但我看不到执行此操作的选项。 It is possible to specify the size of the plot object exported, but not the size of the powerpoint slide size.可以指定导出的 plot object 的大小,但不能指定 powerpoint 幻灯片大小的大小。

Is it possible to define the size of the powerpoint slide when exporting a plot from R into a pptx file?将 plot 从 R 导出到 pptx 文件时,是否可以定义 powerpoint 幻灯片的大小?

Many thanks for advice/help非常感谢您的建议/帮助

#EXAMPLE CODE
#IMPORT LIBRARIES
library(officer)
library(rvg)
#MAKE RANDOM DATA
x<-rnorm(50,1,10)
y<-rnorm(50,2,20)
#WRAP PLOT CODE IN A FUNCTION
plot.code<-function(){
  plot(x,y)
}
#CREATE AND EXPORT PPTX FILE
export.plot<-read_pptx()
export.plot <- add_slide(export.plot, "Title and Content", "Office Theme")
export.plot<-ph_with_vg(export.plot,code=plot.code(),type="body",height=10,width=10)
 #height and width in the above code define the size of the plot, not the slide
print(export.plot, target = "export.plot.pptx")

I have the same question, and haven't yet found a clear answer, but I did figure out a shoddy work-around.我有同样的问题,还没有找到一个明确的答案,但我确实想出了一个伪劣的解决方法。

  1. Open Powerpoint and save an empty presentation with slides the size that you want.打开 Powerpoint 并保存带有所需大小的幻灯片的空演示文稿。
  2. In R, read in the powerpoint that you just created.在 R 中,阅读您刚刚创建的 powerpoint。
  3. Add the content in R添加R中的内容
  4. Save powerpoint in R.在 R 中保存 powerpoint。

That will look something like this, with a previously created manually_created.pptx .这看起来像这样,带有以前创建manually_created.pptx

doc <- officer::read_pptx("manually_created.pptx") %>%
        # remove the empty slide
        officer::remove_slide(1)

doc %>% 
   # add a new slide to the powerpoint
   officer::add_slide("Title and Content", "Office Theme") %>% 
   # add plot to the slide
   officer::ph_with_vg(code = plot.code(), type = "body", height = 10, width = 10) %>%
   # save the powerpoint
   officer:::print.rpptx("manually_created.pptx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM