Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@b-rodrigues
Created January 14, 2022 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-rodrigues/9b29fea2f238c81146a36736d9e97c90 to your computer and use it in GitHub Desktop.
Save b-rodrigues/9b29fea2f238c81146a36736d9e97c90 to your computer and use it in GitHub Desktop.
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