blob: de0e37be5021ece6024138ff0394900de749fa72 [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#'
christophe dervieuxd26add32021-09-23 16:55:00 +02009#' @param center `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
christophe dervieuxd26add32021-09-23 16:55:00 +020011#' `slide_level` is 2 (the default), a two-dimensional layout will be
christophe dervieuxf9bae672021-09-21 16:20:24 +020012#' 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
christophe dervieuxd26add32021-09-23 16:55:00 +020022#' <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
christophe dervieuxd26add32021-09-23 16:55:00 +020026#' `self_contained` must be set to `FALSE` in order to use Reveal
JJ Allaire35c0b492017-02-10 09:30:24 -050027#' plugins.
christophe dervieuxf9bae672021-09-21 16:20:24 +020028#' @param template Pandoc template to use for rendering. Pass "default" to use
christophe dervieuxd26add32021-09-23 16:55:00 +020029#' the rmarkdown package default template; pass `NULL` to use pandoc's
christophe dervieuxf9bae672021-09-21 16:20:24 +020030#' 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
christophe dervieuxd26add32021-09-23 16:55:00 +020032#' features of `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#'
christophe dervieuxd26add32021-09-23 16:55:00 +020038#' @return R Markdown output format to pass to [rmarkdown::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
christophe dervieuxd26add32021-09-23 16:55:00 +020048#' <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,
Christophe Dervieuxaa008e42021-09-23 16:52:37 +020067 toc = FALSE,
68 toc_depth = 3,
JJ Allaire2ec40242014-09-15 09:18:39 -040069 fig_width = 8,
70 fig_height = 6,
71 fig_retina = if (!fig_caption) 2,
72 fig_caption = FALSE,
JJ Allaire2ec40242014-09-15 09:18:39 -040073 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050074 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040075 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010076 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050077 reveal_options = NULL,
JJ Allaire82a8dee2016-07-12 10:25:36 -040078 reveal_plugins = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040079 highlight = "default",
80 mathjax = "default",
81 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040082 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040083 includes = NULL,
84 keep_md = FALSE,
85 lib_dir = NULL,
86 pandoc_args = NULL,
JJ Allaire375805c2016-11-15 08:56:43 -050087 extra_dependencies = NULL,
Atsushi Yasumoto0bf44442020-02-15 00:08:30 +090088 md_extensions = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040089 ...) {
christophe dervieuxf9bae672021-09-21 16:20:24 +020090
JJ Allaire2ec40242014-09-15 09:18:39 -040091 # base pandoc options for all reveal.js output
92 args <- c()
Christophe Dervieuxaa008e42021-09-23 16:52:37 +020093
94 # table of contents
95 args <- c(args, pandoc_toc_args(toc, toc_depth))
christophe dervieuxf9bae672021-09-21 16:20:24 +020096
JJ Allaire2ec40242014-09-15 09:18:39 -040097 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -050098 if (identical(template, "default")) {
christophe dervieux92fa4692021-09-21 16:15:17 +020099 default_template <- reveal_resources("default.html")
JJ Allairea8c414b2016-01-30 14:36:53 -0500100 args <- c(args, "--template", pandoc_path_arg(default_template))
101 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400102 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500103 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200104
JJ Allaire2ec40242014-09-15 09:18:39 -0400105 # incremental
christophe dervieuxf9bae672021-09-21 16:20:24 +0200106 if (incremental) args <- c(args, "--incremental")
107
JJ Allaire2ec40242014-09-15 09:18:39 -0400108 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500109 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200110
JJ Allaire4c178052016-01-30 19:35:39 -0500111 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400112 args <- c(args, "--slide-level", as.character(slide_level))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200113
JJ Allaire2ec40242014-09-15 09:18:39 -0400114 # theme
115 theme <- match.arg(theme, revealjs_themes())
christophe dervieuxf9bae672021-09-21 16:20:24 +0200116 if (identical(theme, "default")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400117 theme <- "simple"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200118 } else if (identical(theme, "dark")) {
JJ Allaire6da1bb62016-01-30 14:28:39 -0500119 theme <- "black"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200120 }
121 if (theme %in% c("blood", "moon", "night", "black")) {
christophe dervieux062940d2021-09-21 16:26:14 +0200122 args <- c(args, pandoc_variable_arg("theme-dark"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200123 }
christophe dervieux1ebd5132021-09-22 08:47:21 +0200124 args <- c(args, pandoc_variable_arg("theme", theme))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200125
126
JJ Allaire2ec40242014-09-15 09:18:39 -0400127 # transition
128 transition <- match.arg(transition, revealjs_transitions())
christophe dervieux062940d2021-09-21 16:26:14 +0200129 args <- c(args, pandoc_variable_arg("transition", transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200130
junkkad55cff02015-03-15 22:27:03 +0100131 # background_transition
132 background_transition <- match.arg(background_transition, revealjs_transitions())
christophe dervieux062940d2021-09-21 16:26:14 +0200133 args <- c(args, pandoc_variable_arg("backgroundTransition", background_transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200134
JJ Allaire4ae486f2016-03-23 06:08:42 -0400135 # use history
136 args <- c(args, pandoc_variable_arg("history", "true"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200137
JJ Allaire37f45b72016-01-30 18:17:45 -0500138 # additional reveal options
139 if (is.list(reveal_options)) {
JJ Allaire375805c2016-11-15 08:56:43 -0500140 for (option in names(reveal_options)) {
JJ Allaire064552c2017-02-10 10:28:41 -0500141 # special handling for nested options
142 if (option %in% c("chalkboard", "menu")) {
143 nested_options <- reveal_options[[option]]
144 for (nested_option in names(nested_options)) {
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200145 args <- c(args,
146 process_reveal_option(
147 paste0(option, "-", nested_option),
148 nested_options[[nested_option]]
149 )
christophe dervieuxf9bae672021-09-21 16:20:24 +0200150 )
JJ Allaire375805c2016-11-15 08:56:43 -0500151 }
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200152 } else {
153 # standard top-level options
154 args <- c(args, process_reveal_option(option, reveal_options[[option]]))
JJ Allaire375805c2016-11-15 08:56:43 -0500155 }
JJ Allaire37f45b72016-01-30 18:17:45 -0500156 }
157 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200158
JJ Allaire82a8dee2016-07-12 10:25:36 -0400159 # reveal plugins
160 if (is.character(reveal_plugins)) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200161
JJ Allaire82a8dee2016-07-12 10:25:36 -0400162 # validate that we need to use self_contained for plugins
christophe dervieuxf9bae672021-09-21 16:20:24 +0200163 if (self_contained) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400164 stop("Using reveal_plugins requires self_contained: false")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200165 }
166
JJ Allaire82a8dee2016-07-12 10:25:36 -0400167 # validate specified plugins are supported
JJ Allaire064552c2017-02-10 10:28:41 -0500168 supported_plugins <- c("notes", "search", "zoom", "chalkboard", "menu")
JJ Allaire82a8dee2016-07-12 10:25:36 -0400169 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200170 if (length(invalid_plugins) > 0) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400171 stop("The following plugin(s) are not supported: ",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200172 paste(invalid_plugins, collapse = ", "),
173 call. = FALSE
174 )
175 }
176
JJ Allaire82a8dee2016-07-12 10:25:36 -0400177 # add plugins
178 sapply(reveal_plugins, function(plugin) {
179 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1"))
JJ Allaire064552c2017-02-10 10:28:41 -0500180 if (plugin %in% c("chalkboard", "menu")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200181 extra_dependencies <<- append(
182 extra_dependencies,
183 list(rmarkdown::html_dependency_font_awesome())
184 )
JJ Allaire375805c2016-11-15 08:56:43 -0500185 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200186 })
JJ Allaire82a8dee2016-07-12 10:25:36 -0400187 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200188
JJ Allaire2ec40242014-09-15 09:18:39 -0400189 # content includes
190 args <- c(args, includes_to_pandoc_args(includes))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200191
JJ Allairefad55232015-10-19 07:47:26 -0400192 # additional css
christophe dervieuxf9bae672021-09-21 16:20:24 +0200193 for (css_file in css) {
JJ Allairefad55232015-10-19 07:47:26 -0400194 args <- c(args, "--css", pandoc_path_arg(css_file))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200195 }
196
JJ Allaire2ec40242014-09-15 09:18:39 -0400197 # pre-processor for arguments that may depend on the name of the
198 # the input file (e.g. ones that need to copy supporting files)
199 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
200 output_dir) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200201
JJ Allairee60feb22016-01-30 18:28:47 -0500202 # we don't work with runtime shiny
203 if (identical(runtime, "shiny")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200204 stop("revealjs_presentation is not compatible with runtime 'shiny'",
205 call. = FALSE
206 )
JJ Allairee60feb22016-01-30 18:28:47 -0500207 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200208
JJ Allaire2ec40242014-09-15 09:18:39 -0400209 # use files_dir as lib_dir if not explicitly specified
christophe dervieuxf9bae672021-09-21 16:20:24 +0200210 if (is.null(lib_dir)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400211 lib_dir <- files_dir
christophe dervieuxf9bae672021-09-21 16:20:24 +0200212 }
213
JJ Allaire2ec40242014-09-15 09:18:39 -0400214 # extra args
215 args <- c()
christophe dervieuxf9bae672021-09-21 16:20:24 +0200216
JJ Allaire2ec40242014-09-15 09:18:39 -0400217 # reveal.js
JJ Allaire01541a22017-02-10 10:32:28 -0500218 revealjs_path <- system.file("reveal.js-3.3.0.1", package = "revealjs")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200219 if (!self_contained || identical(.Platform$OS.type, "windows")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400220 revealjs_path <- relative_to(
christophe dervieuxf9bae672021-09-21 16:20:24 +0200221 output_dir, render_supporting_files(revealjs_path, lib_dir)
222 )
223 } else {
JJ Allaireeea3bca2016-07-13 12:42:01 -0400224 revealjs_path <- pandoc_path_arg(revealjs_path)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200225 }
christophe dervieux062940d2021-09-21 16:26:14 +0200226 args <- c(args, pandoc_variable_arg("revealjs-url", revealjs_path))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200227
JJ Allaire2ec40242014-09-15 09:18:39 -0400228 # highlight
229 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200230
JJ Allaire2ec40242014-09-15 09:18:39 -0400231 # return additional args
232 args
233 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200234
JJ Allaire2ec40242014-09-15 09:18:39 -0400235 # return format
236 output_format(
237 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200238 pandoc = pandoc_options(
239 to = "revealjs",
christophe dervieuxad3a5752021-09-23 10:24:09 +0200240 from = from_rmarkdown(fig_caption, md_extensions),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200241 args = args
242 ),
JJ Allaire2ec40242014-09-15 09:18:39 -0400243 keep_md = keep_md,
244 clean_supporting = self_contained,
245 pre_processor = pre_processor,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200246 base_format = html_document_base(
christophe dervieux0acc6c92021-09-23 11:18:18 +0200247 lib_dir = lib_dir,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200248 self_contained = self_contained,
249 mathjax = mathjax,
250 pandoc_args = pandoc_args,
251 extra_dependencies = extra_dependencies,
252 ...
253 )
254 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400255}
256
257revealjs_themes <- function() {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200258 c(
259 "default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500260 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400261 "simple",
262 "sky",
263 "beige",
264 "serif",
265 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400266 "blood",
267 "moon",
junkka77fbf082015-03-15 22:25:47 +0100268 "night",
269 "black",
270 "league",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200271 "white"
272 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400273}
274
275
276revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100277 c(
278 "default",
279 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400280 "fade",
junkka77fbf082015-03-15 22:25:47 +0100281 "slide",
282 "convex",
283 "concave",
284 "zoom"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200285 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400286}