blob: 360465ba79c5a7e7268bccba109b773f481f3648 [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 Dervieux21239cf2021-09-15 15:34:01 +02009#' @param smart Produce typographically correct output, converting straight
10#' quotes to curly quotes, \code{---} to em-dashes, \code{--} to en-dashes, and
11#' \code{...} to ellipses.
JJ Allaire2ec40242014-09-15 09:18:39 -040012#' @param center \code{TRUE} to vertically center content on slides
christophe dervieuxf9bae672021-09-21 16:20:24 +020013#' @param slide_level Level of heading to denote individual slides. If
14#' \code{slide_level} is 2 (the default), a two-dimensional layout will be
15#' produced, with level 1 headers building horizontally and level 2 headers
16#' building vertically. It is not recommended that you use deeper nesting of
JJ Allaire4c178052016-01-30 19:35:39 -050017#' section levels with reveal.js.
christophe dervieuxf9bae672021-09-21 16:20:24 +020018#' @param theme Visual theme ("simple", "sky", "beige", "serif", "solarized",
JJ Allaire4c178052016-01-30 19:35:39 -050019#' "blood", "moon", "night", "black", "league" or "white").
christophe dervieuxf9bae672021-09-21 16:20:24 +020020#' @param transition Slide transition ("default", "none", "fade", "slide",
junkkad4b3a162015-03-16 07:49:11 +010021#' "convex", "concave" or "zoom")
christophe dervieuxf9bae672021-09-21 16:20:24 +020022#' @param background_transition Slide background-transition ("default", "none",
JJ Allaire4c178052016-01-30 19:35:39 -050023#' "fade", "slide", "convex", "concave" or "zoom")
christophe dervieuxf9bae672021-09-21 16:20:24 +020024#' @param reveal_options Additional options to specify for reveal.js (see
Atsushi Yasumoto66c55852020-08-10 21:50:04 +090025#' \href{https://revealjs.com/config/}{https://revealjs.com/config/}
JJ Allaire35c0b492017-02-10 09:30:24 -050026#' for details).
27#' @param reveal_plugins Reveal plugins to include. Available plugins include
JJ Allaire064552c2017-02-10 10:28:41 -050028#' "notes", "search", "zoom", "chalkboard", and "menu". Note that
JJ Allaire35c0b492017-02-10 09:30:24 -050029#' \code{self_contained} must be set to \code{FALSE} in order to use Reveal
30#' plugins.
christophe dervieuxf9bae672021-09-21 16:20:24 +020031#' @param template Pandoc template to use for rendering. Pass "default" to use
32#' the rmarkdown package default template; pass \code{NULL} to use pandoc's
33#' built-in template; pass a path to use a custom template that you've
34#' created. Note that if you don't use the "default" template then some
35#' features of \code{revealjs_presentation} won't be available (see the
JJ Allaire4c178052016-01-30 19:35:39 -050036#' Templates section below for more details).
JJ Allaire35c0b492017-02-10 09:30:24 -050037#' @param extra_dependencies Additional function arguments to pass to the base R
38#' Markdown HTML output formatter [rmarkdown::html_document_base()].
JJ Allaire8d1c2f42016-01-30 14:56:45 -050039#' @param ... Ignored
christophe dervieuxf9bae672021-09-21 16:20:24 +020040#'
JJ Allaire2ec40242014-09-15 09:18:39 -040041#' @return R Markdown output format to pass to \code{\link{render}}
christophe dervieuxf9bae672021-09-21 16:20:24 +020042#'
JJ Allaire2ec40242014-09-15 09:18:39 -040043#' @details
christophe dervieuxf9bae672021-09-21 16:20:24 +020044#'
JJ Allaire4c178052016-01-30 19:35:39 -050045#' In reveal.js presentations you can use level 1 or level 2 headers for slides.
46#' If you use a mix of level 1 and level 2 headers then a two-dimensional layout
christophe dervieuxf9bae672021-09-21 16:20:24 +020047#' will be produced, with level 1 headers building horizontally and level 2
JJ Allaire4c178052016-01-30 19:35:39 -050048#' headers building vertically.
christophe dervieuxf9bae672021-09-21 16:20:24 +020049#'
50#' For additional documentation on using revealjs presentations see
JJ Allaire4c178052016-01-30 19:35:39 -050051#' \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}.
christophe dervieuxf9bae672021-09-21 16:20:24 +020052#'
JJ Allaire2ec40242014-09-15 09:18:39 -040053#' @examples
54#' \dontrun{
christophe dervieuxf9bae672021-09-21 16:20:24 +020055#'
JJ Allaire2ec40242014-09-15 09:18:39 -040056#' library(rmarkdown)
57#' library(revealjs)
christophe dervieuxf9bae672021-09-21 16:20:24 +020058#'
JJ Allaire2ec40242014-09-15 09:18:39 -040059#' # simple invocation
60#' render("pres.Rmd", revealjs_presentation())
christophe dervieuxf9bae672021-09-21 16:20:24 +020061#'
JJ Allaire2ec40242014-09-15 09:18:39 -040062#' # specify an option for incremental rendering
63#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
64#' }
christophe dervieuxf9bae672021-09-21 16:20:24 +020065#'
JJ Allaire2ec40242014-09-15 09:18:39 -040066#' @export
67revealjs_presentation <- function(incremental = FALSE,
68 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050069 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040070 fig_width = 8,
71 fig_height = 6,
72 fig_retina = if (!fig_caption) 2,
73 fig_caption = FALSE,
74 smart = TRUE,
75 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050076 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040077 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010078 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050079 reveal_options = NULL,
JJ Allaire82a8dee2016-07-12 10:25:36 -040080 reveal_plugins = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040081 highlight = "default",
82 mathjax = "default",
83 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040084 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040085 includes = NULL,
86 keep_md = FALSE,
87 lib_dir = NULL,
88 pandoc_args = NULL,
JJ Allaire375805c2016-11-15 08:56:43 -050089 extra_dependencies = NULL,
Atsushi Yasumoto0bf44442020-02-15 00:08:30 +090090 md_extensions = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040091 ...) {
christophe dervieuxf9bae672021-09-21 16:20:24 +020092
JJ Allaire2ec40242014-09-15 09:18:39 -040093 # base pandoc options for all reveal.js output
94 args <- c()
christophe dervieuxf9bae672021-09-21 16:20:24 +020095
JJ Allaire2ec40242014-09-15 09:18:39 -040096 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -050097 if (identical(template, "default")) {
christophe dervieux92fa4692021-09-21 16:15:17 +020098 default_template <- reveal_resources("default.html")
JJ Allairea8c414b2016-01-30 14:36:53 -050099 args <- c(args, "--template", pandoc_path_arg(default_template))
100 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400101 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500102 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200103
JJ Allaire2ec40242014-09-15 09:18:39 -0400104 # incremental
christophe dervieuxf9bae672021-09-21 16:20:24 +0200105 if (incremental) args <- c(args, "--incremental")
106
JJ Allaire2ec40242014-09-15 09:18:39 -0400107 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500108 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200109
JJ Allaire4c178052016-01-30 19:35:39 -0500110 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400111 args <- c(args, "--slide-level", as.character(slide_level))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200112
JJ Allaire2ec40242014-09-15 09:18:39 -0400113 # theme
114 theme <- match.arg(theme, revealjs_themes())
christophe dervieuxf9bae672021-09-21 16:20:24 +0200115 if (identical(theme, "default")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400116 theme <- "simple"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200117 } else if (identical(theme, "dark")) {
JJ Allaire6da1bb62016-01-30 14:28:39 -0500118 theme <- "black"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200119 }
120 if (theme %in% c("blood", "moon", "night", "black")) {
christophe dervieux062940d2021-09-21 16:26:14 +0200121 args <- c(args, pandoc_variable_arg("theme-dark"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200122 }
christophe dervieux1ebd5132021-09-22 08:47:21 +0200123 args <- c(args, pandoc_variable_arg("theme", theme))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200124
125
JJ Allaire2ec40242014-09-15 09:18:39 -0400126 # transition
127 transition <- match.arg(transition, revealjs_transitions())
christophe dervieux062940d2021-09-21 16:26:14 +0200128 args <- c(args, pandoc_variable_arg("transition", transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200129
junkkad55cff02015-03-15 22:27:03 +0100130 # background_transition
131 background_transition <- match.arg(background_transition, revealjs_transitions())
christophe dervieux062940d2021-09-21 16:26:14 +0200132 args <- c(args, pandoc_variable_arg("backgroundTransition", background_transition))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200133
JJ Allaire4ae486f2016-03-23 06:08:42 -0400134 # use history
135 args <- c(args, pandoc_variable_arg("history", "true"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200136
JJ Allaire37f45b72016-01-30 18:17:45 -0500137 # additional reveal options
138 if (is.list(reveal_options)) {
JJ Allaire375805c2016-11-15 08:56:43 -0500139 for (option in names(reveal_options)) {
JJ Allaire064552c2017-02-10 10:28:41 -0500140 # special handling for nested options
141 if (option %in% c("chalkboard", "menu")) {
142 nested_options <- reveal_options[[option]]
143 for (nested_option in names(nested_options)) {
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200144 args <- c(args,
145 process_reveal_option(
146 paste0(option, "-", nested_option),
147 nested_options[[nested_option]]
148 )
christophe dervieuxf9bae672021-09-21 16:20:24 +0200149 )
JJ Allaire375805c2016-11-15 08:56:43 -0500150 }
Christophe Dervieux2f01dc92021-09-22 09:37:18 +0200151 } else {
152 # standard top-level options
153 args <- c(args, process_reveal_option(option, reveal_options[[option]]))
JJ Allaire375805c2016-11-15 08:56:43 -0500154 }
JJ Allaire37f45b72016-01-30 18:17:45 -0500155 }
156 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200157
JJ Allaire82a8dee2016-07-12 10:25:36 -0400158 # reveal plugins
159 if (is.character(reveal_plugins)) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200160
JJ Allaire82a8dee2016-07-12 10:25:36 -0400161 # validate that we need to use self_contained for plugins
christophe dervieuxf9bae672021-09-21 16:20:24 +0200162 if (self_contained) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400163 stop("Using reveal_plugins requires self_contained: false")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200164 }
165
JJ Allaire82a8dee2016-07-12 10:25:36 -0400166 # validate specified plugins are supported
JJ Allaire064552c2017-02-10 10:28:41 -0500167 supported_plugins <- c("notes", "search", "zoom", "chalkboard", "menu")
JJ Allaire82a8dee2016-07-12 10:25:36 -0400168 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200169 if (length(invalid_plugins) > 0) {
JJ Allaire82a8dee2016-07-12 10:25:36 -0400170 stop("The following plugin(s) are not supported: ",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200171 paste(invalid_plugins, collapse = ", "),
172 call. = FALSE
173 )
174 }
175
JJ Allaire82a8dee2016-07-12 10:25:36 -0400176 # add plugins
177 sapply(reveal_plugins, function(plugin) {
178 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1"))
JJ Allaire064552c2017-02-10 10:28:41 -0500179 if (plugin %in% c("chalkboard", "menu")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200180 extra_dependencies <<- append(
181 extra_dependencies,
182 list(rmarkdown::html_dependency_font_awesome())
183 )
JJ Allaire375805c2016-11-15 08:56:43 -0500184 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200185 })
JJ Allaire82a8dee2016-07-12 10:25:36 -0400186 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200187
JJ Allaire2ec40242014-09-15 09:18:39 -0400188 # content includes
189 args <- c(args, includes_to_pandoc_args(includes))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200190
JJ Allairefad55232015-10-19 07:47:26 -0400191 # additional css
christophe dervieuxf9bae672021-09-21 16:20:24 +0200192 for (css_file in css) {
JJ Allairefad55232015-10-19 07:47:26 -0400193 args <- c(args, "--css", pandoc_path_arg(css_file))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200194 }
195
JJ Allaire2ec40242014-09-15 09:18:39 -0400196 # pre-processor for arguments that may depend on the name of the
197 # the input file (e.g. ones that need to copy supporting files)
198 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
199 output_dir) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200200
JJ Allairee60feb22016-01-30 18:28:47 -0500201 # we don't work with runtime shiny
202 if (identical(runtime, "shiny")) {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200203 stop("revealjs_presentation is not compatible with runtime 'shiny'",
204 call. = FALSE
205 )
JJ Allairee60feb22016-01-30 18:28:47 -0500206 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200207
JJ Allaire2ec40242014-09-15 09:18:39 -0400208 # use files_dir as lib_dir if not explicitly specified
christophe dervieuxf9bae672021-09-21 16:20:24 +0200209 if (is.null(lib_dir)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400210 lib_dir <- files_dir
christophe dervieuxf9bae672021-09-21 16:20:24 +0200211 }
212
JJ Allaire2ec40242014-09-15 09:18:39 -0400213 # extra args
214 args <- c()
christophe dervieuxf9bae672021-09-21 16:20:24 +0200215
JJ Allaire2ec40242014-09-15 09:18:39 -0400216 # reveal.js
JJ Allaire01541a22017-02-10 10:32:28 -0500217 revealjs_path <- system.file("reveal.js-3.3.0.1", package = "revealjs")
christophe dervieuxf9bae672021-09-21 16:20:24 +0200218 if (!self_contained || identical(.Platform$OS.type, "windows")) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400219 revealjs_path <- relative_to(
christophe dervieuxf9bae672021-09-21 16:20:24 +0200220 output_dir, render_supporting_files(revealjs_path, lib_dir)
221 )
222 } else {
JJ Allaireeea3bca2016-07-13 12:42:01 -0400223 revealjs_path <- pandoc_path_arg(revealjs_path)
christophe dervieuxf9bae672021-09-21 16:20:24 +0200224 }
christophe dervieux062940d2021-09-21 16:26:14 +0200225 args <- c(args, pandoc_variable_arg("revealjs-url", revealjs_path))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200226
JJ Allaire2ec40242014-09-15 09:18:39 -0400227 # highlight
228 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
christophe dervieuxf9bae672021-09-21 16:20:24 +0200229
JJ Allaire2ec40242014-09-15 09:18:39 -0400230 # return additional args
231 args
232 }
christophe dervieuxf9bae672021-09-21 16:20:24 +0200233
JJ Allaire2ec40242014-09-15 09:18:39 -0400234 # return format
235 output_format(
236 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
christophe dervieuxf9bae672021-09-21 16:20:24 +0200237 pandoc = pandoc_options(
238 to = "revealjs",
239 from = rmarkdown_format(paste0(
240 md_extensions,
241 ifelse(fig_caption, "", "-implicit_figures")
242 )),
243 args = args
244 ),
JJ Allaire2ec40242014-09-15 09:18:39 -0400245 keep_md = keep_md,
246 clean_supporting = self_contained,
247 pre_processor = pre_processor,
christophe dervieuxf9bae672021-09-21 16:20:24 +0200248 base_format = html_document_base(
249 smart = smart, lib_dir = lib_dir,
250 self_contained = self_contained,
251 mathjax = mathjax,
252 pandoc_args = pandoc_args,
253 extra_dependencies = extra_dependencies,
254 ...
255 )
256 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400257}
258
259revealjs_themes <- function() {
christophe dervieuxf9bae672021-09-21 16:20:24 +0200260 c(
261 "default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500262 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400263 "simple",
264 "sky",
265 "beige",
266 "serif",
267 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400268 "blood",
269 "moon",
junkka77fbf082015-03-15 22:25:47 +0100270 "night",
271 "black",
272 "league",
christophe dervieuxf9bae672021-09-21 16:20:24 +0200273 "white"
274 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400275}
276
277
278revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100279 c(
280 "default",
281 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400282 "fade",
junkka77fbf082015-03-15 22:25:47 +0100283 "slide",
284 "convex",
285 "concave",
286 "zoom"
christophe dervieuxf9bae672021-09-21 16:20:24 +0200287 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400288}