blob: 5e507559de506b547826c5d067402ae53edb50c2 [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")
JJ Allaire8bfd3642016-03-21 13:43:13 -040021#' @param history \code{TRUE} to push each slide change to the browser history.
JJ Allaire4c178052016-01-30 19:35:39 -050022#' @param reveal_options Additional options to specify for reveal.js (see
23#' \href{https://github.com/hakimel/reveal.js#configuration}{https://github.com/hakimel/reveal.js#configuration}
24#' for details).
25#' @param template Pandoc template to use for rendering. Pass "default" to use
26#' the rmarkdown package default template; pass \code{NULL} to use pandoc's
27#' built-in template; pass a path to use a custom template that you've
28#' created. Note that if you don't use the "default" template then some
29#' features of \code{revealjs_presentation} won't be available (see the
30#' Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050031#' @param ... Ignored
JJ Allaire4c178052016-01-30 19:35:39 -050032#'
JJ Allaire2ec40242014-09-15 09:18:39 -040033#' @return R Markdown output format to pass to \code{\link{render}}
JJ Allaire4c178052016-01-30 19:35:39 -050034#'
JJ Allaire2ec40242014-09-15 09:18:39 -040035#' @details
JJ Allaire309bc7d2016-01-30 19:17:54 -050036#'
JJ Allaire4c178052016-01-30 19:35:39 -050037#' In reveal.js presentations you can use level 1 or level 2 headers for slides.
38#' If you use a mix of level 1 and level 2 headers then a two-dimensional layout
39#' will be produced, with level 1 headers building horizontally and level 2
40#' headers building vertically.
41#'
42#' For additional documentation on using revealjs presentations see
43#' \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}.
JJ Allaire4c178052016-01-30 19:35:39 -050044#'
JJ Allaire2ec40242014-09-15 09:18:39 -040045#' @examples
46#' \dontrun{
JJ Allaire4c178052016-01-30 19:35:39 -050047#'
JJ Allaire2ec40242014-09-15 09:18:39 -040048#' library(rmarkdown)
49#' library(revealjs)
JJ Allaire4c178052016-01-30 19:35:39 -050050#'
JJ Allaire2ec40242014-09-15 09:18:39 -040051#' # simple invocation
52#' render("pres.Rmd", revealjs_presentation())
JJ Allaire4c178052016-01-30 19:35:39 -050053#'
JJ Allaire2ec40242014-09-15 09:18:39 -040054#' # specify an option for incremental rendering
55#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
56#' }
JJ Allaire4c178052016-01-30 19:35:39 -050057#'
58#'
JJ Allaire2ec40242014-09-15 09:18:39 -040059#' @export
60revealjs_presentation <- function(incremental = FALSE,
61 center = FALSE,
JJ Allaire4c178052016-01-30 19:35:39 -050062 slide_level = 2,
JJ Allaire2ec40242014-09-15 09:18:39 -040063 fig_width = 8,
64 fig_height = 6,
65 fig_retina = if (!fig_caption) 2,
66 fig_caption = FALSE,
67 smart = TRUE,
68 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050069 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040070 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010071 background_transition = "default",
JJ Allaire8bfd3642016-03-21 13:43:13 -040072 history = TRUE,
JJ Allaire37f45b72016-01-30 18:17:45 -050073 reveal_options = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040074 highlight = "default",
75 mathjax = "default",
76 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040077 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040078 includes = NULL,
79 keep_md = FALSE,
80 lib_dir = NULL,
81 pandoc_args = NULL,
82 ...) {
83
84 # function to lookup reveal resource
85 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -050086 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -040087 package = "revealjs")
88 }
89
90 # base pandoc options for all reveal.js output
91 args <- c()
92
93 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -050094 if (identical(template, "default")) {
95 default_template <- system.file(
96 "rmarkdown/templates/revealjs_presentation/resources/default.html",
97 package = "revealjs"
98 )
99 args <- c(args, "--template", pandoc_path_arg(default_template))
100 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400101 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500102 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400103
104 # incremental
105 if (incremental)
106 args <- c(args, "--incremental")
107
108 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500109 jsbool <- function(value) ifelse(value, "true", "false")
110 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400111
JJ Allaire4c178052016-01-30 19:35:39 -0500112 # slide level
113 args <- c(args, "--slide-level", "2")
114
JJ Allaire2ec40242014-09-15 09:18:39 -0400115 # theme
116 theme <- match.arg(theme, revealjs_themes())
117 if (identical(theme, "default"))
118 theme <- "simple"
119 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500120 theme <- "black"
121 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400122 args <- c(args, "--variable", "theme-dark")
123 args <- c(args, "--variable", paste("theme=", theme, sep=""))
124
125
126 # transition
127 transition <- match.arg(transition, revealjs_transitions())
128 args <- c(args, "--variable", paste("transition=", transition, sep=""))
129
junkkad55cff02015-03-15 22:27:03 +0100130 # background_transition
131 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500132 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
133
JJ Allaire8bfd3642016-03-21 13:43:13 -0400134 # history
135 args <- c(args, pandoc_variable_arg("history", jsbool(history)))
136
JJ Allaire37f45b72016-01-30 18:17:45 -0500137 # additional reveal options
138 if (is.list(reveal_options)) {
139 for (option in names(reveal_options)) {
140 value <- reveal_options[[option]]
141 if (is.logical(value))
142 value <- jsbool(value)
143 args <- c(args, pandoc_variable_arg(option, value))
144 }
145 }
146
JJ Allaire2ec40242014-09-15 09:18:39 -0400147 # content includes
148 args <- c(args, includes_to_pandoc_args(includes))
149
JJ Allairefad55232015-10-19 07:47:26 -0400150 # additional css
151 for (css_file in css)
152 args <- c(args, "--css", pandoc_path_arg(css_file))
153
JJ Allaire2ec40242014-09-15 09:18:39 -0400154 # pre-processor for arguments that may depend on the name of the
155 # the input file (e.g. ones that need to copy supporting files)
156 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
157 output_dir) {
158
JJ Allairee60feb22016-01-30 18:28:47 -0500159 # we don't work with runtime shiny
160 if (identical(runtime, "shiny")) {
161 stop("revealjs_presentation is not compatible with runtime 'shiny'",
162 call. = FALSE)
163 }
164
JJ Allaire2ec40242014-09-15 09:18:39 -0400165 # use files_dir as lib_dir if not explicitly specified
166 if (is.null(lib_dir))
167 lib_dir <- files_dir
168
169 # extra args
170 args <- c()
171
172 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500173 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400174 if (!self_contained || identical(.Platform$OS.type, "windows"))
175 revealjs_path <- relative_to(
176 output_dir, render_supporting_files(revealjs_path, lib_dir))
177 args <- c(args, "--variable", paste("revealjs-url=",
178 pandoc_path_arg(revealjs_path), sep=""))
179
180 # highlight
181 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
182
183 # return additional args
184 args
185 }
186
187 # return format
188 output_format(
189 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
190 pandoc = pandoc_options(to = "revealjs",
191 from = rmarkdown_format(ifelse(fig_caption,
192 "",
193 "-implicit_figures")),
194 args = args),
195 keep_md = keep_md,
196 clean_supporting = self_contained,
197 pre_processor = pre_processor,
198 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
199 self_contained = self_contained,
200 mathjax = mathjax,
201 pandoc_args = pandoc_args, ...))
202}
203
204revealjs_themes <- function() {
205 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500206 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400207 "simple",
208 "sky",
209 "beige",
210 "serif",
211 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400212 "blood",
213 "moon",
junkka77fbf082015-03-15 22:25:47 +0100214 "night",
215 "black",
216 "league",
217 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400218}
219
220
221revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100222 c(
223 "default",
224 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400225 "fade",
junkka77fbf082015-03-15 22:25:47 +0100226 "slide",
227 "convex",
228 "concave",
229 "zoom"
230 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400231}
232
233