blob: 4e9a736751616147b1a03b76e30dc873be7056f2 [file] [log] [blame]
JJ Allaire2ec40242014-09-15 09:18:39 -04001#' Convert to a reveal.js presentation
JJ Allaire4c178052016-01-30 19:35:39 -05002#'
JJ Allaire2ec40242014-09-15 09:18:39 -04003#' Format for converting from R Markdown to a reveal.js presentation.
JJ Allaire4c178052016-01-30 19:35:39 -05004#'
JJ Allaire2ec40242014-09-15 09:18:39 -04005#' @inheritParams rmarkdown::beamer_presentation
6#' @inheritParams rmarkdown::pdf_document
7#' @inheritParams rmarkdown::html_document
JJ Allaire4c178052016-01-30 19:35:39 -05008#'
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
JJ Allaire35c0b492017-02-10 09:30:24 -050013#' @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.
JJ Allaire35c0b492017-02-10 09:30:24 -050018#' @param theme Visual theme ("simple", "sky", "beige", "serif", "solarized",
JJ Allaire4c178052016-01-30 19:35:39 -050019#' "blood", "moon", "night", "black", "league" or "white").
junkkad4b3a162015-03-16 07:49:11 +010020#' @param transition Slide transition ("default", "none", "fade", "slide",
21#' "convex", "concave" or "zoom")
JJ Allaire35c0b492017-02-10 09:30:24 -050022#' @param background_transition Slide background-transition ("default", "none",
JJ Allaire4c178052016-01-30 19:35:39 -050023#' "fade", "slide", "convex", "concave" or "zoom")
24#' @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.
31#' @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
JJ Allaire4c178052016-01-30 19:35:39 -050040#'
JJ Allaire2ec40242014-09-15 09:18:39 -040041#' @return R Markdown output format to pass to \code{\link{render}}
JJ Allaire4c178052016-01-30 19:35:39 -050042#'
JJ Allaire2ec40242014-09-15 09:18:39 -040043#' @details
JJ Allaire309bc7d2016-01-30 19:17:54 -050044#'
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
JJ Allaire35c0b492017-02-10 09:30:24 -050047#' will be produced, with level 1 headers building horizontally and level 2
JJ Allaire4c178052016-01-30 19:35:39 -050048#' headers building vertically.
49#'
JJ Allaire35c0b492017-02-10 09:30:24 -050050#' 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}.
JJ Allaire35c0b492017-02-10 09:30:24 -050052#'
JJ Allaire2ec40242014-09-15 09:18:39 -040053#' @examples
54#' \dontrun{
JJ Allaire4c178052016-01-30 19:35:39 -050055#'
JJ Allaire2ec40242014-09-15 09:18:39 -040056#' library(rmarkdown)
57#' library(revealjs)
JJ Allaire4c178052016-01-30 19:35:39 -050058#'
JJ Allaire2ec40242014-09-15 09:18:39 -040059#' # simple invocation
60#' render("pres.Rmd", revealjs_presentation())
JJ Allaire4c178052016-01-30 19:35:39 -050061#'
JJ Allaire2ec40242014-09-15 09:18:39 -040062#' # specify an option for incremental rendering
63#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
64#' }
JJ Allaire4c178052016-01-30 19:35:39 -050065#'
66#'
JJ Allaire2ec40242014-09-15 09:18:39 -040067#' @export
68revealjs_presentation <- function(incremental = FALSE,
69 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050070 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040071 fig_width = 8,
72 fig_height = 6,
73 fig_retina = if (!fig_caption) 2,
74 fig_caption = FALSE,
75 smart = TRUE,
76 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050077 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040078 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010079 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050080 reveal_options = NULL,
JJ Allaire82a8dee2016-07-12 10:25:36 -040081 reveal_plugins = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040082 highlight = "default",
83 mathjax = "default",
84 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040085 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040086 includes = NULL,
87 keep_md = FALSE,
88 lib_dir = NULL,
89 pandoc_args = NULL,
JJ Allaire375805c2016-11-15 08:56:43 -050090 extra_dependencies = NULL,
Atsushi Yasumoto0bf44442020-02-15 00:08:30 +090091 md_extensions = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040092 ...) {
93
JJ Allaire2ec40242014-09-15 09:18:39 -040094 # base pandoc options for all reveal.js output
95 args <- c()
96
97 # 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 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400104
105 # incremental
106 if (incremental)
107 args <- c(args, "--incremental")
108
109 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500110 jsbool <- function(value) ifelse(value, "true", "false")
111 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400112
JJ Allaire4c178052016-01-30 19:35:39 -0500113 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400114 args <- c(args, "--slide-level", as.character(slide_level))
JJ Allaire4c178052016-01-30 19:35:39 -0500115
JJ Allaire2ec40242014-09-15 09:18:39 -0400116 # theme
117 theme <- match.arg(theme, revealjs_themes())
118 if (identical(theme, "default"))
119 theme <- "simple"
120 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500121 theme <- "black"
122 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400123 args <- c(args, "--variable", "theme-dark")
124 args <- c(args, "--variable", paste("theme=", theme, sep=""))
125
126
127 # transition
128 transition <- match.arg(transition, revealjs_transitions())
129 args <- c(args, "--variable", paste("transition=", transition, sep=""))
130
junkkad55cff02015-03-15 22:27:03 +0100131 # background_transition
132 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500133 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
134
JJ Allaire4ae486f2016-03-23 06:08:42 -0400135 # use history
136 args <- c(args, pandoc_variable_arg("history", "true"))
JJ Allaire8bfd3642016-03-21 13:43:13 -0400137
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
141 add_reveal_option <- function(option, value) {
atusy0e3a0d32021-09-16 19:52:35 +0900142 if (is.logical(value)) {
JJ Allaire37f45b72016-01-30 18:17:45 -0500143 value <- jsbool(value)
atusy0e3a0d32021-09-16 19:52:35 +0900144 } else if (is.character(value)) {
145 # Special handling for some chalkboard plugin options
146 # e.g: color: [ 'rgba(0,0,255,1)', 'rgba(255,255,255,0.5)' ]
147 if (grepl("chalkboard-(background|color|draw|pen)", option)) {
148 value <- sprintf('[%s]', paste(paste0("'", value, "'"), collapse = ", "))
149 }
Christophe Dervieuxd5dc8e92021-07-26 14:50:53 +0200150 # Add quotes around some config that can be several type
christophe dervieuxee7e9b52021-09-16 17:42:17 +0200151 # like number or percent unit or slideNumber = true or slideNumber = 'c/t'
152 if (
153 option %in% c("slideNumber") ||
154 (option %in% c("width", "height") && grepl("%$", value))) {
Christophe Dervieuxd5dc8e92021-07-26 14:50:53 +0200155 value <- paste0("'", value, "'")
156 }
157 }
JJ Allaire375805c2016-11-15 08:56:43 -0500158 args <<- c(args, pandoc_variable_arg(option, value))
159 }
160
161 for (option in names(reveal_options)) {
JJ Allaire064552c2017-02-10 10:28:41 -0500162 # special handling for nested options
163 if (option %in% c("chalkboard", "menu")) {
164 nested_options <- reveal_options[[option]]
165 for (nested_option in names(nested_options)) {
166 add_reveal_option(paste0(option, "-", nested_option),
167 nested_options[[nested_option]])
JJ Allaire375805c2016-11-15 08:56:43 -0500168 }
169 }
170 # standard top-level options
171 else {
172 add_reveal_option(option, reveal_options[[option]])
173 }
JJ Allaire37f45b72016-01-30 18:17:45 -0500174 }
175 }
176
JJ Allaire82a8dee2016-07-12 10:25:36 -0400177 # reveal plugins
178 if (is.character(reveal_plugins)) {
179
180 # validate that we need to use self_contained for plugins
181 if (self_contained)
182 stop("Using reveal_plugins requires self_contained: false")
183
184 # validate specified plugins are supported
JJ Allaire064552c2017-02-10 10:28:41 -0500185 supported_plugins <- c("notes", "search", "zoom", "chalkboard", "menu")
JJ Allaire82a8dee2016-07-12 10:25:36 -0400186 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
187 if (length(invalid_plugins) > 0)
188 stop("The following plugin(s) are not supported: ",
189 paste(invalid_plugins, collapse = ", "), call. = FALSE)
190
191 # add plugins
192 sapply(reveal_plugins, function(plugin) {
193 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1"))
JJ Allaire064552c2017-02-10 10:28:41 -0500194 if (plugin %in% c("chalkboard", "menu")) {
JJ Allaire375805c2016-11-15 08:56:43 -0500195 extra_dependencies <<- append(extra_dependencies,
196 list(rmarkdown::html_dependency_font_awesome()))
197
198 }
JJ Allaire82a8dee2016-07-12 10:25:36 -0400199 })
200 }
201
JJ Allaire2ec40242014-09-15 09:18:39 -0400202 # content includes
203 args <- c(args, includes_to_pandoc_args(includes))
204
JJ Allairefad55232015-10-19 07:47:26 -0400205 # additional css
206 for (css_file in css)
207 args <- c(args, "--css", pandoc_path_arg(css_file))
208
JJ Allaire2ec40242014-09-15 09:18:39 -0400209 # pre-processor for arguments that may depend on the name of the
210 # the input file (e.g. ones that need to copy supporting files)
211 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
212 output_dir) {
213
JJ Allairee60feb22016-01-30 18:28:47 -0500214 # we don't work with runtime shiny
215 if (identical(runtime, "shiny")) {
216 stop("revealjs_presentation is not compatible with runtime 'shiny'",
217 call. = FALSE)
218 }
219
JJ Allaire2ec40242014-09-15 09:18:39 -0400220 # use files_dir as lib_dir if not explicitly specified
221 if (is.null(lib_dir))
222 lib_dir <- files_dir
223
224 # extra args
225 args <- c()
226
227 # reveal.js
JJ Allaire01541a22017-02-10 10:32:28 -0500228 revealjs_path <- system.file("reveal.js-3.3.0.1", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400229 if (!self_contained || identical(.Platform$OS.type, "windows"))
230 revealjs_path <- relative_to(
231 output_dir, render_supporting_files(revealjs_path, lib_dir))
JJ Allaireeea3bca2016-07-13 12:42:01 -0400232 else
233 revealjs_path <- pandoc_path_arg(revealjs_path)
BruceZhao30f9bc02016-07-13 22:33:54 +0800234 args <- c(args, "--variable", paste0("revealjs-url=", revealjs_path))
JJ Allaire2ec40242014-09-15 09:18:39 -0400235
236 # highlight
237 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
238
239 # return additional args
240 args
241 }
242
243 # return format
244 output_format(
245 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
246 pandoc = pandoc_options(to = "revealjs",
Atsushi Yasumoto0bf44442020-02-15 00:08:30 +0900247 from = rmarkdown_format(paste0(
248 md_extensions,
249 ifelse(fig_caption, "", "-implicit_figures")
250 )),
JJ Allaire2ec40242014-09-15 09:18:39 -0400251 args = args),
252 keep_md = keep_md,
253 clean_supporting = self_contained,
254 pre_processor = pre_processor,
255 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
256 self_contained = self_contained,
257 mathjax = mathjax,
JJ Allaire375805c2016-11-15 08:56:43 -0500258 pandoc_args = pandoc_args,
259 extra_dependencies = extra_dependencies,
260 ...))
JJ Allaire2ec40242014-09-15 09:18:39 -0400261}
262
263revealjs_themes <- function() {
264 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500265 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400266 "simple",
267 "sky",
268 "beige",
269 "serif",
270 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400271 "blood",
272 "moon",
junkka77fbf082015-03-15 22:25:47 +0100273 "night",
274 "black",
275 "league",
276 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400277}
278
279
280revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100281 c(
282 "default",
283 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400284 "fade",
junkka77fbf082015-03-15 22:25:47 +0100285 "slide",
286 "convex",
287 "concave",
288 "zoom"
289 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400290}
291
292