#' Convert to an HTML notebook #' #' Format for converting from R Markdown to an HTML notebook. #' #' See the \href{https://rmarkdown.rstudio.com/r_notebook_format.html}{online #' documentation} for additional details on using the \code{html_notebook} #' format. #' @inheritParams html_document #' @param output_source Define an output source for \R chunks (ie, #' outputs to use instead of those produced by evaluating the #' underlying \R code). See \code{\link{html_notebook_output}} for #' more details. #' @param self_contained Produce a standalone HTML file with no external #' dependencies. Defaults to \code{TRUE}. In notebooks, setting this to #' \code{FALSE} is not recommended, since the setting does not apply to #' embedded notebook output such as plots and HTML widgets. #' @importFrom evaluate evaluate #' @export html_notebook <- function(toc = FALSE, toc_depth = 3, toc_float = FALSE, number_sections = FALSE, fig_width = 7, fig_height = 5, fig_retina = 2, fig_caption = TRUE, code_folding = "show", theme = "default", highlight = "textmate", highlight_downlit = FALSE, math_method = "default", mathjax = "default", extra_dependencies = NULL, css = NULL, includes = NULL, md_extensions = NULL, pandoc_args = NULL, output_source = NULL, self_contained = TRUE, ...) { # some global state that is captured in pre_knit exit_actions <- list() on_exit <- function() { for (action in exit_actions) try(action()) } paged_table_html_asis = function(x) { knitr::asis_output( paged_table_html(x) ) } # define pre_knit hook pre_knit <- function(input, ...) { if (is.function(output_source)) { # pull out 'output_source' validate_output_source(output_source) # force knitr labeling (required for uniqueness of labels + cache coherence) knitr.duplicate.label <- getOption("knitr.duplicate.label") if (identical(knitr.duplicate.label, "allow")) { warning("unsetting 'knitr.duplicate.label' for duration of render") options(knitr.duplicate.label = "deny") exit_actions <<- c(exit_actions, function() { options(knitr.duplicate.label = knitr.duplicate.label) }) } # force default unnamed chunk labeling scheme (for cache coherence) unnamed.chunk.label <- knitr::opts_knit$get("unnamed.chunk.label") if (!identical(unnamed.chunk.label, "unnamed-chunk")) { warning("reverting 'unnamed.chunk.label' to 'unnamed-chunk' for duration of render") knitr::opts_knit$set(unnamed.chunk.label = "unnamed-chunk") exit_actions <<- c(exit_actions, function() { knitr::opts_knit$set(unnamed.chunk.label = unnamed.chunk.label) }) } # if our output_source comes with a pre_knit hook, evaluate that output_source_pre_knit <- attr(output_source, "pre_knit", exact = TRUE) if (is.function(output_source_pre_knit)) try(output_source_pre_knit()) # track knit context chunk_options <- list() # use an 'include' hook to track chunk options (any # 'opts_hooks' hook will do; we just want this to be called # on entry to any chunk) include_hook <- knitr::opts_hooks$get("include") exit_actions <<- c(exit_actions, function() { knitr::opts_hooks$set( include = if (is.null(include_hook)) { function(options) options } else { include_hook } ) }) knitr::opts_hooks$set(include = function(options) { # save context chunk_options <<- options # force R engine (so that everything goes through evaluate # hook and hence 'output_source') options$engine <- "R" options$engine.opts <- NULL # disable caching options$cache <- FALSE # call original hook if (is.function(include_hook)) include_hook(options) else options }) # set up evaluate hook (override any pre-existing evaluate hook) evaluate_hook <- knitr::knit_hooks$get("evaluate") exit_actions <<- c(exit_actions, function() { knitr::knit_hooks$set(evaluate = evaluate_hook) }) knitr::knit_hooks$set(evaluate = function(code, ...) { chunk_options <- merge_render_context(chunk_options) context <- list2env(chunk_options) output <- output_source(code, context, ...) as_evaluate_output(output, context, ...) }) } # set large max.print for knitr sql engine (since we will give # it a scrolling treatment) knit_sql_max_print <- knitr::opts_knit$get('sql.max.print'); if (is.null(knit_sql_max_print)) { knitr::opts_knit$set(sql.max.print = 1000) exit_actions <<- c(exit_actions, function() { knitr::opts_knit$set(sql.max.print = knit_sql_max_print) }) } # set sql.print to use paged tables knit_sql_print <- knitr::opts_knit$get('sql.print'); if (is.null(knit_sql_print)) { knitr::opts_knit$set(sql.print = paged_table_html) exit_actions <<- c(exit_actions, function() { knitr::opts_knit$set(sql.print = knit_sql_print) }) } } # pre-processor adds kable-scroll argument to give scrolling treatment for # data frames (which are printed via kable by default) pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) { args <- c() args <- c(args, pandoc_variable_arg("kable-scroll", "1")) args } # post-processor to rename output file if necessary post_processor <- function(metadata, input_file, output_file, clean, verbose) { # rename to .nb.html if necessary nb_output_file <- output_file if (!ends_with_bytes(output_file, ".nb.html")) { nb_output_file <- gsub("\\.html$", ".nb.html", output_file) file.rename(output_file, nb_output_file) } nb_output_file } # these arguments to html_document are fixed so we need to # flag them as invalid for users fixed_args <- c("keep_md", "template", "lib_dir", "dev") forwarded_args <- names(list(...)) for (arg in forwarded_args) { if (arg %in% fixed_args) stop("The ", arg, " option is not valid for the html_notebook format.") } # add dependencies extra_dependencies <- append(extra_dependencies, list(html_dependency_pagedtable())) # Change default value of highlight theme if downlit is to be used if (highlight_downlit && missing(highlight)) highlight <- "default" # generate actual format base_format <- html_document(toc = toc, toc_depth = toc_depth, toc_float = toc_float, number_sections = number_sections, fig_width = fig_width, fig_height = fig_height, fig_retina = fig_retina, fig_caption = fig_caption, code_folding = code_folding, theme = theme, highlight = highlight, highlight_downlit = highlight_downlit, math_method = math_method, mathjax = mathjax, extra_dependencies = extra_dependencies, css = css, includes = includes, md_extensions = md_extensions, pandoc_args = pandoc_args, self_contained = self_contained, # options forced for notebooks dev = "png", code_download = TRUE, keep_md = FALSE, template = "default", lib_dir = NULL, ...) rmarkdown::output_format( knitr = html_notebook_knitr_options(), pandoc = NULL, df_print = paged_table_html_asis, pre_knit = pre_knit, pre_processor = pre_processor, post_processor = post_processor, base_format = base_format, on_exit = on_exit ) } #' Parse an HTML Notebook #' #' Parse an HTML notebook, retrieving annotation information #' related to generated outputs in the document, as well as the #' original R Markdown source document. #' #' See the \href{https://rmarkdown.rstudio.com/r_notebook_format.html}{online #' documentation} for additional details on using the \code{html_notebook} #' format. #' @param path The path to an R Notebook file (with extension \code{.nb.html}). #' @export parse_html_notebook <- function(path) { contents <- read_utf8(path) re_comment <- "^\\s*\\s*$" re_document <- "^