Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
the power of DRY
---
title: "The power of DRY"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
#rmarkdown::render("iwalk_script.Rmd")
library(dplyr)
library(forcats)
library(tidyr)
library(purrr)
data(gss_cat)
tables_list <- gss_cat %>%
group_by(year) %>%
nest() %>%
mutate(tables = map(data, ~count(., race, marital))) %>%
pull(tables)
years_list <- count(gss_cat, year) %>%
pull(year)
tables_list <- set_names(tables_list, years_list)
```
```{r}
create_table <- function(my_table, year){
knitr::kable(my_table,
caption = paste0("hello from create_table(): ", year))
}
```
```{r}
return_section <- function(my_table, year){
a <- knitr::knit_expand(text = c("## Year {{year_in_table}}", create_table(my_table, year)),
year_in_table = year)
cat(a, sep = "\n")
}
```
```{r, results = "asis"}
iwalk(tables_list, return_section)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment