blob: d8a83c8dc26ba8d5aae28408be7db58b526a0c4e [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.
JJ Allaire309bc7d2016-01-30 19:17:54 -050034#'
35#' For additional documentation on using revealjs presentations see \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}.
36#'
JJ Allaire2ec40242014-09-15 09:18:39 -040037#' @section Templates:
38#'
39#' You can provide a custom HTML template to be used for rendering. The syntax
40#' for templates is described in the documentation on
41#' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc
42#' templates}. You can also use the basic pandoc template by passing
43#' \code{template = NULL}.
44#'
45#' Note however that if you choose not to use the "default" reveal.js template
46#' then several aspects of reveal.js presentation rendering will behave
47#' differently:
48#'
49#' \itemize{
50#' \item{The \code{center} parameter does not work (you'd need to
51#' set this directly in the template).
52#' }
53#' \item{The built-in template includes some additional tweaks to styles
54#' to optimize for output from R, these won't be present.
55#' }
56#' \item{MathJax will not work if \code{self_contained} is \code{TRUE}
57#' (these two options can't be used together in normal pandoc templates).
58#' }
59#' }
60#'
61#' @examples
62#' \dontrun{
63#'
64#' library(rmarkdown)
65#' library(revealjs)
66#'
67#' # simple invocation
68#' render("pres.Rmd", revealjs_presentation())
69#'
70#' # specify an option for incremental rendering
71#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
72#' }
73#'
74#'
75#' @export
76revealjs_presentation <- function(incremental = FALSE,
77 center = FALSE,
78 fig_width = 8,
79 fig_height = 6,
80 fig_retina = if (!fig_caption) 2,
81 fig_caption = FALSE,
82 smart = TRUE,
83 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050084 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040085 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010086 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050087 reveal_options = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040088 highlight = "default",
89 mathjax = "default",
90 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040091 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040092 includes = NULL,
93 keep_md = FALSE,
94 lib_dir = NULL,
95 pandoc_args = NULL,
96 ...) {
97
98 # function to lookup reveal resource
99 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -0500100 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -0400101 package = "revealjs")
102 }
103
104 # base pandoc options for all reveal.js output
105 args <- c()
106
107 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500108 if (identical(template, "default")) {
109 default_template <- system.file(
110 "rmarkdown/templates/revealjs_presentation/resources/default.html",
111 package = "revealjs"
112 )
113 args <- c(args, "--template", pandoc_path_arg(default_template))
114 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400115 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500116 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400117
118 # incremental
119 if (incremental)
120 args <- c(args, "--incremental")
121
122 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500123 jsbool <- function(value) ifelse(value, "true", "false")
124 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400125
126 # theme
127 theme <- match.arg(theme, revealjs_themes())
128 if (identical(theme, "default"))
129 theme <- "simple"
130 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500131 theme <- "black"
132 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400133 args <- c(args, "--variable", "theme-dark")
134 args <- c(args, "--variable", paste("theme=", theme, sep=""))
135
136
137 # transition
138 transition <- match.arg(transition, revealjs_transitions())
139 args <- c(args, "--variable", paste("transition=", transition, sep=""))
140
junkkad55cff02015-03-15 22:27:03 +0100141 # background_transition
142 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500143 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
144
JJ Allaire37f45b72016-01-30 18:17:45 -0500145 # additional reveal options
146 if (is.list(reveal_options)) {
147 for (option in names(reveal_options)) {
148 value <- reveal_options[[option]]
149 if (is.logical(value))
150 value <- jsbool(value)
151 args <- c(args, pandoc_variable_arg(option, value))
152 }
153 }
154
JJ Allaire2ec40242014-09-15 09:18:39 -0400155 # content includes
156 args <- c(args, includes_to_pandoc_args(includes))
157
JJ Allairefad55232015-10-19 07:47:26 -0400158 # additional css
159 for (css_file in css)
160 args <- c(args, "--css", pandoc_path_arg(css_file))
161
JJ Allaire2ec40242014-09-15 09:18:39 -0400162 # pre-processor for arguments that may depend on the name of the
163 # the input file (e.g. ones that need to copy supporting files)
164 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
165 output_dir) {
166
JJ Allairee60feb22016-01-30 18:28:47 -0500167 # we don't work with runtime shiny
168 if (identical(runtime, "shiny")) {
169 stop("revealjs_presentation is not compatible with runtime 'shiny'",
170 call. = FALSE)
171 }
172
JJ Allaire2ec40242014-09-15 09:18:39 -0400173 # use files_dir as lib_dir if not explicitly specified
174 if (is.null(lib_dir))
175 lib_dir <- files_dir
176
177 # extra args
178 args <- c()
179
180 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500181 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400182 if (!self_contained || identical(.Platform$OS.type, "windows"))
183 revealjs_path <- relative_to(
184 output_dir, render_supporting_files(revealjs_path, lib_dir))
185 args <- c(args, "--variable", paste("revealjs-url=",
186 pandoc_path_arg(revealjs_path), sep=""))
187
188 # highlight
189 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
190
191 # return additional args
192 args
193 }
194
195 # return format
196 output_format(
197 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
198 pandoc = pandoc_options(to = "revealjs",
199 from = rmarkdown_format(ifelse(fig_caption,
200 "",
201 "-implicit_figures")),
202 args = args),
203 keep_md = keep_md,
204 clean_supporting = self_contained,
205 pre_processor = pre_processor,
206 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
207 self_contained = self_contained,
208 mathjax = mathjax,
209 pandoc_args = pandoc_args, ...))
210}
211
212revealjs_themes <- function() {
213 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500214 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400215 "simple",
216 "sky",
217 "beige",
218 "serif",
219 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400220 "blood",
221 "moon",
junkka77fbf082015-03-15 22:25:47 +0100222 "night",
223 "black",
224 "league",
225 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400226}
227
228
229revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100230 c(
231 "default",
232 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400233 "fade",
junkka77fbf082015-03-15 22:25:47 +0100234 "slide",
235 "convex",
236 "concave",
237 "zoom"
238 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400239}
240
241