2

So I am working on creating an automated powerpoint report in R using the officer package. I want to create a bulleted list with numerous levels but I need to be able to have specific text formatting for each line (i.e. I want the first two words to be green while the rest of the line is black). I tried using unordered_list() but this command doesn't allow the specific text formatting I need. I've tried the code below which colors/bolds the text that I want, but ph_add_par(level = ) does not indent the bullet points.

Is there a way that I can format the text how I want while also being able to manipulate the bullet point levels? I want it to look something like this:

enter image description here

library(officer)
    report <- read_pptx()
    sprint_1 <- sprintf("%s total crimes in %s %s", 
                        tc20, params$month, params$year)

    report%>%
        add_slide(layout = 'Title and Content', master = 'Office Theme')%>%
      ph_with(value = "Academy: Summary Notes", location = ph_location_type(type = "title"))%>%
        ph_empty(location = ph_location_type("body")) %>%
        ph_add_par(level = 1L)%>%
        ph_add_text(str = sprint_1, style = fp_text(color = "black", font.size = 28, bold = TRUE, underlined = TRUE))%>%
        ph_add_par(level = 2L)%>%
        ph_add_text(str = paste("compared to this time in ", params$pastyear, " (", tc_ytd19, " total crimes)", sep = ""), 
                    style = fp_text(color = 'black', font.size = 24)) %>% 
        ph_add_text(str = paste(pct_chg_mth, "change", sep = " "),
                    style = fp_text(color = "green", font.size = 24)) -> report
David Gohel
  • 9,180
  • 2
  • 16
  • 34

1 Answers1

2

I am not able to run your code but the following code should help. Use block_list and in ph_with call set level_list=c(1L, 2L, 1L)

library(officer)

fpt_blue_bold <- fp_text(color = "#006699", bold = TRUE)
fpt_red_italic <- fp_text(color = "#C32900", italic = TRUE)

value <- block_list(
  fpar(ftext("hello world", fpt_blue_bold)),
  fpar(ftext("hello", fpt_blue_bold), " ",
       ftext("world", fpt_red_italic)),
  fpar(
    ftext("blah blah blah", fpt_red_italic)))
value

doc <- read_pptx()
doc <- add_slide(doc)
doc <- ph_with(doc, value, location = ph_location_type(type = "body"), 
               level_list = c(1L, 2L, 1L))
print(doc, target = "test.pptx")

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Hey David! Thanks for the awesome package and for your response. But is there any way to indent the second and third bullet points while still keeping the text formatting? – Logowilliams May 12 '20 at 16:08
  • no sorry, I'll add an option (I'd like to remove/deprecate ph_add_* functions) – David Gohel May 12 '20 at 16:28
  • I have updated my answer - to use that feature, you will need to update package from github until it goes on cran – David Gohel May 13 '20 at 09:40