blob: 178a08c45f2e8b7c059b2e5fd0ebc1741f2a992e [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 Dervieuxe1893ae2021-10-07 17:09:02 +02004#'
5#' In reveal.js presentations you can use level 1 or level 2 headers for slides.
6#' If you use a mix of level 1 and level 2 headers then a two-dimensional layout
7#' will be produced, with level 1 headers building horizontally and level 2
8#' headers building vertically.
christophe dervieuxf9bae672021-09-21 16:20:24 +02009#'
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020010#' For additional documentation on using revealjs presentations see
11#' <https://github.com/rstudio/revealjs>
12#'
13#' # About plugins
14#'
15#' ## Built-in plugins with reveal.js
16#'
17#' ### Zoom
18#'
19#' When activated, ALT + Click can be used to zoom on a slide.
20#'
21#' ### Notes
22#'
23#' Show a [speaker view](https://revealjs.com/speaker-view/) in a separated
24#' window. This speaker view contains a timer, current slide, next slide, and
25#' speaker notes. It also duplicate the window to have presentation mode
26#' synchronized with main presentation.
27#'
28#' Use
29#' ```markdown
30#' ::: notes
31#' Content of speaker notes
32#' :::
33#' ```
34#' to create notes only viewable in presentation mode.
35#'
36#' ### Search
37#'
38#' When opt-in, it is possible to show a search box when pressing `CTRL + SHIFT +
39#' F`. It will seach in the whole presentation, and highlight matched words. The
40#' matches will also be highlighted in overview mode (pressing ESC to see all
41#' slides in one scrollable view)
42#'
43#' ## Menu
44#'
45#' A slideout menu plugin for Reveal.js to quickly jump to any slide by title.
46#'
47#' Version `r version <- readLines(revealjs_lib_path("plugin", "menu", "VERSION"))` is
48#' currently used and documentation for configurations can be found at
49#' [denehyg/reveal.js-menu](https://github.com/denehyg/reveal.js-menu/blob/`r version`/README.md)
50#'
51#' ### Known limitations
52#'
53#' Some configurations cannot be modified in the current template:
54#'
55#' * `loadIcons: false` the fontawesome icons are loaded by \pkg{rmarkdown}
56#' when this plugin is used
57#' * `custom: false`
58#' * `themes: false`
59#' * `transitions: false`
60
61#' ## Chalkboard
62#'
63#' A plugin adding a chalkboard and slide annotation
64#'
65#' Version `r version <- readLines(revealjs_lib_path("plugin", "chalkboard", "VERSION"))` is
66#' currently used and documentation for configurations can be found at
67#' [rajgoel/reveal.js-plugins](https://github.com/rajgoel/reveal.js-plugins/tree/`r version`/4.1.5/chalkboard)
68#'
69#' By default, chalkboard and annotations modes will be accessible using keyboard
70#' shortcuts, respectively, pressing B, or pressing C.
71#' In addition, buttons on the bottom left can be added by using the following
72#'
73#' ```yaml
74#' reveal_plugins:
75#' - chalkboard
76#' reveal_options:
77#' chalkboard:
78#' toggleNotesButton: true
79#' toggleChalkboardButton: true
80#' ```
JJ Allaire2ec40242014-09-15 09:18:39 -040081#' @inheritParams rmarkdown::beamer_presentation
82#' @inheritParams rmarkdown::pdf_document
83#' @inheritParams rmarkdown::html_document
christophe dervieuxf9bae672021-09-21 16:20:24 +020084#'
christophe dervieuxd26add32021-09-23 16:55:00 +020085#' @param center `TRUE` to vertically center content on slides
christophe dervieuxf9bae672021-09-21 16:20:24 +020086#' @param slide_level Level of heading to denote individual slides. If
christophe dervieuxd26add32021-09-23 16:55:00 +020087#' `slide_level` is 2 (the default), a two-dimensional layout will be
christophe dervieuxf9bae672021-09-21 16:20:24 +020088#' produced, with level 1 headers building horizontally and level 2 headers
89#' building vertically. It is not recommended that you use deeper nesting of
JJ Allaire4c178052016-01-30 19:35:39 -050090#' section levels with reveal.js.
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020091#' @param theme Visual theme (`r knitr::combine_words(setdiff(revealjs_themes(), "default"), before = '"', and = " or ")`)
92#' @param transition Slide transition (
93#' `r (trans <- knitr::combine_words(setdiff(revealjs_transitions(), "default"), before = '"', and = " or "))`
94#' )
95#' @param background_transition Slide background-transition (
96#' `r trans`
97#' )
christophe dervieuxf9bae672021-09-21 16:20:24 +020098#' @param reveal_options Additional options to specify for reveal.js (see
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020099#' <https://revealjs.com/config/> for details). Options for plugins can also
100#' be passed, using plugin name as first level key (e.g `list(slideNumber =
101#' FALSE, menu = list(number = TRUE))`).
JJ Allaire35c0b492017-02-10 09:30:24 -0500102#' @param reveal_plugins Reveal plugins to include. Available plugins include
JJ Allaire064552c2017-02-10 10:28:41 -0500103#' "notes", "search", "zoom", "chalkboard", and "menu". Note that
christophe dervieuxd26add32021-09-23 16:55:00 +0200104#' `self_contained` must be set to `FALSE` in order to use Reveal
JJ Allaire35c0b492017-02-10 09:30:24 -0500105#' plugins.
christophe dervieuxf9bae672021-09-21 16:20:24 +0200106#' @param template Pandoc template to use for rendering. Pass "default" to use
christophe dervieuxd26add32021-09-23 16:55:00 +0200107#' the rmarkdown package default template; pass `NULL` to use pandoc's
christophe dervieuxf9bae672021-09-21 16:20:24 +0200108#' built-in template; pass a path to use a custom template that you've
109#' created. Note that if you don't use the "default" template then some
christophe dervieuxd26add32021-09-23 16:55:00 +0200110#' features of `revealjs_presentation` won't be available (see the
JJ Allaire4c178052016-01-30 19:35:39 -0500111#' Templates section below for more details).
JJ Allaire35c0b492017-02-10 09:30:24 -0500112#' @param extra_dependencies Additional function arguments to pass to the base R
113#' Markdown HTML output formatter [rmarkdown::html_document_base()].
JJ Allaire8d1c2f42016-01-30 14:56:45 -0500114#' @param ... Ignored
christophe dervieuxf9bae672021-09-21 16:20:24 +0200115#'
christophe dervieuxd26add32021-09-23 16:55:00 +0200116#' @return R Markdown output format to pass to [rmarkdown::render()]
christophe dervieuxf9bae672021-09-21 16:20:24 +0200117#'
JJ Allaire2ec40242014-09-15 09:18:39 -0400118#' @examples
119#' \dontrun{
christophe dervieuxf9bae672021-09-21 16:20:24 +0200120#'
JJ Allaire2ec40242014-09-15 09:18:39 -0400121#' library(rmarkdown)
122#' library(revealjs)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200123#'
JJ Allaire2ec40242014-09-15 09:18:39 -0400124#' # simple invocation
125#' render("pres.Rmd", revealjs_presentation())
christophe dervieuxf9bae672021-09-21 16:20:24 +0200126#'
JJ Allaire2ec40242014-09-15 09:18:39 -0400127#' # specify an option for incremental rendering
128#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
129#' }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200130#'
JJ Allaire2ec40242014-09-15 09:18:39 -0400131#' @export
132revealjs_presentation <- function(incremental = FALSE,
133 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -0500134 slide_level = 2,
Christophe Dervieuxaa008e42021-09-23 16:52:37 +0200135 toc = FALSE,
136 toc_depth = 3,
JJ Allaire2ec40242014-09-15 09:18:39 -0400137 fig_width = 8,
138 fig_height = 6,
139 fig_retina = if (!fig_caption) 2,
140 fig_caption = FALSE,
JJ Allaire2ec40242014-09-15 09:18:39 -0400141 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -0500142 theme = "simple",
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200143 transition = "convex",
144 background_transition = "fade",
JJ Allaire37f45b72016-01-30 18:17:45 -0500145 reveal_options = NULL,
JJ Allaire82a8dee2016-07-12 10:25:36 -0400146 reveal_plugins = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -0400147 highlight = "default",
148 mathjax = "default",
149 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -0400150 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -0400151 includes = NULL,
152 keep_md = FALSE,
153 lib_dir = NULL,
154 pandoc_args = NULL,
JJ Allaire375805c2016-11-15 08:56:43 -0500155 extra_dependencies = NULL,
Atsushi Yasumoto0bf44442020-02-15 00:08:30 +0900156 md_extensions = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -0400157 ...) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200158
JJ Allaire2ec40242014-09-15 09:18:39 -0400159 # base pandoc options for all reveal.js output
160 args <- c()
Christophe Dervieuxaa008e42021-09-23 16:52:37 +0200161
162 # table of contents
163 args <- c(args, pandoc_toc_args(toc, toc_depth))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200164
JJ Allaire2ec40242014-09-15 09:18:39 -0400165 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500166 if (identical(template, "default")) {
christophe dervieux92fa4692021-09-21 16:15:17 +0200167 default_template <- reveal_resources("default.html")
JJ Allairea8c414b2016-01-30 14:36:53 -0500168 args <- c(args, "--template", pandoc_path_arg(default_template))
169 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400170 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500171 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200172
JJ Allaire2ec40242014-09-15 09:18:39 -0400173 # incremental
christophe dervieuxf9bae672021-09-21 16:20:24 +0200174 if (incremental) args <- c(args, "--incremental")
175
JJ Allaire2ec40242014-09-15 09:18:39 -0400176 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500177 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200178
JJ Allaire4c178052016-01-30 19:35:39 -0500179 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400180 args <- c(args, "--slide-level", as.character(slide_level))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200181
JJ Allaire2ec40242014-09-15 09:18:39 -0400182 # theme
183 theme <- match.arg(theme, revealjs_themes())
christophe dervieuxf9bae672021-09-21 16:20:24 +0200184 if (identical(theme, "default")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400185 theme <- "simple"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200186 } else if (identical(theme, "dark")) {
JJ Allaire6da1bb62016-01-30 14:28:39 -0500187 theme <- "black"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200188 }
189 if (theme %in% c("blood", "moon", "night", "black")) {
christophe dervieux062940d2021-09-21 16:26:14 +0200190 args <- c(args, pandoc_variable_arg("theme-dark"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200191 }
christophe dervieux1ebd5132021-09-22 08:47:21 +0200192 args <- c(args, pandoc_variable_arg("theme", theme))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200193
Marc Kupietz7c8f7de2023-10-07 11:42:29 +0200194 if (theme == "ids") {
195 args <- c(args, pandoc_variable_arg("theme-ids"))
196 fig_height = 7
197 fig_width = 12
198 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200199
JJ Allaire2ec40242014-09-15 09:18:39 -0400200 # transition
201 transition <- match.arg(transition, revealjs_transitions())
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200202 if (identical(transition, "default")) {
203 # revealjs default is convex
204 transition <- "convex"
205 }
christophe dervieux062940d2021-09-21 16:26:14 +0200206 args <- c(args, pandoc_variable_arg("transition", transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200207
junkkad55cff02015-03-15 22:27:03 +0100208 # background_transition
209 background_transition <- match.arg(background_transition, revealjs_transitions())
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200210 if (identical(background_transition, "default")) {
211 # revealjs default is fade
212 background_transition <- "fade"
213 }
christophe dervieux062940d2021-09-21 16:26:14 +0200214 args <- c(args, pandoc_variable_arg("backgroundTransition", background_transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200215
JJ Allaire4ae486f2016-03-23 06:08:42 -0400216 # use history
217 args <- c(args, pandoc_variable_arg("history", "true"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200218
JJ Allaire37f45b72016-01-30 18:17:45 -0500219 # additional reveal options
220 if (is.list(reveal_options)) {
JJ Allaire375805c2016-11-15 08:56:43 -0500221 for (option in names(reveal_options)) {
JJ Allaire064552c2017-02-10 10:28:41 -0500222 # special handling for nested options
223 if (option %in% c("chalkboard", "menu")) {
224 nested_options <- reveal_options[[option]]
225 for (nested_option in names(nested_options)) {
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200226 args <- c(args,
227 process_reveal_option(
228 paste0(option, "-", nested_option),
229 nested_options[[nested_option]]
230 )
christophe dervieuxf9bae672021-09-21 16:20:24 +0200231 )
JJ Allaire375805c2016-11-15 08:56:43 -0500232 }
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200233 } else {
234 # standard top-level options
235 args <- c(args, process_reveal_option(option, reveal_options[[option]]))
JJ Allaire375805c2016-11-15 08:56:43 -0500236 }
JJ Allaire37f45b72016-01-30 18:17:45 -0500237 }
238 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200239
JJ Allaire82a8dee2016-07-12 10:25:36 -0400240 # reveal plugins
241 if (is.character(reveal_plugins)) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200242
JJ Allaire82a8dee2016-07-12 10:25:36 -0400243 # validate that we need to use self_contained for plugins
christophe dervieuxf9bae672021-09-21 16:20:24 +0200244 if (self_contained) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400245 stop("Using reveal_plugins requires self_contained: false")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200246 }
247
JJ Allaire82a8dee2016-07-12 10:25:36 -0400248 # validate specified plugins are supported
JJ Allaire064552c2017-02-10 10:28:41 -0500249 supported_plugins <- c("notes", "search", "zoom", "chalkboard", "menu")
JJ Allaire82a8dee2016-07-12 10:25:36 -0400250 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200251 if (length(invalid_plugins) > 0) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400252 stop("The following plugin(s) are not supported: ",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200253 paste(invalid_plugins, collapse = ", "),
254 call. = FALSE
255 )
256 }
257
JJ Allaire82a8dee2016-07-12 10:25:36 -0400258 # add plugins
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200259 if ("chalkboard" %in% reveal_plugins) {
260 # chalkboard require customcontrols so we add it to activate in the template
261 reveal_plugins <- c(reveal_plugins, "customcontrols")
262 }
JJ Allaire82a8dee2016-07-12 10:25:36 -0400263 sapply(reveal_plugins, function(plugin) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200264 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin)))
265 if (plugin %in% c("customcontrols", "menu")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200266 extra_dependencies <<- append(
267 extra_dependencies,
268 list(rmarkdown::html_dependency_font_awesome())
269 )
JJ Allaire375805c2016-11-15 08:56:43 -0500270 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200271 })
JJ Allaire82a8dee2016-07-12 10:25:36 -0400272 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200273
JJ Allaire2ec40242014-09-15 09:18:39 -0400274 # content includes
275 args <- c(args, includes_to_pandoc_args(includes))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200276
JJ Allairefad55232015-10-19 07:47:26 -0400277 # additional css
christophe dervieuxf9bae672021-09-21 16:20:24 +0200278 for (css_file in css) {
JJ Allairefad55232015-10-19 07:47:26 -0400279 args <- c(args, "--css", pandoc_path_arg(css_file))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200280 }
281
JJ Allaire2ec40242014-09-15 09:18:39 -0400282 # pre-processor for arguments that may depend on the name of the
283 # the input file (e.g. ones that need to copy supporting files)
284 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
285 output_dir) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200286
JJ Allairee60feb22016-01-30 18:28:47 -0500287 # we don't work with runtime shiny
288 if (identical(runtime, "shiny")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200289 stop("revealjs_presentation is not compatible with runtime 'shiny'",
290 call. = FALSE
291 )
JJ Allairee60feb22016-01-30 18:28:47 -0500292 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200293
JJ Allaire2ec40242014-09-15 09:18:39 -0400294 # use files_dir as lib_dir if not explicitly specified
christophe dervieuxf9bae672021-09-21 16:20:24 +0200295 if (is.null(lib_dir)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400296 lib_dir <- files_dir
christophe dervieuxf9bae672021-09-21 16:20:24 +0200297 }
298
JJ Allaire2ec40242014-09-15 09:18:39 -0400299 # extra args
300 args <- c()
christophe dervieuxf9bae672021-09-21 16:20:24 +0200301
JJ Allaire2ec40242014-09-15 09:18:39 -0400302 # reveal.js
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200303 revealjs_path <- revealjs_lib_path()
christophe dervieuxf9bae672021-09-21 16:20:24 +0200304 if (!self_contained || identical(.Platform$OS.type, "windows")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400305 revealjs_path <- relative_to(
christophe dervieuxf9bae672021-09-21 16:20:24 +0200306 output_dir, render_supporting_files(revealjs_path, lib_dir)
307 )
308 } else {
JJ Allaireeea3bca2016-07-13 12:42:01 -0400309 revealjs_path <- pandoc_path_arg(revealjs_path)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200310 }
christophe dervieux062940d2021-09-21 16:26:14 +0200311 args <- c(args, pandoc_variable_arg("revealjs-url", revealjs_path))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200312
JJ Allaire2ec40242014-09-15 09:18:39 -0400313 # highlight
314 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200315
JJ Allaire2ec40242014-09-15 09:18:39 -0400316 # return additional args
317 args
318 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200319
JJ Allaire2ec40242014-09-15 09:18:39 -0400320 # return format
321 output_format(
322 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200323 pandoc = pandoc_options(
324 to = "revealjs",
christophe dervieuxad3a5752021-09-23 10:24:09 +0200325 from = from_rmarkdown(fig_caption, md_extensions),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200326 args = args
327 ),
JJ Allaire2ec40242014-09-15 09:18:39 -0400328 keep_md = keep_md,
329 clean_supporting = self_contained,
330 pre_processor = pre_processor,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200331 base_format = html_document_base(
christophe dervieux0acc6c92021-09-23 11:18:18 +0200332 lib_dir = lib_dir,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200333 self_contained = self_contained,
334 mathjax = mathjax,
335 pandoc_args = pandoc_args,
336 extra_dependencies = extra_dependencies,
337 ...
338 )
339 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400340}
341
342revealjs_themes <- function() {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200343 c(
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200344 "default", # not used by reveal
Marc Kupietza737b1b2023-10-07 09:32:20 +0200345 "ids",
JJ Allaire2ec40242014-09-15 09:18:39 -0400346 "simple",
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200347 "dark", # our alias for black
348 "black",
JJ Allaire2ec40242014-09-15 09:18:39 -0400349 "sky",
350 "beige",
351 "serif",
352 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400353 "blood",
354 "moon",
junkka77fbf082015-03-15 22:25:47 +0100355 "night",
junkka77fbf082015-03-15 22:25:47 +0100356 "league",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200357 "white"
358 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400359}
360
JJ Allaire2ec40242014-09-15 09:18:39 -0400361revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100362 c(
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200363 "default", # not used by reveal
364 "convex",
JJ Allaire2ec40242014-09-15 09:18:39 -0400365 "fade",
junkka77fbf082015-03-15 22:25:47 +0100366 "slide",
junkka77fbf082015-03-15 22:25:47 +0100367 "concave",
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200368 "zoom",
369 "none"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200370 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400371}