blob: 1ad46fcde1a79c5f2280e9b5389ad5d0462a8b96 [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 Allaire4c178052016-01-30 19:35:39 -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
14#' section levels with reveal.js.
15#' @param theme Visual theme ("simple", "sky", "beige", "serif", "solarized",
16#' "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 Allaire4c178052016-01-30 19:35:39 -050019#' @param background_transition Slide background-transition ("default", "none",
20#' "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}
23#' for details).
JJ Allaire82a8dee2016-07-12 10:25:36 -040024#' @param reveal_plugins Reveal plugins to include. Available plugins include "notes",
25#' "search", and "zoom". Note that \code{self_contained} must be set to
26#' \code{FALSE} in order to use Reveal plugins.
JJ Allaire4c178052016-01-30 19:35:39 -050027#' @param template Pandoc template to use for rendering. Pass "default" to use
28#' the rmarkdown package default template; pass \code{NULL} to use pandoc's
29#' built-in template; pass a path to use a custom template that you've
30#' created. Note that if you don't use the "default" template then some
31#' features of \code{revealjs_presentation} won't be available (see the
32#' Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050033#' @param ... Ignored
JJ Allaire4c178052016-01-30 19:35:39 -050034#'
JJ Allaire2ec40242014-09-15 09:18:39 -040035#' @return R Markdown output format to pass to \code{\link{render}}
JJ Allaire4c178052016-01-30 19:35:39 -050036#'
JJ Allaire2ec40242014-09-15 09:18:39 -040037#' @details
JJ Allaire309bc7d2016-01-30 19:17:54 -050038#'
JJ Allaire4c178052016-01-30 19:35:39 -050039#' In reveal.js presentations you can use level 1 or level 2 headers for slides.
40#' If you use a mix of level 1 and level 2 headers then a two-dimensional layout
41#' will be produced, with level 1 headers building horizontally and level 2
42#' headers building vertically.
43#'
44#' For additional documentation on using revealjs presentations see
45#' \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}.
JJ Allaire4c178052016-01-30 19:35:39 -050046#'
JJ Allaire2ec40242014-09-15 09:18:39 -040047#' @examples
48#' \dontrun{
JJ Allaire4c178052016-01-30 19:35:39 -050049#'
JJ Allaire2ec40242014-09-15 09:18:39 -040050#' library(rmarkdown)
51#' library(revealjs)
JJ Allaire4c178052016-01-30 19:35:39 -050052#'
JJ Allaire2ec40242014-09-15 09:18:39 -040053#' # simple invocation
54#' render("pres.Rmd", revealjs_presentation())
JJ Allaire4c178052016-01-30 19:35:39 -050055#'
JJ Allaire2ec40242014-09-15 09:18:39 -040056#' # specify an option for incremental rendering
57#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
58#' }
JJ Allaire4c178052016-01-30 19:35:39 -050059#'
60#'
JJ Allaire2ec40242014-09-15 09:18:39 -040061#' @export
62revealjs_presentation <- function(incremental = FALSE,
63 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050064 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040065 fig_width = 8,
66 fig_height = 6,
67 fig_retina = if (!fig_caption) 2,
68 fig_caption = FALSE,
69 smart = TRUE,
70 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050071 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040072 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010073 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050074 reveal_options = NULL,
JJ Allaire82a8dee2016-07-12 10:25:36 -040075 reveal_plugins = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040076 highlight = "default",
77 mathjax = "default",
78 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040079 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040080 includes = NULL,
81 keep_md = FALSE,
82 lib_dir = NULL,
83 pandoc_args = NULL,
84 ...) {
85
86 # function to lookup reveal resource
87 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -050088 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -040089 package = "revealjs")
90 }
91
92 # base pandoc options for all reveal.js output
93 args <- c()
94
95 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -050096 if (identical(template, "default")) {
97 default_template <- system.file(
98 "rmarkdown/templates/revealjs_presentation/resources/default.html",
99 package = "revealjs"
100 )
101 args <- c(args, "--template", pandoc_path_arg(default_template))
102 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400103 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500104 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400105
106 # incremental
107 if (incremental)
108 args <- c(args, "--incremental")
109
110 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500111 jsbool <- function(value) ifelse(value, "true", "false")
112 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400113
JJ Allaire4c178052016-01-30 19:35:39 -0500114 # slide level
JJ Allairec58a6c42016-03-30 23:45:21 -0400115 args <- c(args, "--slide-level", as.character(slide_level))
JJ Allaire4c178052016-01-30 19:35:39 -0500116
JJ Allaire2ec40242014-09-15 09:18:39 -0400117 # theme
118 theme <- match.arg(theme, revealjs_themes())
119 if (identical(theme, "default"))
120 theme <- "simple"
121 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500122 theme <- "black"
123 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400124 args <- c(args, "--variable", "theme-dark")
125 args <- c(args, "--variable", paste("theme=", theme, sep=""))
126
127
128 # transition
129 transition <- match.arg(transition, revealjs_transitions())
130 args <- c(args, "--variable", paste("transition=", transition, sep=""))
131
junkkad55cff02015-03-15 22:27:03 +0100132 # background_transition
133 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500134 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
135
JJ Allaire4ae486f2016-03-23 06:08:42 -0400136 # use history
137 args <- c(args, pandoc_variable_arg("history", "true"))
JJ Allaire8bfd3642016-03-21 13:43:13 -0400138
JJ Allaire37f45b72016-01-30 18:17:45 -0500139 # additional reveal options
140 if (is.list(reveal_options)) {
141 for (option in names(reveal_options)) {
142 value <- reveal_options[[option]]
143 if (is.logical(value))
144 value <- jsbool(value)
JJ Allairea075efe2016-07-14 21:24:12 -0400145 else if (is.character(value))
146 value <- paste0("'", value, "'")
JJ Allaire37f45b72016-01-30 18:17:45 -0500147 args <- c(args, pandoc_variable_arg(option, value))
148 }
149 }
150
JJ Allaire82a8dee2016-07-12 10:25:36 -0400151 # reveal plugins
152 if (is.character(reveal_plugins)) {
153
154 # validate that we need to use self_contained for plugins
155 if (self_contained)
156 stop("Using reveal_plugins requires self_contained: false")
157
158 # validate specified plugins are supported
159 supported_plugins <- c("notes", "search", "zoom")
160 invalid_plugins <- setdiff(reveal_plugins, supported_plugins)
161 if (length(invalid_plugins) > 0)
162 stop("The following plugin(s) are not supported: ",
163 paste(invalid_plugins, collapse = ", "), call. = FALSE)
164
165 # add plugins
166 sapply(reveal_plugins, function(plugin) {
167 args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1"))
168 })
169 }
170
JJ Allaire2ec40242014-09-15 09:18:39 -0400171 # content includes
172 args <- c(args, includes_to_pandoc_args(includes))
173
JJ Allairefad55232015-10-19 07:47:26 -0400174 # additional css
175 for (css_file in css)
176 args <- c(args, "--css", pandoc_path_arg(css_file))
177
JJ Allaire2ec40242014-09-15 09:18:39 -0400178 # pre-processor for arguments that may depend on the name of the
179 # the input file (e.g. ones that need to copy supporting files)
180 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
181 output_dir) {
182
JJ Allairee60feb22016-01-30 18:28:47 -0500183 # we don't work with runtime shiny
184 if (identical(runtime, "shiny")) {
185 stop("revealjs_presentation is not compatible with runtime 'shiny'",
186 call. = FALSE)
187 }
188
JJ Allaire2ec40242014-09-15 09:18:39 -0400189 # use files_dir as lib_dir if not explicitly specified
190 if (is.null(lib_dir))
191 lib_dir <- files_dir
192
193 # extra args
194 args <- c()
195
196 # reveal.js
Bruce's Thinkpad8b73dcf2016-07-14 00:12:43 +0800197 revealjs_path <- system.file("reveal.js-3.3.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400198 if (!self_contained || identical(.Platform$OS.type, "windows"))
199 revealjs_path <- relative_to(
200 output_dir, render_supporting_files(revealjs_path, lib_dir))
JJ Allaireeea3bca2016-07-13 12:42:01 -0400201 else
202 revealjs_path <- pandoc_path_arg(revealjs_path)
BruceZhao30f9bc02016-07-13 22:33:54 +0800203 args <- c(args, "--variable", paste0("revealjs-url=", revealjs_path))
JJ Allaire2ec40242014-09-15 09:18:39 -0400204
205 # highlight
206 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
207
208 # return additional args
209 args
210 }
211
212 # return format
213 output_format(
214 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
215 pandoc = pandoc_options(to = "revealjs",
216 from = rmarkdown_format(ifelse(fig_caption,
217 "",
218 "-implicit_figures")),
219 args = args),
220 keep_md = keep_md,
221 clean_supporting = self_contained,
222 pre_processor = pre_processor,
223 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
224 self_contained = self_contained,
225 mathjax = mathjax,
226 pandoc_args = pandoc_args, ...))
227}
228
229revealjs_themes <- function() {
230 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500231 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400232 "simple",
233 "sky",
234 "beige",
235 "serif",
236 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400237 "blood",
238 "moon",
junkka77fbf082015-03-15 22:25:47 +0100239 "night",
240 "black",
241 "league",
242 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400243}
244
245
246revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100247 c(
248 "default",
249 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400250 "fade",
junkka77fbf082015-03-15 22:25:47 +0100251 "slide",
252 "convex",
253 "concave",
254 "zoom"
255 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400256}
257
258