Skip to content

Instantly share code, notes, and snippets.

@tallguyjenks
Created September 9, 2020 14:53
Show Gist options
  • Save tallguyjenks/05817ad2ba27dd1ef46cd5b0e4e55f0a to your computer and use it in GitHub Desktop.
Save tallguyjenks/05817ad2ba27dd1ef46cd5b0e4e55f0a to your computer and use it in GitHub Desktop.
Example of parameterized RMarkdown as seen in this video: https://youtu.be/oFKb8WYDLB0
---
title: "Parameterized RMarkdown"
author: "Bryan Jenks"
date: "8/28/2020"
output: html_document
params:
# data: diamonds # Options: mpg or diamonds
# toggle: FALSE # Options: TRUE or FALSE
# year: 2018
#=======================#
# Render Function: #
#=======================#
year:
label: "Year"
value: 2017
input: slider
min: 2010
max: 2020
step: 1
sep: ""
data:
label: "Data Set:"
value: mpg
input: select
choices: [mpg, diamonds]
file:
label: "Input dataset:"
value: results.csv
input: file
toggle: True
---
```{r}
# Only overrides if different from front matter otherwise frontmatter is used.
# Needs to be ran from CLI
# rmarkdown::render("param.Rmd", params = list(
# data = "mpg",
# year = 2019,
# toggle = TRUE
# ))
# Ask for params in shiny interface also CLI
# rmarkdown::render("param.Rmd", params = "ask") # alternatively:
# Knit with parameters button in RStudio VERY useful GUI menu when combined with the above YAML
```
```{r echo=FALSE, warning=FALSE}
library(ggplot2)
library(dplyr)
```
# The `r params$data` Data set was chosen in `r params$year`
In the **`r params$data`** data set we can review a lot of important data. Lets make a visualization!
```{r viz, include=params$toggle}
mpg %>%
ggplot() +
aes(x = manufacturer) +
geom_bar(fill = "#0c4c8a") +
theme_minimal()
```
```{r viz2, include=!params$toggle}
diamonds %>%
ggplot() +
aes(x = cut, y = color, fill = clarity) +
geom_tile(size = 1L) +
scale_fill_hue() +
theme_minimal()
```
# Ideas
- Homework & Answer documents with conditional code chunk inclusion
- Data frame of student scores and using params and mappy some map functions from the purrr package look through and generate code to send emails with results to students with blastula package
- Different reports for particular regions/conditions of your data
# Useful Info
[Bookdown Reference](https://bookdown.org/yihui/rmarkdown/parameterized-reports.html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment