我想在 word 文档中添加一个 basemap 。
在官员包的文档中有一个使用 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 用法示例。
最佳答案
我想我找到了解决方案!
当我在 barplot(...) 调用之前设置断点时,当使用 plot_instr 包装函数调用 body_add 时,我可以看到代码。该代码创建一个临时 png 文件。我复制了这段代码并对其进行了修改:
x=1:5
cols=2:6
doc <- read_docx()
file <- tempfile(fileext = ".png")
options(bitmapType = "cairo")
png(filename = file, width = 5, height = 5, units = "in", res = 300)
tryCatch({
barplot(x,col=cols)
}, finally = {
dev.off()
})
on.exit(unlink(file))
value <- external_img(src = file, width = 5, height = 5)
body_add(doc, value)
print(doc, target = tempfile(fileext = ".docx"))
关于r - 如何向文档添加没有固定值的 basemap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64570405/