Created
January 14, 2022 20:53
-
-
Save b-rodrigues/9b29fea2f238c81146a36736d9e97c90 to your computer and use it in GitHub Desktop.
the power of DRY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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