Skip to content

Instantly share code, notes, and snippets.

@ChrisBeeley
Last active April 21, 2021 09:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBeeley/1a84301a9329aeb7dd9a9cf2ff95f62c to your computer and use it in GitHub Desktop.
Save ChrisBeeley/1a84301a9329aeb7dd9a9cf2ff95f62c to your computer and use it in GitHub Desktop.
---
title: "Loop demo"
author: "Chris Beeley"
date: "21/04/2021"
output: html_document
params:
species: NA
---
```{r setup, include=FALSE}
library(palmerpenguins)
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE)
```
## `r params$species`
```{r}
penguins %>%
filter(species == params$species) %>%
ggplot(aes(x = bill_length_mm)) + geom_density()
```
---
title: "Loop variables"
author: "Chris Beeley"
date: "21/04/2021"
output: html_document
params:
variable: NA
---
```{r setup, include=FALSE}
library(palmerpenguins)
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE)
```
## `r params$variable`
```{r}
penguins %>%
ggplot(aes(x = .data[[params$variable]])) + geom_density()
```
# install.packages("palmerpenguins")
library(rmarkdown)
library(palmerpenguins)
# loop rows
for(i in unique(penguins$species)){
render("loop_penguins.Rmd", params = list(species = i),
output_file = paste0("report_", i))
}
# loop columns
for(i in c("bill_length_mm", "bill_depth_mm")){
render("loop_variables.Rmd", params = list(variable = i),
output_file = paste0("variable_report_", i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment