Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Last active October 2, 2019 23:18
Show Gist options
  • Save PaulC91/635e9b093b5a9270ca88289f3eed2799 to your computer and use it in GitHub Desktop.
Save PaulC91/635e9b093b5a9270ca88289f3eed2799 to your computer and use it in GitHub Desktop.
Example of programming powerpoint slides using purrr::walk and the officer package
library(tidyverse)
library(officer)
library(mschart)
# create new ppt with title slide
mypres <- read_pptx() %>%
add_slide(layout="Title Slide", master="Office Theme") %>%
ph_with_text(type = "ctrTitle", str = "Make slide decks with purrr & officer") %>%
ph_with_text(type = "subTitle", str = "example using mtcars data")
# function to add new slide for each class in mtcars data
new_slide <- function (i) {
data <- mpg %>%
filter(class == i)
ppt_chart <- ms_scatterchart(data, x = "displ", y = "hwy") %>%
chart_settings(scatterstyle = "marker") %>%
chart_labels(title = "")
mypres %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_text(type = "title", str = paste("Class:", i)) %>%
ph_with_chart(chart = ppt_chart, type = "body") %>%
ph_with_text(type = "ftr", str = "A footer") %>%
ph_with_text(type = "dt", str = format(Sys.Date()))
}
# get unique list of classes in data
data_groups <- unique(mpg$class)
# add new slides with purrr::walk
walk(data_groups, new_slide)
# save presentation
mypres %>%
print(target = "mtcars_pres.pptx") %>%
invisible()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment