【发布时间】:2020-11-01 00:14:03
【问题描述】:
我想在 word 文档中添加底图。
在officer包的文档中有一个使用plot_instr函数的例子:
anyplot <- plot_instr(code = {
barplot(1:5, col = 2:6)
})
doc <- read_docx()
doc <- body_add(doc, anyplot, width = 5, height = 4)
print(doc, target = tempfile(fileext = ".docx"))
我想在函数内的 word 文档中添加一个绘图,所以我需要像这样的绘图函数的变量输入:
x=1:5
cols=2:6
anyplot <- plot_instr(code = {
barplot(x,col=cols)
})
doc <- read_docx()
doc <- body_add(doc, anyplot, width = 5, height = 4)
print(doc, target = tempfile(fileext = ".docx"))
但上面的代码不起作用,我找不到任何其他使用 plot_instr 的示例。
【问题讨论】:
-
我猜“代码”在不同的环境中运行,因此无法访问定义的变量。 (虽然我不确定)所以一种可能有效的方法是将这些变量存储为 .rdata 对象并使用代码访问变量。这样您就可以在不引用之前定义的任何变量的情况下获取它们。然而,这是非常低效的,应该作为最后的手段。 (另外我实际上并没有测试它)
-
问题是关于你代码中的名字
x,body_add的第一个参数也是x...