Reputation: 4537
Using the officer
package in R, when working with a PowerPoint you can add text with the function ph_with_text
. However, it's not clear how to add multiple bullets of text or how to set the level of indentation. I would like to achieve the following structure:
I have tried two methods which both produce very wrong results. I have tried taking my text and adding \n
and \n\t
to create linebreaks and tabs (like how I would create the structure in PowerPoint.
doc = read_pptx()
doc = add_slide(layout = "Title and Content", master = "Office Theme")
doc = ph_with_text(doc,type = "body",
str = "Question 1\n\tAnswer 1\n\tAnswer 2\nQuestion 2\n\tAnswer 1\n\tAnswer 2",
index = 1)
This creates the bullets, but not the depth. There's a whitespace tab after each bullet before each answer. Additionally, these are not new bullets, if I manually edit the file and press tab on one bullet point, every point after is also shifted. Clearly the correct structure hasn't been acheived.
I have also tried just calling ph_with_text
repeatedly.
doc = add_slide(layout = "Title and Content", master = "Office Theme")
doc = ph_with_text(doc,type = "body", str = "Question 1", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 1", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 2", index = 1)
doc = ph_with_text(doc,type = "body", str = "Question 2", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 1", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 2", index = 1)
But this ends up overlaying the text on the same line and it's an unreadable mess.
How do I, via officer
, add text to a slide achieving multiple bullets and indented sub-elements?
Upvotes: 3
Views: 1577
Reputation: 10650
The function ph_with_ul
is the function you need
library(magrittr)
pptx <- read_pptx()
pptx <- add_slide(x = pptx, layout = "Title and Content", master = "Office Theme")
pptx <- ph_with_text(x = pptx, type = "title", str = "Example title")
pptx <- ph_with_ul(
x = pptx, type = "body", index = 1,
str_list = c("Level1", "Level2", "Level2", "Level3", "Level3", "Level1"),
level_list = c(1, 2, 2, 3, 3, 1),
style = fp_text(color = "red", font.size = 0) )
print(pptx, target = "example2.pptx")
Upvotes: 6