blob: f64313865f8d069ffd6c40d75b64cf8f623730eb [file] [log] [blame]
JJ Allaire2ec40242014-09-15 09:18:39 -04001#' Convert to a reveal.js presentation
2#'
3#' Format for converting from R Markdown to a reveal.js presentation.
4#'
5#' @inheritParams rmarkdown::beamer_presentation
6#' @inheritParams rmarkdown::pdf_document
7#' @inheritParams rmarkdown::html_document
8#'
9#' @param center \code{TRUE} to vertically center content on slides
JJ Allaire6da1bb62016-01-30 14:28:39 -050010#' @param theme Visual theme ("simple", "sky", "beige", "serif",
junkkad4b3a162015-03-16 07:49:11 +010011#' "solarized", "blood", "moon", "night", "black", "league" or "white").
12#' @param transition Slide transition ("default", "none", "fade", "slide",
13#' "convex", "concave" or "zoom")
14#' @param background_transition Slide background-transition ("default", "none", "fade", "slide",
15#' "convex", "concave" or "zoom")
JJ Allaire37f45b72016-01-30 18:17:45 -050016#' @param reveal_options Additional options to specify for reveal.js (see
17#' \href{https://github.com/hakimel/reveal.js#configuration}{https://github.com/hakimel/reveal.js#configuration} for details).
JJ Allaire2ec40242014-09-15 09:18:39 -040018#' @param template Pandoc template to use for rendering. Pass "default"
19#' to use the rmarkdown package default template; pass \code{NULL}
20#' to use pandoc's built-in template; pass a path to use a custom template
21#' that you've created. Note that if you don't use the "default" template
22#' then some features of \code{revealjs_presentation} won't be available
23#' (see the Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050024#' @param ... Ignored
JJ Allaire2ec40242014-09-15 09:18:39 -040025#'
26#' @return R Markdown output format to pass to \code{\link{render}}
27#'
28#' @details
29#'
30#' In reveal.js presentations you can use level 1 or level 2 headers for
31#' slides. If you use a mix of level 1 and level 2 headers then a
32#' two-dimensional layout will be produced, with level 1 headers building
33#' horizontally and level 2 headers building vertically.
34#'
35#' For more information on markdown syntax for presentations see
36#' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing
37#' slide shows with pandoc}.
38#'
39#' @section Templates:
40#'
41#' You can provide a custom HTML template to be used for rendering. The syntax
42#' for templates is described in the documentation on
43#' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc
44#' templates}. You can also use the basic pandoc template by passing
45#' \code{template = NULL}.
46#'
47#' Note however that if you choose not to use the "default" reveal.js template
48#' then several aspects of reveal.js presentation rendering will behave
49#' differently:
50#'
51#' \itemize{
52#' \item{The \code{center} parameter does not work (you'd need to
53#' set this directly in the template).
54#' }
55#' \item{The built-in template includes some additional tweaks to styles
56#' to optimize for output from R, these won't be present.
57#' }
58#' \item{MathJax will not work if \code{self_contained} is \code{TRUE}
59#' (these two options can't be used together in normal pandoc templates).
60#' }
61#' }
62#'
63#' @examples
64#' \dontrun{
65#'
66#' library(rmarkdown)
67#' library(revealjs)
68#'
69#' # simple invocation
70#' render("pres.Rmd", revealjs_presentation())
71#'
72#' # specify an option for incremental rendering
73#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
74#' }
75#'
76#'
77#' @export
78revealjs_presentation <- function(incremental = FALSE,
79 center = FALSE,
80 fig_width = 8,
81 fig_height = 6,
82 fig_retina = if (!fig_caption) 2,
83 fig_caption = FALSE,
84 smart = TRUE,
85 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050086 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040087 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010088 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050089 reveal_options = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040090 highlight = "default",
91 mathjax = "default",
92 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040093 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040094 includes = NULL,
95 keep_md = FALSE,
96 lib_dir = NULL,
97 pandoc_args = NULL,
98 ...) {
99
100 # function to lookup reveal resource
101 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -0500102 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -0400103 package = "revealjs")
104 }
105
106 # base pandoc options for all reveal.js output
107 args <- c()
108
109 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500110 if (identical(template, "default")) {
111 default_template <- system.file(
112 "rmarkdown/templates/revealjs_presentation/resources/default.html",
113 package = "revealjs"
114 )
115 args <- c(args, "--template", pandoc_path_arg(default_template))
116 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400117 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500118 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400119
120 # incremental
121 if (incremental)
122 args <- c(args, "--incremental")
123
124 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500125 jsbool <- function(value) ifelse(value, "true", "false")
126 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400127
128 # theme
129 theme <- match.arg(theme, revealjs_themes())
130 if (identical(theme, "default"))
131 theme <- "simple"
132 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500133 theme <- "black"
134 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400135 args <- c(args, "--variable", "theme-dark")
136 args <- c(args, "--variable", paste("theme=", theme, sep=""))
137
138
139 # transition
140 transition <- match.arg(transition, revealjs_transitions())
141 args <- c(args, "--variable", paste("transition=", transition, sep=""))
142
junkkad55cff02015-03-15 22:27:03 +0100143 # background_transition
144 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500145 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
146
JJ Allaire37f45b72016-01-30 18:17:45 -0500147 # additional reveal options
148 if (is.list(reveal_options)) {
149 for (option in names(reveal_options)) {
150 value <- reveal_options[[option]]
151 if (is.logical(value))
152 value <- jsbool(value)
153 args <- c(args, pandoc_variable_arg(option, value))
154 }
155 }
156
JJ Allaire2ec40242014-09-15 09:18:39 -0400157 # content includes
158 args <- c(args, includes_to_pandoc_args(includes))
159
JJ Allairefad55232015-10-19 07:47:26 -0400160 # additional css
161 for (css_file in css)
162 args <- c(args, "--css", pandoc_path_arg(css_file))
163
JJ Allaire2ec40242014-09-15 09:18:39 -0400164 # pre-processor for arguments that may depend on the name of the
165 # the input file (e.g. ones that need to copy supporting files)
166 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
167 output_dir) {
168
JJ Allairee60feb22016-01-30 18:28:47 -0500169 # we don't work with runtime shiny
170 if (identical(runtime, "shiny")) {
171 stop("revealjs_presentation is not compatible with runtime 'shiny'",
172 call. = FALSE)
173 }
174
JJ Allaire2ec40242014-09-15 09:18:39 -0400175 # use files_dir as lib_dir if not explicitly specified
176 if (is.null(lib_dir))
177 lib_dir <- files_dir
178
179 # extra args
180 args <- c()
181
182 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500183 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400184 if (!self_contained || identical(.Platform$OS.type, "windows"))
185 revealjs_path <- relative_to(
186 output_dir, render_supporting_files(revealjs_path, lib_dir))
187 args <- c(args, "--variable", paste("revealjs-url=",
188 pandoc_path_arg(revealjs_path), sep=""))
189
190 # highlight
191 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
192
193 # return additional args
194 args
195 }
196
197 # return format
198 output_format(
199 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
200 pandoc = pandoc_options(to = "revealjs",
201 from = rmarkdown_format(ifelse(fig_caption,
202 "",
203 "-implicit_figures")),
204 args = args),
205 keep_md = keep_md,
206 clean_supporting = self_contained,
207 pre_processor = pre_processor,
208 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
209 self_contained = self_contained,
210 mathjax = mathjax,
211 pandoc_args = pandoc_args, ...))
212}
213
214revealjs_themes <- function() {
215 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500216 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400217 "simple",
218 "sky",
219 "beige",
220 "serif",
221 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400222 "blood",
223 "moon",
junkka77fbf082015-03-15 22:25:47 +0100224 "night",
225 "black",
226 "league",
227 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400228}
229
230
231revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100232 c(
233 "default",
234 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400235 "fade",
junkka77fbf082015-03-15 22:25:47 +0100236 "slide",
237 "convex",
238 "concave",
239 "zoom"
240 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400241}
242
243