Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Last active March 2, 2019 17:17
Show Gist options
  • Save PaulC91/686005c60bec9d1020ea4f1957d8312b to your computer and use it in GitHub Desktop.
Save PaulC91/686005c60bec9d1020ea4f1957d8312b to your computer and use it in GitHub Desktop.
R code to generate multiple powerpoint decks with native ppt charts from a single dataset using officer and purrr
library(tidyverse)
library(officer)
library(mschart)
# function to make ppt deck for each class in mpg data
new_deck <- function (i) {
data <- mpg %>%
filter(class == i)
ppt_chart <- ms_scatterchart(data, x = "displ", y = "hwy", group = "cyl") %>%
chart_settings(scatterstyle = "marker") %>%
chart_labels(title = "")
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") %>%
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)
# make a slide deck for each group and store in list
decks <- map(data_groups, new_deck)
# export each ppt with descriptive name using purrr::walk
walk(1:length(decks),
~ decks[[.x]] %>%
print(paste0("mpg_", data_groups[.x], ".pptx")) %>%
invisible()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment