Officer包用于创建内容并排的幻灯片-内容仅显示在一个部分中



尽管我尽了最大努力,但我无法在使用Rofficer生成的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") 

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

有两种解决方案:

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-这个参数的文档是"占位符的索引。如果是两个正文占位符,可以有两个不同的索引:1和2用于布局中定义的第一个和第二个正文占位符。如果使用这个参数,position_right和position_top将被忽略。">

您可以明确指定对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") 

相关内容

  • 没有找到相关文章

最新更新