|
| 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. |
0 commit comments