Skip to content

Instantly share code, notes, and snippets.

@charliejhadley
Created May 19, 2022 15:33
Show Gist options
  • Save charliejhadley/355cc8dde095a9681d64407dd2b63ea6 to your computer and use it in GitHub Desktop.
Save charliejhadley/355cc8dde095a9681d64407dd2b63ea6 to your computer and use it in GitHub Desktop.
Why can't knit_child() have parameters?!
---
title: "Report on animals"
output: html_document
date: '2022-05-19'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE
)
library(tidyverse)
```
# Introduction
This report is designed to show how to include programmatically insert multiple sections that are generated from grouping variables in a dataset.
# Summarising by diet
Let's summarise each of the diet types in turn.
```{r results='asis', warning=FALSE, message=FALSE}
msleep %>%
pull(vore) %>%
unique() %>%
map(~knitr::knit_expand("vore-section.Rmd",
vore_type = .x)) %>%
unlist() %>%
knitr::knit_child(text = ., quiet = TRUE) %>%
unlist() %>%
cat(., sep = '\n')
```
# Summarising by conservation status
I haven't written this yet.
```{r}
vore_type_chr <- as.character(glue::glue("{{vore_type}}"))
```
## Animals in the `r vore_type_chr` category
### Conservation status
In this section we look into the conservation of animals with a `r vore_type_chr` diet.
```{r}
vore_conservation <- function(vore_type_chr) {
msleep %>%
filter(vore == vore_type_chr) %>%
count(conservation, sort = TRUE) %>%
knitr::kable()
}
```
```{r}
vore_conservation(vore_type_chr)
```
### Sleep characteristics
Sleep characteristics
```{r}
msleep %>%
filter(vore == "carni") %>%
ggplot(aes(x = sleep_total,
y = sleep_rem)) +
geom_point()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment