blob: 6af3dc2154a6c5f9b9479b21a6877263c1311318 [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).
24#' @param template Pandoc template to use for rendering. Pass "default" to use
25#' the rmarkdown package default template; pass \code{NULL} to use pandoc's
26#' built-in template; pass a path to use a custom template that you've
27#' created. Note that if you don't use the "default" template then some
28#' features of \code{revealjs_presentation} won't be available (see the
29#' Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050030#' @param ... Ignored
JJ Allaire4c178052016-01-30 19:35:39 -050031#'
JJ Allaire2ec40242014-09-15 09:18:39 -040032#' @return R Markdown output format to pass to \code{\link{render}}
JJ Allaire4c178052016-01-30 19:35:39 -050033#'
JJ Allaire2ec40242014-09-15 09:18:39 -040034#' @details
JJ Allaire309bc7d2016-01-30 19:17:54 -050035#'
JJ Allaire4c178052016-01-30 19:35:39 -050036#' In reveal.js presentations you can use level 1 or level 2 headers for slides.
37#' If you use a mix of level 1 and level 2 headers then a two-dimensional layout
38#' will be produced, with level 1 headers building horizontally and level 2
39#' headers building vertically.
40#'
41#' For additional documentation on using revealjs presentations see
42#' \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}.
JJ Allaire4c178052016-01-30 19:35:39 -050043#'
JJ Allaire2ec40242014-09-15 09:18:39 -040044#' @examples
45#' \dontrun{
JJ Allaire4c178052016-01-30 19:35:39 -050046#'
JJ Allaire2ec40242014-09-15 09:18:39 -040047#' library(rmarkdown)
48#' library(revealjs)
JJ Allaire4c178052016-01-30 19:35:39 -050049#'
JJ Allaire2ec40242014-09-15 09:18:39 -040050#' # simple invocation
51#' render("pres.Rmd", revealjs_presentation())
JJ Allaire4c178052016-01-30 19:35:39 -050052#'
JJ Allaire2ec40242014-09-15 09:18:39 -040053#' # specify an option for incremental rendering
54#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
55#' }
JJ Allaire4c178052016-01-30 19:35:39 -050056#'
57#'
JJ Allaire2ec40242014-09-15 09:18:39 -040058#' @export
59revealjs_presentation <- function(incremental = FALSE,
60 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050061 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040062 fig_width = 8,
63 fig_height = 6,
64 fig_retina = if (!fig_caption) 2,
65 fig_caption = FALSE,
66 smart = TRUE,
67 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050068 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040069 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010070 background_transition = "default",
JJ Allaire37f45b72016-01-30 18:17:45 -050071 reveal_options = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040072 highlight = "default",
73 mathjax = "default",
74 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040075 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040076 includes = NULL,
77 keep_md = FALSE,
78 lib_dir = NULL,
79 pandoc_args = NULL,
80 ...) {
81
82 # function to lookup reveal resource
83 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -050084 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -040085 package = "revealjs")
86 }
87
88 # base pandoc options for all reveal.js output
89 args <- c()
90
91 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -050092 if (identical(template, "default")) {
93 default_template <- system.file(
94 "rmarkdown/templates/revealjs_presentation/resources/default.html",
95 package = "revealjs"
96 )
97 args <- c(args, "--template", pandoc_path_arg(default_template))
98 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -040099 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500100 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400101
102 # incremental
103 if (incremental)
104 args <- c(args, "--incremental")
105
106 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500107 jsbool <- function(value) ifelse(value, "true", "false")
108 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400109
JJ Allaire4c178052016-01-30 19:35:39 -0500110 # slide level
Michael Lerch8d845bb2016-03-29 19:50:10 -0600111 if (!is.null(slide_level))
112 args <- c(args, "--slide-level", as.character(slide_level))
JJ Allaire4c178052016-01-30 19:35:39 -0500113
JJ Allaire2ec40242014-09-15 09:18:39 -0400114 # theme
115 theme <- match.arg(theme, revealjs_themes())
116 if (identical(theme, "default"))
117 theme <- "simple"
118 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500119 theme <- "black"
120 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400121 args <- c(args, "--variable", "theme-dark")
122 args <- c(args, "--variable", paste("theme=", theme, sep=""))
123
124
125 # transition
126 transition <- match.arg(transition, revealjs_transitions())
127 args <- c(args, "--variable", paste("transition=", transition, sep=""))
128
junkkad55cff02015-03-15 22:27:03 +0100129 # background_transition
130 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500131 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
132
JJ Allaire4ae486f2016-03-23 06:08:42 -0400133 # use history
134 args <- c(args, pandoc_variable_arg("history", "true"))
JJ Allaire8bfd3642016-03-21 13:43:13 -0400135
JJ Allaire37f45b72016-01-30 18:17:45 -0500136 # additional reveal options
137 if (is.list(reveal_options)) {
138 for (option in names(reveal_options)) {
139 value <- reveal_options[[option]]
140 if (is.logical(value))
141 value <- jsbool(value)
142 args <- c(args, pandoc_variable_arg(option, value))
143 }
144 }
145
JJ Allaire2ec40242014-09-15 09:18:39 -0400146 # content includes
147 args <- c(args, includes_to_pandoc_args(includes))
148
JJ Allairefad55232015-10-19 07:47:26 -0400149 # additional css
150 for (css_file in css)
151 args <- c(args, "--css", pandoc_path_arg(css_file))
152
JJ Allaire2ec40242014-09-15 09:18:39 -0400153 # pre-processor for arguments that may depend on the name of the
154 # the input file (e.g. ones that need to copy supporting files)
155 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
156 output_dir) {
157
JJ Allairee60feb22016-01-30 18:28:47 -0500158 # we don't work with runtime shiny
159 if (identical(runtime, "shiny")) {
160 stop("revealjs_presentation is not compatible with runtime 'shiny'",
161 call. = FALSE)
162 }
163
JJ Allaire2ec40242014-09-15 09:18:39 -0400164 # use files_dir as lib_dir if not explicitly specified
165 if (is.null(lib_dir))
166 lib_dir <- files_dir
167
168 # extra args
169 args <- c()
170
171 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500172 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400173 if (!self_contained || identical(.Platform$OS.type, "windows"))
174 revealjs_path <- relative_to(
175 output_dir, render_supporting_files(revealjs_path, lib_dir))
176 args <- c(args, "--variable", paste("revealjs-url=",
177 pandoc_path_arg(revealjs_path), sep=""))
178
179 # highlight
180 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
181
182 # return additional args
183 args
184 }
185
186 # return format
187 output_format(
188 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
189 pandoc = pandoc_options(to = "revealjs",
190 from = rmarkdown_format(ifelse(fig_caption,
191 "",
192 "-implicit_figures")),
193 args = args),
194 keep_md = keep_md,
195 clean_supporting = self_contained,
196 pre_processor = pre_processor,
197 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
198 self_contained = self_contained,
199 mathjax = mathjax,
200 pandoc_args = pandoc_args, ...))
201}
202
203revealjs_themes <- function() {
204 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500205 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400206 "simple",
207 "sky",
208 "beige",
209 "serif",
210 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400211 "blood",
212 "moon",
junkka77fbf082015-03-15 22:25:47 +0100213 "night",
214 "black",
215 "league",
216 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400217}
218
219
220revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100221 c(
222 "default",
223 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400224 "fade",
junkka77fbf082015-03-15 22:25:47 +0100225 "slide",
226 "convex",
227 "concave",
228 "zoom"
229 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400230}
231
232