Created
April 27, 2020 15:48
-
-
Save martinctc/35a56f564d4f3e6aeba3858bdf6433f6 to your computer and use it in GitHub Desktop.
[Minimal Function to generate HTML report using RMarkdown] #R
This file contains 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
## This is a minimal example to show how you can create a | |
## function that generates HTML reports using RMarkdown. | |
## | |
## This uses a list-pmap workflow so you can input lists | |
## as arguments and dynamically produce a RMarkdown that | |
## is only limited by the length of your lists. | |
## | |
## The example below outputs a table and two plots from | |
## `iris`. | |
#### Load packages #### | |
library(tidyverse) | |
#### Generate list of outputs #### | |
## 1. A summary table | |
summary_table <- | |
iris %>% | |
group_by(Species) %>% | |
summarise_all(~mean(.)) | |
## 2. Plot of Sepal variables | |
sepal_plot <- | |
iris %>% | |
ggplot(aes(x = Sepal.Length, | |
y = Sepal.Width, | |
colour = Species)) + | |
geom_point() | |
## 3. Plot of Petal variables | |
petal_plot <- | |
iris %>% | |
ggplot(aes(x = Petal.Length, | |
y = Petal.Width, | |
colour = Species)) + | |
geom_point() | |
## Combine all outputs into a list of 3 objects | |
output_list <- | |
list(summary_table, | |
sepal_plot, | |
petal_plot) | |
#### Function to generate chunk strings #### | |
## This is only used as a supporting function | |
## Not used directly | |
#' | |
#' @param level Numeric value to specify the header level of the chunk. | |
#' @param title Character string to specify the title of the chunk. | |
#' @param echo Logical value to specify whether to display code. | |
#' @param object Character string to specify name of the object to show. | |
generate_chunks <- function(level = 3, title, echo = FALSE, object){ | |
level_hash <- paste(rep('#', level), collapse = "") | |
obj <- c(paste(level_hash, title), | |
paste0('```{r, echo=', echo, '}'), | |
object, | |
'```', | |
' ') | |
return(obj) | |
} | |
#### `minimal_html2()`` - with list-pmap workflow #### | |
## | |
## This the main function for creating a HTML document. | |
## | |
## NOTE THAT `outputs`, `titles`, `echos`, and `levels` must have the same length | |
#' @param title Character string to specify the title of the chunk. | |
#' @param filename File name to be used in the exported HTML. | |
#' @param outputs A list of outputs to be added to the HTML report. | |
#' @param titles A list/vector of character strings to specify the title of the chunks. | |
#' @param echos A list/vector of logical values to specify whether to display code. | |
#' @param levels A list/vector of numeric value to specify the header level of the chunk. | |
minimal_html2 <- function(title = "My minimal HTML generator", | |
filename = "minimal_html", | |
outputs = output_list, | |
titles, | |
echos, | |
levels){ | |
## Title of document | |
title_chr <- paste0('title: \"', title, '\"') | |
## chunk loopage | |
## merged to create `chunk_merged` | |
chunk_merged <- | |
list(output = outputs, | |
title = titles, | |
echo = echos, | |
level = levels, | |
id = seq(1, length(outputs))) %>% | |
purrr::pmap(function(output, title, echo, level, id){ | |
generate_chunks(level = level, | |
title = title, | |
echo = echo, | |
object = paste0("outputs[[", id, "]]")) | |
}) %>% | |
reduce(c) | |
## markdown object | |
markobj <- c('---', | |
title_chr <- paste0('title: \"', title, '\"'), | |
'output: ', | |
' html_document:', | |
' theme: united', | |
' toc: true', | |
' toc_float:', | |
' collapsed: false', | |
' smooth_scroll: true', | |
'---', | |
'', | |
'', | |
chunk_merged) | |
writeLines(markobj, paste0(filename, ".Rmd")) | |
rmarkdown::render(paste0(filename, ".Rmd")) | |
## Load in browser | |
utils::browseURL(paste0(filename, ".html")) | |
## Deletes specified files | |
unlink(c(paste0(filename, ".Rmd"), | |
paste0(filename, ".md"))) | |
} | |
#### Run example #### | |
minimal_html2(title = "My minimal HTML generator", | |
filename = "minimal_html", | |
outputs = output_list, | |
titles = c("Summary Table", "Sepal Plot", "Petal Plot"), | |
echos = rep(FALSE, 3), | |
levels = rep(3, 3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment