blob: 12edafcf65ad99e805b479dfe2d713ad455e657f [file] [log] [blame]
JJ Allaire2ec40242014-09-15 09:18:39 -04001#' Convert to a reveal.js presentation
christophe dervieuxf9bae672021-09-21 16:20:24 +02002#'
JJ Allaire2ec40242014-09-15 09:18:39 -04003#' Format for converting from R Markdown to a reveal.js presentation.
christophe dervieuxf9bae672021-09-21 16:20:24 +02004#'
JJ Allaire2ec40242014-09-15 09:18:39 -04005#' @inheritParams rmarkdown::beamer_presentation
6#' @inheritParams rmarkdown::pdf_document
7#' @inheritParams rmarkdown::html_document
christophe dervieuxf9bae672021-09-21 16:20:24 +02008#'
JJ Allaire2ec40242014-09-15 09:18:39 -04009#' @param center \code{TRUE} to vertically center content on slides
christophe dervieuxf9bae672021-09-21 16:20:24 +020010#' @param slide_level Level of heading to denote individual slides. If
11#' \code{slide_level} is 2 (the default), a two-dimensional layout will be
12#' produced, with level 1 headers building horizontally and level 2 headers
13#' building vertically. It is not recommended that you use deeper nesting of
JJ Allaire4c178052016-01-30 19:35:39 -050014#' section levels with reveal.js.
christophe dervieuxf9bae672021-09-21 16:20:24 +020015#' @param theme Visual theme ("simple", "sky", "beige", "serif", "solarized",
JJ Allaire4c178052016-01-30 19:35:39 -050016#' "blood", "moon", "night", "black", "league" or "white").
christophe dervieuxf9bae672021-09-21 16:20:24 +020017#' @param transition Slide transition ("default", "none", "fade", "slide",
junkkad4b3a162015-03-16 07:49:11 +010018#' "convex", "concave" or "zoom")
christophe dervieuxf9bae672021-09-21 16:20:24 +020019#' @param background_transition Slide background-transition ("default", "none",
JJ Allaire4c178052016-01-30 19:35:39 -050020#' "fade", "slide", "convex", "concave" or "zoom")
christophe dervieuxf9bae672021-09-21 16:20:24 +020021#' @param reveal_options Additional options to specify for reveal.js (see
Atsushi Yasumoto66c55852020-08-10 21:50:04 +090022#' \href{https://revealjs.com/config/}{https://revealjs.com/config/}
JJ Allaire35c0b492017-02-10 09:30:24 -050023#' for details).
24#' @param reveal_plugins Reveal plugins to include. Available plugins include
JJ Allaire064552c2017-02-10 10:28:41 -050025#' "notes", "search", "zoom", "chalkboard", and "menu". Note that
JJ Allaire35c0b492017-02-10 09:30:24 -050026#' \code{self_contained} must be set to \code{FALSE} in order to use Reveal
27#' plugins.
christophe dervieuxf9bae672021-09-21 16:20:24 +020028#' @param template Pandoc template to use for rendering. Pass "default" to use
29#' the rmarkdown package default template; pass \code{NULL} to use pandoc's
30#' built-in template; pass a path to use a custom template that you've
31#' created. Note that if you don't use the "default" template then some
32#' features of \code{revealjs_presentation} won't be available (see the
JJ Allaire4c178052016-01-30 19:35:39 -050033#' Templates section below for more details).
JJ Allaire35c0b492017-02-10 09:30:24 -050034#' @param extra_dependencies Additional function arguments to pass to the base R
35#' Markdown HTML output formatter [rmarkdown::html_document_base()].
JJ Allaire8d1c2f42016-01-30 14:56:45 -050036#' @param ... Ignored
christophe dervieuxf9bae672021-09-21 16:20:24 +020037#'
JJ Allaire2ec40242014-09-15 09:18:39 -040038#' @return R Markdown output format to pass to \code{\link{render}}
christophe dervieuxf9bae672021-09-21 16:20:24 +020039#'
JJ Allaire2ec40242014-09-15 09:18:39 -040040#' @details
christophe dervieuxf9bae672021-09-21 16:20:24 +020041#'
JJ Allaire4c178052016-01-30 19:35:39 -050042#' In reveal.js presentations you can use level 1 or level 2 headers for slides.
43#' If you use a mix of level 1 and level 2 headers then a two-dimensional layout
christophe dervieuxf9bae672021-09-21 16:20:24 +020044#' will be produced, with level 1 headers building horizontally and level 2
JJ Allaire4c178052016-01-30 19:35:39 -050045#' headers building vertically.
christophe dervieuxf9bae672021-09-21 16:20:24 +020046#'
47#' For additional documentation on using revealjs presentations see
JJ Allaire4c178052016-01-30 19:35:39 -050048#' \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}.
christophe dervieuxf9bae672021-09-21 16:20:24 +020049#'
JJ Allaire2ec40242014-09-15 09:18:39 -040050#' @examples
51#' \dontrun{
christophe dervieuxf9bae672021-09-21 16:20:24 +020052#'
JJ Allaire2ec40242014-09-15 09:18:39 -040053#' library(rmarkdown)
54#' library(revealjs)
christophe dervieuxf9bae672021-09-21 16:20:24 +020055#'
JJ Allaire2ec40242014-09-15 09:18:39 -040056#' # simple invocation
57#' render("pres.Rmd", revealjs_presentation())
christophe dervieuxf9bae672021-09-21 16:20:24 +020058#'
JJ Allaire2ec40242014-09-15 09:18:39 -040059#' # specify an option for incremental rendering
60#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
61#' }
christophe dervieuxf9bae672021-09-21 16:20:24 +020062#'
JJ Allaire2ec40242014-09-15 09:18:39 -040063#' @export
64revealjs_presentation <- function(incremental = FALSE,
65 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050066 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040067 fig_width = 8,
68 fig_height = 6,
69 fig_retina = if (!fig_caption) 2,
70 fig_caption = FALSE,
JJ Allaire2ec40242014-09-15 09:18:39 -040071 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050072 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040073 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010074 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050075 reveal_options = NULL,
JJ Allaire82a8dee2016-07-12 10:25:36 -040076 reveal_plugins = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040077 highlight = "default",
78 mathjax = "default",
79 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040080 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040081 includes = NULL,
82 keep_md = FALSE,
83 lib_dir = NULL,
84 pandoc_args = NULL,
JJ Allaire375805c2016-11-15 08:56:43 -050085 extra_dependencies = NULL,
Atsushi Yasumoto0bf44442020-02-15 00:08:30 +090086 md_extensions = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040087 ...) {
christophe dervieuxf9bae672021-09-21 16:20:24 +020088
JJ Allaire2ec40242014-09-15 09:18:39 -040089 # base pandoc options for all reveal.js output
90 args <- c()
christophe dervieuxf9bae672021-09-21 16:20:24 +020091
JJ Allaire2ec40242014-09-15 09:18:39 -040092 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -050093 if (identical(template, "default")) {
christophe dervieux92fa4692021-09-21 16:15:17 +020094 default_template <- reveal_resources("default.html")
JJ Allairea8c414b2016-01-30 14:36:53 -050095 args <- c(args, "--template", pandoc_path_arg(default_template))
96 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -040097 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -050098 }
christophe dervieuxf9bae672021-09-21 16:20:24 +020099
JJ Allaire2ec40242014-09-15 09:18:39 -0400100 # incremental
christophe dervieuxf9bae672021-09-21 16:20:24 +0200101 if (incremental) args <- c(args, "--incremental")
102
JJ Allaire2ec40242014-09-15 09:18:39 -0400103 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500104 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200105
JJ Allaire4c178052016-01-30 19:35:39 -0500106 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400107 args <- c(args, "--slide-level", as.character(slide_level))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200108
JJ Allaire2ec40242014-09-15 09:18:39 -0400109 # theme
110 theme <- match.arg(theme, revealjs_themes())
christophe dervieuxf9bae672021-09-21 16:20:24 +0200111 if (identical(theme, "default")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400112 theme <- "simple"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200113 } else if (identical(theme, "dark")) {
JJ Allaire6da1bb62016-01-30 14:28:39 -0500114 theme <- "black"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200115 }
116 if (theme %in% c("blood", "moon", "night", "black")) {
christophe dervieux062940d2021-09-21 16:26:14 +0200117 args <- c(args, pandoc_variable_arg("theme-dark"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200118 }
christophe dervieux1ebd5132021-09-22 08:47:21 +0200119 args <- c(args, pandoc_variable_arg("theme", theme))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200120
121
JJ Allaire2ec40242014-09-15 09:18:39 -0400122 # transition
123 transition <- match.arg(transition, revealjs_transitions())
christophe dervieux062940d2021-09-21 16:26:14 +0200124 args <- c(args, pandoc_variable_arg("transition", transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200125
junkkad55cff02015-03-15 22:27:03 +0100126 # background_transition
127 background_transition <- match.arg(background_transition, revealjs_transitions())
christophe dervieux062940d2021-09-21 16:26:14 +0200128 args <- c(args, pandoc_variable_arg("backgroundTransition", background_transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200129
JJ Allaire4ae486f2016-03-23 06:08:42 -0400130 # use history
131 args <- c(args, pandoc_variable_arg("history", "true"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200132
JJ Allaire37f45b72016-01-30 18:17:45 -0500133 # additional reveal options
134 if (is.list(reveal_options)) {
JJ Allaire375805c2016-11-15 08:56:43 -0500135 for (option in names(reveal_options)) {
JJ Allaire064552c2017-02-10 10:28:41 -0500136 # special handling for nested options
137 if (option %in% c("chalkboard", "menu")) {
138 nested_options <- reveal_options[[option]]
139 for (nested_option in names(nested_options)) {
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200140 args <- c(args,
141 process_reveal_option(
142 paste0(option, "-", nested_option),
143 nested_options[[nested_option]]
144 )
christophe dervieuxf9bae672021-09-21 16:20:24 +0200145 )
JJ Allaire375805c2016-11-15 08:56:43 -0500146 }
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200147 } else {
148 # standard top-level options
149 args <- c(args, process_reveal_option(option, reveal_options[[option]]))
JJ Allaire375805c2016-11-15 08:56:43 -0500150 }
JJ Allaire37f45b72016-01-30 18:17:45 -0500151 }
152 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200153
JJ Allaire82a8dee2016-07-12 10:25:36 -0400154 # reveal plugins
155 if (is.character(reveal_plugins)) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200156
JJ Allaire82a8dee2016-07-12 10:25:36 -0400157 # validate that we need to use self_contained for plugins
christophe dervieuxf9bae672021-09-21 16:20:24 +0200158 if (self_contained) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400159 stop("Using reveal_plugins requires self_contained: false")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200160 }
161
JJ Allaire82a8dee2016-07-12 10:25:36 -0400162 # validate specified plugins are supported
JJ Allaire064552c2017-02-10 10:28:41 -0500163 supported_plugins <- c("notes", "search", "zoom", "chalkboard", "menu")
JJ Allaire82a8dee2016-07-12 10:25:36 -0400164 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200165 if (length(invalid_plugins) > 0) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400166 stop("The following plugin(s) are not supported: ",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200167 paste(invalid_plugins, collapse = ", "),
168 call. = FALSE
169 )
170 }
171
JJ Allaire82a8dee2016-07-12 10:25:36 -0400172 # add plugins
173 sapply(reveal_plugins, function(plugin) {
174 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1"))
JJ Allaire064552c2017-02-10 10:28:41 -0500175 if (plugin %in% c("chalkboard", "menu")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200176 extra_dependencies <<- append(
177 extra_dependencies,
178 list(rmarkdown::html_dependency_font_awesome())
179 )
JJ Allaire375805c2016-11-15 08:56:43 -0500180 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200181 })
JJ Allaire82a8dee2016-07-12 10:25:36 -0400182 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200183
JJ Allaire2ec40242014-09-15 09:18:39 -0400184 # content includes
185 args <- c(args, includes_to_pandoc_args(includes))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200186
JJ Allairefad55232015-10-19 07:47:26 -0400187 # additional css
christophe dervieuxf9bae672021-09-21 16:20:24 +0200188 for (css_file in css) {
JJ Allairefad55232015-10-19 07:47:26 -0400189 args <- c(args, "--css", pandoc_path_arg(css_file))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200190 }
191
JJ Allaire2ec40242014-09-15 09:18:39 -0400192 # pre-processor for arguments that may depend on the name of the
193 # the input file (e.g. ones that need to copy supporting files)
194 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
195 output_dir) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200196
JJ Allairee60feb22016-01-30 18:28:47 -0500197 # we don't work with runtime shiny
198 if (identical(runtime, "shiny")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200199 stop("revealjs_presentation is not compatible with runtime 'shiny'",
200 call. = FALSE
201 )
JJ Allairee60feb22016-01-30 18:28:47 -0500202 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200203
JJ Allaire2ec40242014-09-15 09:18:39 -0400204 # use files_dir as lib_dir if not explicitly specified
christophe dervieuxf9bae672021-09-21 16:20:24 +0200205 if (is.null(lib_dir)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400206 lib_dir <- files_dir
christophe dervieuxf9bae672021-09-21 16:20:24 +0200207 }
208
JJ Allaire2ec40242014-09-15 09:18:39 -0400209 # extra args
210 args <- c()
christophe dervieuxf9bae672021-09-21 16:20:24 +0200211
JJ Allaire2ec40242014-09-15 09:18:39 -0400212 # reveal.js
JJ Allaire01541a22017-02-10 10:32:28 -0500213 revealjs_path <- system.file("reveal.js-3.3.0.1", package = "revealjs")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200214 if (!self_contained || identical(.Platform$OS.type, "windows")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400215 revealjs_path <- relative_to(
christophe dervieuxf9bae672021-09-21 16:20:24 +0200216 output_dir, render_supporting_files(revealjs_path, lib_dir)
217 )
218 } else {
JJ Allaireeea3bca2016-07-13 12:42:01 -0400219 revealjs_path <- pandoc_path_arg(revealjs_path)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200220 }
christophe dervieux062940d2021-09-21 16:26:14 +0200221 args <- c(args, pandoc_variable_arg("revealjs-url", revealjs_path))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200222
JJ Allaire2ec40242014-09-15 09:18:39 -0400223 # highlight
224 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200225
JJ Allaire2ec40242014-09-15 09:18:39 -0400226 # return additional args
227 args
228 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200229
JJ Allaire2ec40242014-09-15 09:18:39 -0400230 # return format
231 output_format(
232 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200233 pandoc = pandoc_options(
234 to = "revealjs",
christophe dervieuxad3a5752021-09-23 10:24:09 +0200235 from = from_rmarkdown(fig_caption, md_extensions),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200236 args = args
237 ),
JJ Allaire2ec40242014-09-15 09:18:39 -0400238 keep_md = keep_md,
239 clean_supporting = self_contained,
240 pre_processor = pre_processor,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200241 base_format = html_document_base(
christophe dervieux0acc6c92021-09-23 11:18:18 +0200242 lib_dir = lib_dir,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200243 self_contained = self_contained,
244 mathjax = mathjax,
245 pandoc_args = pandoc_args,
246 extra_dependencies = extra_dependencies,
247 ...
248 )
249 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400250}
251
252revealjs_themes <- function() {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200253 c(
254 "default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500255 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400256 "simple",
257 "sky",
258 "beige",
259 "serif",
260 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400261 "blood",
262 "moon",
junkka77fbf082015-03-15 22:25:47 +0100263 "night",
264 "black",
265 "league",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200266 "white"
267 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400268}
269
270
271revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100272 c(
273 "default",
274 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400275 "fade",
junkka77fbf082015-03-15 22:25:47 +0100276 "slide",
277 "convex",
278 "concave",
279 "zoom"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200280 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400281}