I am trying to create automated fitness testing reports by training hub using RMarkdown. I am building off a previous question I asked on the topic here using the same dataframe structure and similar code.
I would like to slightly customize each RMarkdown report based on hub while still allowing it to be an automated process (i.e. 1 script + 1 .rmd). For example, all hubs will have the Broad Jump plot but for A I want a broad jump plot and a 10m sprint plot.
I have tried using params in the YAML and render function but not had any luck so far.
Sample Data
Date Athlete Test Average Hub
1 2019-06-03 Athlete1 Broad_Jump 175.000000 A
2 2019-06-10 Athlete1 Broad_Jump 187.000000 A
3 2019-06-10 Athlete2 Broad_Jump 200.666667 B
4 2019-06-10 Athlete3 10m_Sprint 1.831333 B
5 2019-06-10 Athlete2 10m_Sprint 2.026667 B
6 2019-06-17 Athlete1 Broad_Jump 191.500000 A
7 2019-06-17 Athlete2 Broad_Jump 200.666667 B
8 2019-06-17 Athlete3 10m_Sprint 1.803667 B
9 2019-06-17 Athlete2 10m_Sprint 2.090000 B
10 2019-06-24 Athlete1 Broad_Jump 192.000000 A
R Script
library(rmarkdown)
library(knitr)
library(dplyr)
library(ggplot2)
WT <- read.csv("WT.csv")
WT_10m <- WT %>%
filter(Test == "10m_Sprint") %>%
select(Date, Athlete, Hub, Average)
plot2 <- ggplot(WT_10m, aes(x=Date, y=Average))+
geom_point()
for (hub in unique(WT$Hub)){
subgroup <- subset(WT, Hub == hub)
render(
input = "Hub_Test.rmd",
params = list("plot2"=plot2),
output_file = paste0('report.', hub, '.pdf'))
.Rmd File
---
title: "WT Monitoring: Hub"
output: pdf_document
params:
plot2: plot2
hub:
label: "hub"
value: A
choices: [A, B]
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(rmarkdown)
library(knitr)
library(dplyr)
library(ggplot2)
WT <- read.csv("WT.csv")
subgroup <- subset(WT, Hub == hub)
subgroup_Broad <- subgroup %>%
filter(Test == "Broad_Jump") %>%
select(Date, Athlete, Hub, Average)
ggplot(subgroup_Broad, aes(x=Date, y= Average)) +
geom_point()
params$plot2
I'm not sure whether to use params, the render file, or some other method to accomplish this task. There are several hubs and tests so I'm trying to avoid having a separate Rmarkdown template for each hub.