Skip to content

Instantly share code, notes, and snippets.

@vale-tech
Last active April 18, 2024 13:49
Show Gist options
  • Save vale-tech/679b59f92a45f668fdf84ba226fa4d43 to your computer and use it in GitHub Desktop.
Save vale-tech/679b59f92a45f668fdf84ba226fa4d43 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)
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("child_report.Rmd", envir = environment(), quiet = TRUE)
})
cat(unlist(res), sep = "\n")
```
Put this code below in a file called child_report.Rmd
## Variable: "`r x`"
```{r}
print(make_plot(dataset, x))
modelsummary(eval(parse(text = paste0("lm(", x, " ~ ccon + pop, data = dataset)"))), output = "flextable")
```
Put this code below in compile_reports.R, and run it to compile the documents!
library(dplyr)
options(knitr.duplicate.label = "allow") # needed if you do not want to provide unique chunk names
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment