r - 如何向文档添加没有固定值的 basemap

标签 r officer

我想在 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/

相关文章:

r - 在 ggplot2 的分面点图中绘制顺序

r - 来自多个数据框列的值到一个向量中

r - 使用 R 的军官包将图标添加到依赖于列值的 powerpoint 项目符号

r - 在 R 警官中,如何为 PowerPoint 设置文本和项目符号级别的格式

r - 在 R 中将 pptx 保存为 pdf

r - 通过官员向PowerPoint幻灯片添加多个文本符号

rm() 除特定对象外的所有内容

r - 在 R 的 for 循环中使用粘贴函数编写 ifelse 语句

r - gsub 在 for 循环中失败

R 和 Microsoft Word : Updating text in one Word document based on text in another Word document