使用 rvg 将 R 基础图形导出到 PowerPoint
Export R base graphics using rvg to PowerPoint
我正在尝试使用 officer 包来生成包含 R 基本图形的 PowerPoint 文档,最好不要使用固定分辨率,而是作为可编辑的矢量图形。这是我一直在尝试的方法,但生成的 PowerPoint 文档仍然缺少图表(中间 .dml
已创建并包含一些 XML)。
library (officer)
library (rvg)
# write an R base graphics plot to "plot.dml"
dml_pptx ("plot.dml")
x = (-300:300)/100
plot (x,sin(x),type="l",col="blue")
dev.off ()
# create a PowerPoint document
doc = read_pptx ()
doc = add_slide (doc,layout ="Title and Content",master="Office Theme")
ph_with (doc,external_img (src="plot.dml"),location=ph_location_type(type="body"))
print (doc,"example.pptx")
当我改为通过 jpeg ("plot.jpg")
生成图形文件,然后将该文件包含在演示文稿中时,它可以工作,但我想避免固定分辨率并让图表在 PowerPoint 中可编辑。
替代策略可能是 export
包,
library (export)
x = (-300:300)/100
plot (x,sin(x),type="l",col="blue")
graph2ppt (file="example2.pptx",width=6,height=6)
后者交互工作,但在脚本中失败。我错过了什么?
在此先感谢您的帮助。
HPF
您需要使用函数 dml
的参数 code
并提供包含绘图说明的表达式:
library(rvg)
library(officer)
library(magrittr)
read_pptx() %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(dml(code = {
x = (-300:300)/100
plot (x,sin(x),type="l",col="blue")
}), location = ph_location_type(type = "body")) %>%
print(target = "demo_rvg.pptx")
我正在尝试使用 officer 包来生成包含 R 基本图形的 PowerPoint 文档,最好不要使用固定分辨率,而是作为可编辑的矢量图形。这是我一直在尝试的方法,但生成的 PowerPoint 文档仍然缺少图表(中间 .dml
已创建并包含一些 XML)。
library (officer)
library (rvg)
# write an R base graphics plot to "plot.dml"
dml_pptx ("plot.dml")
x = (-300:300)/100
plot (x,sin(x),type="l",col="blue")
dev.off ()
# create a PowerPoint document
doc = read_pptx ()
doc = add_slide (doc,layout ="Title and Content",master="Office Theme")
ph_with (doc,external_img (src="plot.dml"),location=ph_location_type(type="body"))
print (doc,"example.pptx")
当我改为通过 jpeg ("plot.jpg")
生成图形文件,然后将该文件包含在演示文稿中时,它可以工作,但我想避免固定分辨率并让图表在 PowerPoint 中可编辑。
替代策略可能是 export
包,
library (export)
x = (-300:300)/100
plot (x,sin(x),type="l",col="blue")
graph2ppt (file="example2.pptx",width=6,height=6)
后者交互工作,但在脚本中失败。我错过了什么?
在此先感谢您的帮助。
HPF
您需要使用函数 dml
的参数 code
并提供包含绘图说明的表达式:
library(rvg)
library(officer)
library(magrittr)
read_pptx() %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(dml(code = {
x = (-300:300)/100
plot (x,sin(x),type="l",col="blue")
}), location = ph_location_type(type = "body")) %>%
print(target = "demo_rvg.pptx")