r - 为什么我不能将网格中的一组图发送到 PowerPoint with officer?

r - Why can't I send a group of plots in a grid to PowerPoint with officer?

我已经使用 gridextra 创建了一个 qicharts2 图网格,但是当我尝试使用 officer 将它发送到 PowerPoint 时,我收到了这个错误..

错误 doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, : StartTag:无效的元素名称 [68]

这是我的代码:

library(qicharts2)
library(gridExtra)
library(officer)
library(rvg)



#24 random numbers from a normal distribution for example.
y1 <- rnorm(24)
y2 <- rnorm(24)

yC1 <- qic(y1)
yC2 <- qic(y2)

grid <- grid.arrange(yC1,yC2)


filename <- "C:\Desktop\MyCharts.pptx"


read_pptx(filename) %>% 

  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_vg(code = print(grid), type = "body") %>% 
  print(target = filename) %>% 
  invisible()

非常感谢大家就如何改进我的问题提出的建议。

任何帮助进一步帮助大大收到

当使用 grid.arrange 时,您正在 绘图 ,打印方法在这里没有用(print(grid) 对图形设备没有影响)。以下是使用 grid.arrange:

library(qicharts2)
library(gridExtra)
library(officer)
library(rvg)
library(magrittr)

#24 random numbers from a normal distribution for example.
y1 <- rnorm(24)
y2 <- rnorm(24)

yC1 <- qic(y1)
yC2 <- qic(y2)

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_vg(code = grid.arrange(yC1,yC2), type = "body") %>% 
  print(target = "filename.pptx") %>% 
  browseURL()

Edit for rvg >= 0.2.4 :您必须使用 dml 功能 :

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = dml(grid.arrange(yC1,yC2)), location = ph_location_type(type = "body")) %>% 
  print(target = "filename.pptx") %>% 
  browseURL()

添加绘图而不是打印对我有用。 grid.arrange 中存在一些错误,但如果它通过 plot() 它会起作用。这个特定的例子与之前的答案一样工作,但更复杂的是多个 grobs 和文本框。

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = dml(code=plot(grid.arrange(yC1,yC2))),
          location = ph_location_type(type = "body")) %>% 
  print(target = "filename.pptx") %>% 
  browseURL()