#' Quick Markdown Report
#'
#' Create a markdown report from a list of instructions.
#'
#' @param ... instructions, character will be converted to text and headers,
#' formulas will be converted to chunks, optional lhs of formula is a list of
#' chunk options.
#' @param output_file path of output, optional
#' @param header YAML header, given as a list fed to `yaml::as.yaml`
#' @param open if `TRUE` (default), report will be opened after creation
#'
#' @return the path of the output file
#' @export
#'
#' @examples
#' library(tidyr)
#' x <- iris %>% nest(-Species)
#'
#' # create report on the fly
#' quickmd(
#' "# Plot of {x$Species[1]}",
#' "{x$Species[1]} has a mean petal length of {mean(x$data[[1]]$Petal.Length)}",
#' ~ hist(x$data[[1]]$Sepal.Width),
#' .(fig.height=3) ~ hist(x$data[[1]]$Sepal.Length))
#'
#' # create report from a list of instructions
#' dots <- list("# Plot of {x$Species[1]}",
#' "{x$Species[1]} has a mean petal length of {mean(x$data[[1]]$Petal.Length)}",
#' ~hist(x$data[[1]]$Sepal.Width),
#' list(fig.height=3) ~ hist(x$data[[1]]$Sepal.Length))
#'
#' quickmd(!!!dots)
quickmd <- function(
...,
output_file = NULL,
header = list(
title = "mmmd report",
author = Sys.getenv("USERNAME"),
date = "`r Sys.Date()`",
output = list(html_document = list(code_folding = "hide"))),
open = TRUE){
if (!is.data.frame(x)) stop("x must be a data.frame")
dots <- tibble::lst(...)
body <- purrr::map_chr(dots,~purrr::when(
.,
is.character(.) ~ glue::glue(.),
inherits(.,"formula") ~ formula_to_chunk(.))) %>%
paste(collapse = "\n\n")
chr <- glue::glue("---\n{as.yaml(header)}---\n\n{body}")
temp_rmd <- tempfile(fileext = ".Rmd")
write(chr,temp_rmd)
output_file <- rmarkdown::render(temp_rmd, output_file = output_file)
if (open) browseURL(output_file)
output_file
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.