Using code below, I'm able to generate a ppt report for subset of mtcars
dataset:
library(ggplot2)
library(tidyverse)
library(patchwork)
library(officer)
library(officedown)
library(glue)
small <- mtcars %>%
filter(carb %in% c(1, 2))
p1 <- ggplot(mpg, wt, data = small, colour = cyl)
p2 <- ggplot(mpg, data = small) + ggtitle("small")
p <- p1 | p2
template_pptx <- read_pptx()
report <- add_slide(template_pptx, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value=p, location = ph_location_label(ph_label = "Content Placeholder 2"))
print(report, target=glue('report for small car.pptx'))
Now let's say we need to reproduce the report generating process for the following datasets as well:
middle <- mtcars %>%
filter(carb %in% c(3, 4))
large <- mtcars %>%
filter(carb %in% c(6, 8))
My ideas is to convert multiple ggplots part to a function and save in a script plot.R
, then I will write pseudocode script named main.R
to run the whole process and generate 3 reports for small, middle, large datasets respectively:
# main.R
for i in c(small, middle, large){
source('plot.R')
# maybe need to import and run plot function() from plot.R
# save figure to ppt
template_pptx <- read_pptx("./ppt_template.pptx")
report <- add_slide(template_pptx, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value=p, location = ph_location_label(ph_label = "Content Placeholder 2"))
print(report, target=glue('report for {i} car.pptx'))
}
The problem I met is I don't know how to convert plotting code to a function and pass params (maybe save a config.yaml
file in case we have many params?) to the predefined function, and finally generate a parameterized report?
Many thanks for your comments and help at advance.
References:
R: multiple ggplot2 plot using d*ply
https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html
R - How to generate parameterized reports via nested tibbles + pwalk(rmarkdown::render)