blob: f8d9906a21ad356a08e5fb9d733a7b717546a4c3 [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#'
JJ Allaire2ec40242014-09-15 09:18:39 -04009#' @param center \code{TRUE} to vertically center content on slides
JJ Allaire35c0b492017-02-10 09:30:24 -050010#' @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.
JJ Allaire35c0b492017-02-10 09:30:24 -050015#' @param theme Visual theme ("simple", "sky", "beige", "serif", "solarized",
JJ Allaire4c178052016-01-30 19:35:39 -050016#' "blood", "moon", "night", "black", "league" or "white").
junkkad4b3a162015-03-16 07:49:11 +010017#' @param transition Slide transition ("default", "none", "fade", "slide",
18#' "convex", "concave" or "zoom")
JJ Allaire35c0b492017-02-10 09:30:24 -050019#' @param background_transition Slide background-transition ("default", "none",
JJ Allaire4c178052016-01-30 19:35:39 -050020#' "fade", "slide", "convex", "concave" or "zoom")
21#' @param reveal_options Additional options to specify for reveal.js (see
22#' \href{https://github.com/hakimel/reveal.js#configuration}{https://github.com/hakimel/reveal.js#configuration}
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.
28#' @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
JJ Allaire4c178052016-01-30 19:35:39 -050037#'
JJ Allaire2ec40242014-09-15 09:18:39 -040038#' @return R Markdown output format to pass to \code{\link{render}}
JJ Allaire4c178052016-01-30 19:35:39 -050039#'
JJ Allaire2ec40242014-09-15 09:18:39 -040040#' @details
JJ Allaire309bc7d2016-01-30 19:17:54 -050041#'
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
JJ Allaire35c0b492017-02-10 09:30:24 -050044#' will be produced, with level 1 headers building horizontally and level 2
JJ Allaire4c178052016-01-30 19:35:39 -050045#' headers building vertically.
46#'
JJ Allaire35c0b492017-02-10 09:30:24 -050047#' 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}.
JJ Allaire35c0b492017-02-10 09:30:24 -050049#'
JJ Allaire2ec40242014-09-15 09:18:39 -040050#' @examples
51#' \dontrun{
JJ Allaire4c178052016-01-30 19:35:39 -050052#'
JJ Allaire2ec40242014-09-15 09:18:39 -040053#' library(rmarkdown)
54#' library(revealjs)
JJ Allaire4c178052016-01-30 19:35:39 -050055#'
JJ Allaire2ec40242014-09-15 09:18:39 -040056#' # simple invocation
57#' render("pres.Rmd", revealjs_presentation())
JJ Allaire4c178052016-01-30 19:35:39 -050058#'
JJ Allaire2ec40242014-09-15 09:18:39 -040059#' # specify an option for incremental rendering
60#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
61#' }
JJ Allaire4c178052016-01-30 19:35:39 -050062#'
63#'
JJ Allaire2ec40242014-09-15 09:18:39 -040064#' @export
65revealjs_presentation <- function(incremental = FALSE,
66 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050067 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040068 fig_width = 8,
69 fig_height = 6,
70 fig_retina = if (!fig_caption) 2,
71 fig_caption = FALSE,
72 smart = TRUE,
73 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,
JJ Allaire2ec40242014-09-15 09:18:39 -040088 ...) {
89
90 # function to lookup reveal resource
91 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -050092 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -040093 package = "revealjs")
94 }
95
96 # base pandoc options for all reveal.js output
97 args <- c()
98
99 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500100 if (identical(template, "default")) {
101 default_template <- system.file(
102 "rmarkdown/templates/revealjs_presentation/resources/default.html",
103 package = "revealjs"
104 )
105 args <- c(args, "--template", pandoc_path_arg(default_template))
106 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400107 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500108 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400109
110 # incremental
111 if (incremental)
112 args <- c(args, "--incremental")
113
114 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500115 jsbool <- function(value) ifelse(value, "true", "false")
116 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400117
JJ Allaire4c178052016-01-30 19:35:39 -0500118 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400119 args <- c(args, "--slide-level", as.character(slide_level))
JJ Allaire4c178052016-01-30 19:35:39 -0500120
JJ Allaire2ec40242014-09-15 09:18:39 -0400121 # theme
122 theme <- match.arg(theme, revealjs_themes())
123 if (identical(theme, "default"))
124 theme <- "simple"
125 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500126 theme <- "black"
127 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400128 args <- c(args, "--variable", "theme-dark")
129 args <- c(args, "--variable", paste("theme=", theme, sep=""))
130
131
132 # transition
133 transition <- match.arg(transition, revealjs_transitions())
134 args <- c(args, "--variable", paste("transition=", transition, sep=""))
135
junkkad55cff02015-03-15 22:27:03 +0100136 # background_transition
137 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500138 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
139
JJ Allaire4ae486f2016-03-23 06:08:42 -0400140 # use history
141 args <- c(args, pandoc_variable_arg("history", "true"))
JJ Allaire8bfd3642016-03-21 13:43:13 -0400142
JJ Allaire37f45b72016-01-30 18:17:45 -0500143 # additional reveal options
144 if (is.list(reveal_options)) {
JJ Allaire375805c2016-11-15 08:56:43 -0500145
146 add_reveal_option <- function(option, value) {
JJ Allaire37f45b72016-01-30 18:17:45 -0500147 if (is.logical(value))
148 value <- jsbool(value)
JJ Allairea075efe2016-07-14 21:24:12 -0400149 else if (is.character(value))
150 value <- paste0("'", value, "'")
JJ Allaire375805c2016-11-15 08:56:43 -0500151 args <<- c(args, pandoc_variable_arg(option, value))
152 }
153
154 for (option in names(reveal_options)) {
JJ Allaire064552c2017-02-10 10:28:41 -0500155 # special handling for nested options
156 if (option %in% c("chalkboard", "menu")) {
157 nested_options <- reveal_options[[option]]
158 for (nested_option in names(nested_options)) {
159 add_reveal_option(paste0(option, "-", nested_option),
160 nested_options[[nested_option]])
JJ Allaire375805c2016-11-15 08:56:43 -0500161 }
162 }
163 # standard top-level options
164 else {
165 add_reveal_option(option, reveal_options[[option]])
166 }
JJ Allaire37f45b72016-01-30 18:17:45 -0500167 }
168 }
169
JJ Allaire82a8dee2016-07-12 10:25:36 -0400170 # reveal plugins
171 if (is.character(reveal_plugins)) {
172
173 # validate that we need to use self_contained for plugins
174 if (self_contained)
175 stop("Using reveal_plugins requires self_contained: false")
176
177 # validate specified plugins are supported
JJ Allaire064552c2017-02-10 10:28:41 -0500178 supported_plugins <- c("notes", "search", "zoom", "chalkboard", "menu")
JJ Allaire82a8dee2016-07-12 10:25:36 -0400179 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
180 if (length(invalid_plugins) > 0)
181 stop("The following plugin(s) are not supported: ",
182 paste(invalid_plugins, collapse = ", "), call. = FALSE)
183
184 # add plugins
185 sapply(reveal_plugins, function(plugin) {
186 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1"))
JJ Allaire064552c2017-02-10 10:28:41 -0500187 if (plugin %in% c("chalkboard", "menu")) {
JJ Allaire375805c2016-11-15 08:56:43 -0500188 extra_dependencies <<- append(extra_dependencies,
189 list(rmarkdown::html_dependency_font_awesome()))
190
191 }
JJ Allaire82a8dee2016-07-12 10:25:36 -0400192 })
193 }
194
JJ Allaire2ec40242014-09-15 09:18:39 -0400195 # content includes
196 args <- c(args, includes_to_pandoc_args(includes))
197
JJ Allairefad55232015-10-19 07:47:26 -0400198 # additional css
199 for (css_file in css)
200 args <- c(args, "--css", pandoc_path_arg(css_file))
201
JJ Allaire2ec40242014-09-15 09:18:39 -0400202 # pre-processor for arguments that may depend on the name of the
203 # the input file (e.g. ones that need to copy supporting files)
204 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
205 output_dir) {
206
JJ Allairee60feb22016-01-30 18:28:47 -0500207 # we don't work with runtime shiny
208 if (identical(runtime, "shiny")) {
209 stop("revealjs_presentation is not compatible with runtime 'shiny'",
210 call. = FALSE)
211 }
212
JJ Allaire2ec40242014-09-15 09:18:39 -0400213 # use files_dir as lib_dir if not explicitly specified
214 if (is.null(lib_dir))
215 lib_dir <- files_dir
216
217 # extra args
218 args <- c()
219
220 # reveal.js
Bruce's Thinkpad8b73dcf2016-07-14 00:12:43 +0800221 revealjs_path <- system.file("reveal.js-3.3.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400222 if (!self_contained || identical(.Platform$OS.type, "windows"))
223 revealjs_path <- relative_to(
224 output_dir, render_supporting_files(revealjs_path, lib_dir))
JJ Allaireeea3bca2016-07-13 12:42:01 -0400225 else
226 revealjs_path <- pandoc_path_arg(revealjs_path)
BruceZhao30f9bc02016-07-13 22:33:54 +0800227 args <- c(args, "--variable", paste0("revealjs-url=", revealjs_path))
JJ Allaire2ec40242014-09-15 09:18:39 -0400228
229 # highlight
230 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
231
232 # return additional args
233 args
234 }
235
236 # return format
237 output_format(
238 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
239 pandoc = pandoc_options(to = "revealjs",
240 from = rmarkdown_format(ifelse(fig_caption,
241 "",
242 "-implicit_figures")),
243 args = args),
244 keep_md = keep_md,
245 clean_supporting = self_contained,
246 pre_processor = pre_processor,
247 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
248 self_contained = self_contained,
249 mathjax = mathjax,
JJ Allaire375805c2016-11-15 08:56:43 -0500250 pandoc_args = pandoc_args,
251 extra_dependencies = extra_dependencies,
252 ...))
JJ Allaire2ec40242014-09-15 09:18:39 -0400253}
254
255revealjs_themes <- function() {
256 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500257 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400258 "simple",
259 "sky",
260 "beige",
261 "serif",
262 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400263 "blood",
264 "moon",
junkka77fbf082015-03-15 22:25:47 +0100265 "night",
266 "black",
267 "league",
268 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400269}
270
271
272revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100273 c(
274 "default",
275 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400276 "fade",
junkka77fbf082015-03-15 22:25:47 +0100277 "slide",
278 "convex",
279 "concave",
280 "zoom"
281 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400282}
283
284