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)
You could put your plotting code in a function which eg takes two arguments, a dataframe (x) and a title.
Similarly put the code to prepare the pptx inside a function, which eg takes two arguments, a dataframe (x) and a title or filename or...
In my code below I have put your three datasets in a list and then make use of purrr::iwalk
to loop over this list to make a pptx report for each dataset. Using purrr::iwalk
the name of the dataset is passed as the second argument to the reporting function.
library(ggplot2)
library(patchwork)
library(dplyr)
library(purrr)
library(officer)
library(glue)
plot_fun <- function(x, title) {
p1 <- ggplot(data = x, aes(mpg, wt, colour = cyl))
p2 <- ggplot(data = x, aes(mpg)) + ggtitle(title)
p1 | p2
}
pptx_fun <- function(x, y) {
p <- plot_fun(x, title = y)
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 {y} car.pptx'))
}
data_list <- lapply(list(small = 1:2, medium = 3:4, large = 5:6), function(x) filter(mtcars, carb %in% x))
purrr::iwalk(data_list, pptx_fun)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.