Skip to content

Commit a40e8c3

Browse files
committed
🔨 update automating Rmd reports part 1
1 parent 207910c commit a40e8c3

10 files changed

+230
-0
lines changed
26.6 KB
Loading
70.2 KB
Loading
38.6 KB
Loading
135 KB
Loading
14.5 KB
Loading
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: "Creating professional R Markdown reports"
3+
author: "Erika Duan"
4+
date: "`r Sys.Date()`"
5+
output:
6+
github_document:
7+
toc: true
8+
---
9+
10+
```{r setup, include=FALSE}
11+
# Set up global environment ----------------------------------------------------
12+
knitr::opts_chunk$set(echo=TRUE, results='hide', fig.show='hold', fig.align='center')
13+
```
14+
15+
```{r, echo=TRUE, message=FALSE, warning=FALSE}
16+
# Load required packages -------------------------------------------------------
17+
if (!require("pacman")) install.packages("pacman")
18+
pacman::p_load(here,
19+
tidyverse,
20+
palmerpenguins)
21+
```
22+
23+
24+
# Creating html tabs in R Markdown reports
25+
26+
Creating [html tabs](https://bookdown.org/yihui/rmarkdown-cookbook/html-tabs.html) is a handy way to organise parallel sections of information i.e. displaying multiple plots from the same dataset. We can use the dataset from the `palmerpenguin` R package to explore this.
27+
28+
```{r, results='hold'}
29+
# Preview palmerpenguin dataset ------------------------------------------------
30+
palmerpenguins::penguins %>%
31+
head(3) %>%
32+
knitr::kable()
33+
```
34+
35+
36+
## Creating tabs manually
37+
38+
Tabs can be created manually by adding the code `{.tabset}` next to an empty header one level higher than the header level that you want to display as a tab. Make sure you add the code `{-}` next to a second higher level header to signal where tab creation ends.
39+
40+
```{r, echo=FALSE, results='hold', out.width="70%"}
41+
knitr::include_graphics("../../figures/dv-creating_rmd_reports-manual_tabs_code.png")
42+
```
43+
44+
The html output consists of interactive headers with titles corresponding to individual header names.
45+
46+
```{r, echo=FALSE, results='hold', out.width="100%"}
47+
knitr::include_graphics("../../figures/dv-creating_rmd_reports-manual_tabs.png")
48+
```
49+
50+
51+
## Creating tabs dynamically
52+
53+
There will be circumstances where you do not want to or cannot manually specify the number of tabs required. In these circumstances, dynamic tabs can be created through the following steps:
54+
55+
1. Create individual plots (using a function) and store them inside a list.
56+
2. Create a chunk of code in between a higher level header labelled `{.tabset}` and another one labelled `{-}`.
57+
3. Set this chunk of code with `echo=FALSE` and `results='asis'`.
58+
59+
```{r, echo=TRUE, results='hide'}
60+
# Create a list of plots -------------------------------------------------------
61+
plot_bill_length_vs_depth <- function(penguin_species) {
62+
palmerpenguins::penguins %>%
63+
filter(species == penguin_species) %>%
64+
ggplot(aes(x = bill_length_mm,
65+
y = bill_depth_mm)) +
66+
geom_point() +
67+
labs(title = paste0("Species: ", penguin_species)) +
68+
theme_bw() +
69+
theme(panel.grid = element_line(linetype = "dotted"))
70+
}
71+
72+
# Extract distinct species names and create a plot per species
73+
plots <- palmerpenguins::penguins %>%
74+
distinct(species) %>%
75+
pull(species) %>%
76+
map(~ plot_bill_length_vs_depth(.x))
77+
78+
# Set plot names
79+
species_names <- palmerpenguins::penguins %>%
80+
distinct(species) %>%
81+
pull(species)
82+
83+
plots <- set_names(plots, species_names)
84+
```
85+
86+
```{r, echo=TRUE, message=FALSE, warning=FALSE, fig.show='hide', results='hide'}
87+
# Use a for loop to print code for tab creation via R Markdown -----------------
88+
# Set echo=FALSE and results='asis' in the code chunk
89+
# Insert chunk between a header labelled `{.tabset}` and a header labelled `{-}`
90+
# Store each plot as temp object and then print each temp object and two new lines
91+
for (i in seq_along(plots)) {
92+
temp <- plots[[i]]
93+
cat("##", "Species: ", names(plots[i]), "\n") # Create tabs from plot names
94+
print(temp)
95+
cat("\n\n")
96+
}
97+
```
98+
99+
This will generate dynamically sized tabs as shown below.
100+
101+
```{r, echo=FALSE, results='hold', out.width='80%'}
102+
knitr::include_graphics("../../figures/dv-creating_rmd_reports-dynamic_tabs.png")
103+
```
104+
105+
**Note:** The option `results='asis'` allows R code to be printed as a raw output (i.e. code is not printed inside fenced code blocks). This is handy for writing for loops to automate aspects of R Markdown report generation.
106+
107+
108+
# Resources
109+
110+
+ The definitive R Markdown [resource guide](https://bookdown.org/yihui/rmarkdown-cookbook/html-tabs.html) by Yihui Xie.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
Creating professional R Markdown reports
2+
================
3+
Erika Duan
4+
2022-05-22
5+
6+
- [Creating html tabs in R Markdown
7+
reports](#creating-html-tabs-in-r-markdown-reports)
8+
- [Creating tabs manually](#creating-tabs-manually)
9+
- [Creating tabs dynamically](#creating-tabs-dynamically)
10+
- [Resources](#resources)
11+
12+
``` r
13+
# Load required packages -------------------------------------------------------
14+
if (!require("pacman")) install.packages("pacman")
15+
pacman::p_load(here,
16+
tidyverse,
17+
palmerpenguins)
18+
```
19+
20+
# Creating html tabs in R Markdown reports
21+
22+
Creating [html
23+
tabs](https://bookdown.org/yihui/rmarkdown-cookbook/html-tabs.html) is a
24+
handy way to organise parallel sections of information i.e. displaying
25+
multiple plots from the same dataset. We can use the dataset from the
26+
`palmerpenguin` R package to explore this.
27+
28+
``` r
29+
# Preview palmerpenguin dataset ------------------------------------------------
30+
palmerpenguins::penguins %>%
31+
head(3) %>%
32+
knitr::kable()
33+
```
34+
35+
| species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
36+
|:--------|:----------|---------------:|--------------:|------------------:|------------:|:-------|-----:|
37+
| Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
38+
| Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
39+
| Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
40+
41+
## Creating tabs manually
42+
43+
Tabs can be created manually by adding the code `{.tabset}` next to an
44+
empty header one level higher than the header level that you want to
45+
display as a tab. Make sure you add the code `{-}` next to a second
46+
higher level header to signal where tab creation ends.
47+
48+
<img src="../../figures/dv-creating_rmd_reports-manual_tabs_code.png" width="70%" style="display: block; margin: auto;" />
49+
50+
The html output consists of interactive headers with titles
51+
corresponding to individual header names.
52+
53+
<img src="../../figures/dv-creating_rmd_reports-manual_tabs.png" width="100%" style="display: block; margin: auto;" />
54+
55+
## Creating tabs dynamically
56+
57+
There will be circumstances where you do not want to or cannot manually
58+
specify the number of tabs required. In these circumstances, dynamic
59+
tabs can be created through the following steps:
60+
61+
1. Create individual plots (using a function) and store them inside a
62+
list.
63+
2. Create a chunk of code in between a higher level header labelled
64+
`{.tabset}` and another one labelled `{-}`.
65+
3. Set this chunk of code with `echo=FALSE` and `results='asis'`.
66+
67+
``` r
68+
# Create a list of plots -------------------------------------------------------
69+
plot_bill_length_vs_depth <- function(penguin_species) {
70+
palmerpenguins::penguins %>%
71+
filter(species == penguin_species) %>%
72+
ggplot(aes(x = bill_length_mm,
73+
y = bill_depth_mm)) +
74+
geom_point() +
75+
labs(title = paste0("Species: ", penguin_species)) +
76+
theme_bw() +
77+
theme(panel.grid = element_line(linetype = "dotted"))
78+
}
79+
80+
# Extract distinct species names and create a plot per species
81+
plots <- palmerpenguins::penguins %>%
82+
distinct(species) %>%
83+
pull(species) %>%
84+
map(~ plot_bill_length_vs_depth(.x))
85+
86+
# Set plot names
87+
species_names <- palmerpenguins::penguins %>%
88+
distinct(species) %>%
89+
pull(species)
90+
91+
plots <- set_names(plots, species_names)
92+
```
93+
94+
``` r
95+
# Use a for loop to print code for tab creation via R Markdown -----------------
96+
# Set echo=FALSE and results='asis' in the code chunk
97+
# Insert chunk between a header labelled `{.tabset}` and a header labelled `{-}`
98+
# Store each plot as temp object and then print each temp object and two new lines
99+
for (i in seq_along(plots)) {
100+
temp <- plots[[i]]
101+
cat("##", "Species: ", names(plots[i]), "\n") # Create tabs from plot names
102+
print(temp)
103+
cat("\n\n")
104+
}
105+
```
106+
107+
This will generate dynamically sized tabs as shown below.
108+
109+
<img src="../../figures/dv-creating_rmd_reports-dynamic_tabs.png" width="80%" style="display: block; margin: auto;" />
110+
111+
**Note:** The option `results='asis'` allows R code to be printed as a
112+
raw output (i.e. code is not printed inside fenced code blocks). This is
113+
handy for writing for loops to automate aspects of R Markdown report
114+
generation.
115+
116+
# Resources
117+
118+
- The definitive R Markdown [resource
119+
guide](https://bookdown.org/yihui/rmarkdown-cookbook/html-tabs.html)
120+
by Yihui Xie.
5.53 KB
Loading
5.54 KB
Loading
5.39 KB
Loading

0 commit comments

Comments
 (0)