简体   繁体   中英

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

Despite my best efforts, I cannot get any content loaded side by side in Powerpoint slides generated using the R package officer . What I would like is content (sourced images or plots, etc. on the right and text on the left). Here is my workflow:

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") 

The result of the above code is that both pieces of content appear on the right flank. I can get text on the left side in the form of a title w/ ph_with_text(type = "title", index=1, str = "title") but despite my best efforts no captions or content appears on the left hand side of the slide.

You can explicitly specify that your first call to ph_with doesn't appear on the right.

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") 

There are two solutions:

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") 

The parameter index is not part of the function and is ignored. I think you want to use id but its values should be 1 and 2 - documentation of this parameter is "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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM