blob: 887fa784b534e415678cb9d9b3d8bed0e71b5191 [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 Allaire5665d542016-01-30 17:38:06 -050016#' @param history \code{TRUE} to push each slide change to the browser history.
JJ Allaire37f45b72016-01-30 18:17:45 -050017#' @param reveal_options Additional options to specify for reveal.js (see
18#' \href{https://github.com/hakimel/reveal.js#configuration}{https://github.com/hakimel/reveal.js#configuration} for details).
JJ Allaire2ec40242014-09-15 09:18:39 -040019#' @param template Pandoc template to use for rendering. Pass "default"
20#' to use the rmarkdown package default template; pass \code{NULL}
21#' to use pandoc's built-in template; pass a path to use a custom template
22#' that you've created. Note that if you don't use the "default" template
23#' then some features of \code{revealjs_presentation} won't be available
24#' (see the Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050025#' @param ... Ignored
JJ Allaire2ec40242014-09-15 09:18:39 -040026#'
27#' @return R Markdown output format to pass to \code{\link{render}}
28#'
29#' @details
30#'
31#' In reveal.js presentations you can use level 1 or level 2 headers for
32#' slides. If you use a mix of level 1 and level 2 headers then a
33#' two-dimensional layout will be produced, with level 1 headers building
34#' horizontally and level 2 headers building vertically.
35#'
36#' For more information on markdown syntax for presentations see
37#' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing
38#' slide shows with pandoc}.
39#'
40#' @section Templates:
41#'
42#' You can provide a custom HTML template to be used for rendering. The syntax
43#' for templates is described in the documentation on
44#' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc
45#' templates}. You can also use the basic pandoc template by passing
46#' \code{template = NULL}.
47#'
48#' Note however that if you choose not to use the "default" reveal.js template
49#' then several aspects of reveal.js presentation rendering will behave
50#' differently:
51#'
52#' \itemize{
53#' \item{The \code{center} parameter does not work (you'd need to
54#' set this directly in the template).
55#' }
56#' \item{The built-in template includes some additional tweaks to styles
57#' to optimize for output from R, these won't be present.
58#' }
59#' \item{MathJax will not work if \code{self_contained} is \code{TRUE}
60#' (these two options can't be used together in normal pandoc templates).
61#' }
62#' }
63#'
64#' @examples
65#' \dontrun{
66#'
67#' library(rmarkdown)
68#' library(revealjs)
69#'
70#' # simple invocation
71#' render("pres.Rmd", revealjs_presentation())
72#'
73#' # specify an option for incremental rendering
74#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
75#' }
76#'
77#'
78#' @export
79revealjs_presentation <- function(incremental = FALSE,
80 center = FALSE,
81 fig_width = 8,
82 fig_height = 6,
83 fig_retina = if (!fig_caption) 2,
84 fig_caption = FALSE,
85 smart = TRUE,
86 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050087 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040088 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010089 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050090 reveal_options = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040091 highlight = "default",
92 mathjax = "default",
93 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040094 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040095 includes = NULL,
96 keep_md = FALSE,
97 lib_dir = NULL,
98 pandoc_args = NULL,
99 ...) {
100
101 # function to lookup reveal resource
102 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -0500103 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -0400104 package = "revealjs")
105 }
106
107 # base pandoc options for all reveal.js output
108 args <- c()
109
110 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500111 if (identical(template, "default")) {
112 default_template <- system.file(
113 "rmarkdown/templates/revealjs_presentation/resources/default.html",
114 package = "revealjs"
115 )
116 args <- c(args, "--template", pandoc_path_arg(default_template))
117 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400118 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500119 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400120
121 # incremental
122 if (incremental)
123 args <- c(args, "--incremental")
124
125 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500126 jsbool <- function(value) ifelse(value, "true", "false")
127 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400128
129 # theme
130 theme <- match.arg(theme, revealjs_themes())
131 if (identical(theme, "default"))
132 theme <- "simple"
133 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500134 theme <- "black"
135 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400136 args <- c(args, "--variable", "theme-dark")
137 args <- c(args, "--variable", paste("theme=", theme, sep=""))
138
139
140 # transition
141 transition <- match.arg(transition, revealjs_transitions())
142 args <- c(args, "--variable", paste("transition=", transition, sep=""))
143
junkkad55cff02015-03-15 22:27:03 +0100144 # background_transition
145 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500146 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
147
JJ Allaire37f45b72016-01-30 18:17:45 -0500148 # additional reveal options
149 if (is.list(reveal_options)) {
150 for (option in names(reveal_options)) {
151 value <- reveal_options[[option]]
152 if (is.logical(value))
153 value <- jsbool(value)
154 args <- c(args, pandoc_variable_arg(option, value))
155 }
156 }
157
JJ Allaire2ec40242014-09-15 09:18:39 -0400158 # content includes
159 args <- c(args, includes_to_pandoc_args(includes))
160
JJ Allairefad55232015-10-19 07:47:26 -0400161 # additional css
162 for (css_file in css)
163 args <- c(args, "--css", pandoc_path_arg(css_file))
164
JJ Allaire2ec40242014-09-15 09:18:39 -0400165 # pre-processor for arguments that may depend on the name of the
166 # the input file (e.g. ones that need to copy supporting files)
167 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
168 output_dir) {
169
JJ Allairee60feb22016-01-30 18:28:47 -0500170 # we don't work with runtime shiny
171 if (identical(runtime, "shiny")) {
172 stop("revealjs_presentation is not compatible with runtime 'shiny'",
173 call. = FALSE)
174 }
175
JJ Allaire2ec40242014-09-15 09:18:39 -0400176 # use files_dir as lib_dir if not explicitly specified
177 if (is.null(lib_dir))
178 lib_dir <- files_dir
179
180 # extra args
181 args <- c()
182
183 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500184 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400185 if (!self_contained || identical(.Platform$OS.type, "windows"))
186 revealjs_path <- relative_to(
187 output_dir, render_supporting_files(revealjs_path, lib_dir))
188 args <- c(args, "--variable", paste("revealjs-url=",
189 pandoc_path_arg(revealjs_path), sep=""))
190
191 # highlight
192 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
193
194 # return additional args
195 args
196 }
197
198 # return format
199 output_format(
200 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
201 pandoc = pandoc_options(to = "revealjs",
202 from = rmarkdown_format(ifelse(fig_caption,
203 "",
204 "-implicit_figures")),
205 args = args),
206 keep_md = keep_md,
207 clean_supporting = self_contained,
208 pre_processor = pre_processor,
209 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
210 self_contained = self_contained,
211 mathjax = mathjax,
212 pandoc_args = pandoc_args, ...))
213}
214
215revealjs_themes <- function() {
216 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500217 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400218 "simple",
219 "sky",
220 "beige",
221 "serif",
222 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400223 "blood",
224 "moon",
junkka77fbf082015-03-15 22:25:47 +0100225 "night",
226 "black",
227 "league",
228 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400229}
230
231
232revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100233 c(
234 "default",
235 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400236 "fade",
junkka77fbf082015-03-15 22:25:47 +0100237 "slide",
238 "convex",
239 "concave",
240 "zoom"
241 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400242}
243
244