用于创建包含并排内容的幻灯片的 Officer r 包 - 内容仅出现在一个部分中

Officer r package to create slides with side by side content -content only appearing in one section

尽管我尽了最大努力,但我无法在使用 R 程序包 officer 生成的 Powerpoint 幻灯片中并排加载任何内容。我想要的是内容(右边是来源图像或图表等,左边是文本)。这是我的工作流程:

plot.gg <- ggplot(iris, aes(Sepal.Length, Petal.Width)) + 
   geom_line()

library(officer)
read_pptx()  %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  ph_with("text",  location = ph_location_type(type = "body", index = 4)) %>%
  ph_with(plot.gg, location = ph_location_type(type = "body", index = 3)) %>%
  print(target = "test.pptx") 

以上代码的结果是两条内容都出现在右侧。我可以在左侧以带 ph_with_text(type = "title", index=1, str = "title") 的标题形式获取文本,但尽管我尽了最大努力,但幻灯片的左侧没有出现标题或内容。

您可以明确指定您对 ph_with 的第一次调用不会出现在右侧。

read_pptx()  %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  ph_with("text",  location = ph_location_type(type = "body", index = 4, position_right = F)) %>%
  ph_with(plot.gg, location = ph_location_type(type = "body", index = 3)) %>%
  print(target = "test.pptx") 

有两种解决方案:

library(officer)
library(magrittr)
library(ggplot2)
plot.gg <- ggplot(iris, aes(Sepal.Length, Petal.Width)) + 
  geom_line()

# solution 1 : you don't have layout named "Two Content" ---
read_pptx()  %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  ph_with("text",  location = ph_location_type(type = "body", position_right = FALSE)) %>%
  ph_with(plot.gg, location = ph_location_type(type = "body")) %>%
  print(target = "test.pptx") 

# solution 1 : you can rely on a layout named "Two Content" ---
read_pptx()  %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  ph_with("text",  location = ph_location_left()) %>%
  ph_with(plot.gg, location = ph_location_right()) %>%
  print(target = "test.pptx") 

参数index不是函数的一部分,被忽略。我想你想使用 id 但它的值应该是 1 和 2 - 这个参数的文档是 "index of the placeholder. If two body placeholder, there can be two different index: 1 and 2 for the first and second body placeholders defined in the layout. If this argument is used, position_right and position_top will be ignored."