Skip to content

Instantly share code, notes, and snippets.

@turgeonmaxime
Created October 18, 2018 18:03
Show Gist options
  • Save turgeonmaxime/7ab6e22b3a4191e31def2e42b7e69130 to your computer and use it in GitHub Desktop.
Save turgeonmaxime/7ab6e22b3a4191e31def2e42b7e69130 to your computer and use it in GitHub Desktop.
# Loop through list of departments
list_depts <- c("Adult Medicine",
"Heart Health",
"Maternal Services")
for (dept in list_depts) {
# Add underscores
dept_f <- stringr::str_replace_all(dept, " ", "_")
# Change the name of output
output <- "report_template.Rmd" %>%
str_replace("template", dept_f) %>%
str_replace("Rmd", "pdf")
# Render custom report
rmarkdown::render("report_template.Rmd",
params = list(
department = dept
),
output_file = output)
}
---
title: "`r paste0('Survey Results--', params$org)`"
author:
name: "Maxime Turgeon"
date: "`r format(Sys.Date(), format = '%B %e, %Y')`"
output: html_document
params:
department: Adult Medicine
---
```{r setup, include=FALSE, message = FALSE}
knitr::opts_chunk$set(echo = FALSE)
# list required pacakges
library(tidyverse)
library(lubridate)
library(glue)
```
```{r data}
# Generate fake data
date <- ymd("2018-01-01") + months(0:11)
metric <- rnorm(length(date), mean = 7.5, sd = 2)
```
```{r plot}
# Plot results
tibble(date, metric) %>%
ggplot(aes(x = date, y = metric)) +
geom_line() +
geom_point() +
expand_limits(y = 0) +
theme_minimal() +
ggtitle(glue("Metric results for {params$department}"))
```
@turgeonmaxime
Copy link
Author

To run the code above, you need to have both files in the same directory (or update the input/output paths). Then simply source the R script and voila!

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