Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-rodrigues/ed62c86060c5f1901c8299a98fe44191 to your computer and use it in GitHub Desktop.
Save b-rodrigues/ed62c86060c5f1901c8299a98fe44191 to your computer and use it in GitHub Desktop.
Put this code below in a file called example_report.Rmd
---
title: "Report for `r params$country`"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
tufte::tufte_handout:
latex_engine: xelatex
params:
country: "country"
year: "year"
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \floatplacement{figure}{H}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \usepackage{xcolor}
- \usepackage{numprint}
---
```{r}
# run this to compile document
library(dplyr)
library(knitr)
library(pwt10)
library(ggplot2)
library(modelsummary)
msummary_docx <- function(...){
modelsummary(..., output = "flextable")
}
data("pwt10.0")
dataset <- pwt10.0 %>%
filter(country %in% params$country,
year >= params$year)
make_plot <- function(dataset, var){
ggplot(dataset) +
geom_line(aes(y = !!sym(var), x = year)) +
ggtitle(paste0("Variable: ", var))
}
```
```{r, results="asis"}
variables <- c("cgdpe",
"avh",
"rtfpna")
res <- lapply(variables, function(x){
knitr::knit_child(text = c(
'## Variable: "`r x`"',
'```{r}',
'print(make_plot(dataset, x))',
'eval(parse(text = paste0("msummary_docx(lm(", x, " ~ ccon + pop, data = dataset))")))',
'```'
),
envir = environment(),
quiet = TRUE)
})
cat(unlist(res), sep = "\n")
```
Put this code below in compile_reports.R, and run it to compile the documents!
library(dplyr)
countries <- c("Austria", "Belgium", "France", "Germany", "Denmark", "Portugal")
for(i in countries){
rmarkdown::render(input = "example_report.Rmd",
output_file = paste0("report_", i, "_", Sys.Date(), ".docx"),
rmarkdown::word_document(),
params = list(country = i, year = 1990))
}
@vale-tech
Copy link

vale-tech commented Oct 22, 2022

Your sample was very helpful to me, thank you. I wanted to see how to make the code work with a child template instead of the text option of knit_child which I think is sometimes hard to read. You can get rid of the wrapper function, but have to set the option to ignore chunks with the same name. Also, no indenting in child templates! At least for me it will only produce html and not knitted markdown.

Gist - https://gist.github.com/vale-tech/679b59f92a45f668fdf84ba226fa4d43.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment