JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 1 | #' Convert to a reveal.js presentation |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 2 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 3 | #' Format for converting from R Markdown to a reveal.js presentation. |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 4 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 5 | #' @inheritParams rmarkdown::beamer_presentation |
| 6 | #' @inheritParams rmarkdown::pdf_document |
| 7 | #' @inheritParams rmarkdown::html_document |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 8 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 9 | #' @param center \code{TRUE} to vertically center content on slides |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 10 | #' @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"). |
junkka | d4b3a16 | 2015-03-16 07:49:11 +0100 | [diff] [blame] | 17 | #' @param transition Slide transition ("default", "none", "fade", "slide", |
| 18 | #' "convex", "concave" or "zoom") |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 19 | #' @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). |
JJ Allaire | 82a8dee | 2016-07-12 10:25:36 -0400 | [diff] [blame] | 24 | #' @param reveal_plugins Reveal plugins to include. Available plugins include "notes", |
| 25 | #' "search", and "zoom". Note that \code{self_contained} must be set to |
| 26 | #' \code{FALSE} in order to use Reveal plugins. |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 27 | #' @param template Pandoc template to use for rendering. Pass "default" to use |
| 28 | #' the rmarkdown package default template; pass \code{NULL} to use pandoc's |
| 29 | #' built-in template; pass a path to use a custom template that you've |
| 30 | #' created. Note that if you don't use the "default" template then some |
| 31 | #' features of \code{revealjs_presentation} won't be available (see the |
| 32 | #' Templates section below for more details). |
JJ Allaire | 8d1c2f4 | 2016-01-30 14:56:45 -0500 | [diff] [blame] | 33 | #' @param ... Ignored |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 34 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 35 | #' @return R Markdown output format to pass to \code{\link{render}} |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 36 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 37 | #' @details |
JJ Allaire | 309bc7d | 2016-01-30 19:17:54 -0500 | [diff] [blame] | 38 | #' |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 39 | #' In reveal.js presentations you can use level 1 or level 2 headers for slides. |
| 40 | #' If you use a mix of level 1 and level 2 headers then a two-dimensional layout |
| 41 | #' will be produced, with level 1 headers building horizontally and level 2 |
| 42 | #' headers building vertically. |
| 43 | #' |
| 44 | #' For additional documentation on using revealjs presentations see |
| 45 | #' \href{https://github.com/rstudio/revealjs}{https://github.com/rstudio/revealjs}. |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 46 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 47 | #' @examples |
| 48 | #' \dontrun{ |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 49 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 50 | #' library(rmarkdown) |
| 51 | #' library(revealjs) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 52 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 53 | #' # simple invocation |
| 54 | #' render("pres.Rmd", revealjs_presentation()) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 55 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 56 | #' # specify an option for incremental rendering |
| 57 | #' render("pres.Rmd", revealjs_presentation(incremental = TRUE)) |
| 58 | #' } |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 59 | #' |
| 60 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 61 | #' @export |
| 62 | revealjs_presentation <- function(incremental = FALSE, |
| 63 | center = FALSE, |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 64 | slide_level = 2, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 65 | fig_width = 8, |
| 66 | fig_height = 6, |
| 67 | fig_retina = if (!fig_caption) 2, |
| 68 | fig_caption = FALSE, |
| 69 | smart = TRUE, |
| 70 | self_contained = TRUE, |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 71 | theme = "simple", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 72 | transition = "default", |
junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 73 | background_transition = "default", |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 74 | reveal_options = NULL, |
JJ Allaire | 82a8dee | 2016-07-12 10:25:36 -0400 | [diff] [blame] | 75 | reveal_plugins = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 76 | highlight = "default", |
| 77 | mathjax = "default", |
| 78 | template = "default", |
JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 79 | css = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 80 | includes = NULL, |
| 81 | keep_md = FALSE, |
| 82 | lib_dir = NULL, |
| 83 | pandoc_args = NULL, |
JJ Allaire | 375805c | 2016-11-15 08:56:43 -0500 | [diff] [blame] | 84 | extra_dependencies = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 85 | ...) { |
| 86 | |
| 87 | # function to lookup reveal resource |
| 88 | reveal_resources <- function() { |
JJ Allaire | 2d8f3f2 | 2016-01-30 13:08:52 -0500 | [diff] [blame] | 89 | system.file("rmarkdown/templates/revealjs_presentation/resources", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 90 | package = "revealjs") |
| 91 | } |
| 92 | |
| 93 | # base pandoc options for all reveal.js output |
| 94 | args <- c() |
| 95 | |
| 96 | # template path and assets |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 97 | if (identical(template, "default")) { |
| 98 | default_template <- system.file( |
| 99 | "rmarkdown/templates/revealjs_presentation/resources/default.html", |
| 100 | package = "revealjs" |
| 101 | ) |
| 102 | args <- c(args, "--template", pandoc_path_arg(default_template)) |
| 103 | } else if (!is.null(template)) { |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 104 | args <- c(args, "--template", pandoc_path_arg(template)) |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 105 | } |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 106 | |
| 107 | # incremental |
| 108 | if (incremental) |
| 109 | args <- c(args, "--incremental") |
| 110 | |
| 111 | # centering |
JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 112 | jsbool <- function(value) ifelse(value, "true", "false") |
| 113 | args <- c(args, pandoc_variable_arg("center", jsbool(center))) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 114 | |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 115 | # slide level |
JJ Allaire | c58a6c4 | 2016-03-30 23:45:21 -0400 | [diff] [blame] | 116 | args <- c(args, "--slide-level", as.character(slide_level)) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 117 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 118 | # theme |
| 119 | theme <- match.arg(theme, revealjs_themes()) |
| 120 | if (identical(theme, "default")) |
| 121 | theme <- "simple" |
| 122 | else if (identical(theme, "dark")) |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 123 | theme <- "black" |
| 124 | if (theme %in% c("blood", "moon", "night", "black")) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 125 | args <- c(args, "--variable", "theme-dark") |
| 126 | args <- c(args, "--variable", paste("theme=", theme, sep="")) |
| 127 | |
| 128 | |
| 129 | # transition |
| 130 | transition <- match.arg(transition, revealjs_transitions()) |
| 131 | args <- c(args, "--variable", paste("transition=", transition, sep="")) |
| 132 | |
junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 133 | # background_transition |
| 134 | background_transition <- match.arg(background_transition, revealjs_transitions()) |
JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 135 | args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep="")) |
| 136 | |
JJ Allaire | 4ae486f | 2016-03-23 06:08:42 -0400 | [diff] [blame] | 137 | # use history |
| 138 | args <- c(args, pandoc_variable_arg("history", "true")) |
JJ Allaire | 8bfd364 | 2016-03-21 13:43:13 -0400 | [diff] [blame] | 139 | |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 140 | # additional reveal options |
| 141 | if (is.list(reveal_options)) { |
JJ Allaire | 375805c | 2016-11-15 08:56:43 -0500 | [diff] [blame] | 142 | |
| 143 | add_reveal_option <- function(option, value) { |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 144 | if (is.logical(value)) |
| 145 | value <- jsbool(value) |
JJ Allaire | a075efe | 2016-07-14 21:24:12 -0400 | [diff] [blame] | 146 | else if (is.character(value)) |
| 147 | value <- paste0("'", value, "'") |
JJ Allaire | 375805c | 2016-11-15 08:56:43 -0500 | [diff] [blame] | 148 | args <<- c(args, pandoc_variable_arg(option, value)) |
| 149 | } |
| 150 | |
| 151 | for (option in names(reveal_options)) { |
| 152 | # special handling for nested chalkboard options |
| 153 | if (identical(option, "chalkboard")) { |
| 154 | chalkboard_options <- reveal_options[[option]] |
| 155 | for (chalkboard_option in names(chalkboard_options)) { |
| 156 | add_reveal_option(paste0("chalkboard-", chalkboard_option), |
| 157 | chalkboard_options[[chalkboard_option]]) |
| 158 | } |
| 159 | } |
| 160 | # standard top-level options |
| 161 | else { |
| 162 | add_reveal_option(option, reveal_options[[option]]) |
| 163 | } |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
JJ Allaire | 82a8dee | 2016-07-12 10:25:36 -0400 | [diff] [blame] | 167 | # reveal plugins |
| 168 | if (is.character(reveal_plugins)) { |
| 169 | |
| 170 | # validate that we need to use self_contained for plugins |
| 171 | if (self_contained) |
| 172 | stop("Using reveal_plugins requires self_contained: false") |
| 173 | |
| 174 | # validate specified plugins are supported |
JJ Allaire | 375805c | 2016-11-15 08:56:43 -0500 | [diff] [blame] | 175 | supported_plugins <- c("notes", "search", "zoom", "chalkboard") |
JJ Allaire | 82a8dee | 2016-07-12 10:25:36 -0400 | [diff] [blame] | 176 | invalid_plugins <- setdiff(reveal_plugins, supported_plugins) |
| 177 | if (length(invalid_plugins) > 0) |
| 178 | stop("The following plugin(s) are not supported: ", |
| 179 | paste(invalid_plugins, collapse = ", "), call. = FALSE) |
| 180 | |
| 181 | # add plugins |
| 182 | sapply(reveal_plugins, function(plugin) { |
| 183 | args <<- c(args, pandoc_variable_arg(paste0("plugin-", plugin), "1")) |
JJ Allaire | 375805c | 2016-11-15 08:56:43 -0500 | [diff] [blame] | 184 | if (identical(plugin, "chalkboard")) { |
| 185 | extra_dependencies <<- append(extra_dependencies, |
| 186 | list(rmarkdown::html_dependency_font_awesome())) |
| 187 | |
| 188 | } |
JJ Allaire | 82a8dee | 2016-07-12 10:25:36 -0400 | [diff] [blame] | 189 | }) |
| 190 | } |
| 191 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 192 | # content includes |
| 193 | args <- c(args, includes_to_pandoc_args(includes)) |
| 194 | |
JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 195 | # additional css |
| 196 | for (css_file in css) |
| 197 | args <- c(args, "--css", pandoc_path_arg(css_file)) |
| 198 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 199 | # pre-processor for arguments that may depend on the name of the |
| 200 | # the input file (e.g. ones that need to copy supporting files) |
| 201 | pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, |
| 202 | output_dir) { |
| 203 | |
JJ Allaire | e60feb2 | 2016-01-30 18:28:47 -0500 | [diff] [blame] | 204 | # we don't work with runtime shiny |
| 205 | if (identical(runtime, "shiny")) { |
| 206 | stop("revealjs_presentation is not compatible with runtime 'shiny'", |
| 207 | call. = FALSE) |
| 208 | } |
| 209 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 210 | # use files_dir as lib_dir if not explicitly specified |
| 211 | if (is.null(lib_dir)) |
| 212 | lib_dir <- files_dir |
| 213 | |
| 214 | # extra args |
| 215 | args <- c() |
| 216 | |
| 217 | # reveal.js |
Bruce's Thinkpad | 8b73dcf | 2016-07-14 00:12:43 +0800 | [diff] [blame] | 218 | revealjs_path <- system.file("reveal.js-3.3.0", package = "revealjs") |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 219 | if (!self_contained || identical(.Platform$OS.type, "windows")) |
| 220 | revealjs_path <- relative_to( |
| 221 | output_dir, render_supporting_files(revealjs_path, lib_dir)) |
JJ Allaire | eea3bca | 2016-07-13 12:42:01 -0400 | [diff] [blame] | 222 | else |
| 223 | revealjs_path <- pandoc_path_arg(revealjs_path) |
BruceZhao | 30f9bc0 | 2016-07-13 22:33:54 +0800 | [diff] [blame] | 224 | args <- c(args, "--variable", paste0("revealjs-url=", revealjs_path)) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 225 | |
| 226 | # highlight |
| 227 | args <- c(args, pandoc_highlight_args(highlight, default = "pygments")) |
| 228 | |
| 229 | # return additional args |
| 230 | args |
| 231 | } |
| 232 | |
| 233 | # return format |
| 234 | output_format( |
| 235 | knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md), |
| 236 | pandoc = pandoc_options(to = "revealjs", |
| 237 | from = rmarkdown_format(ifelse(fig_caption, |
| 238 | "", |
| 239 | "-implicit_figures")), |
| 240 | args = args), |
| 241 | keep_md = keep_md, |
| 242 | clean_supporting = self_contained, |
| 243 | pre_processor = pre_processor, |
| 244 | base_format = html_document_base(smart = smart, lib_dir = lib_dir, |
| 245 | self_contained = self_contained, |
| 246 | mathjax = mathjax, |
JJ Allaire | 375805c | 2016-11-15 08:56:43 -0500 | [diff] [blame] | 247 | pandoc_args = pandoc_args, |
| 248 | extra_dependencies = extra_dependencies, |
| 249 | ...)) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | revealjs_themes <- function() { |
| 253 | c("default", |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 254 | "dark", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 255 | "simple", |
| 256 | "sky", |
| 257 | "beige", |
| 258 | "serif", |
| 259 | "solarized", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 260 | "blood", |
| 261 | "moon", |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 262 | "night", |
| 263 | "black", |
| 264 | "league", |
| 265 | "white") |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | revealjs_transitions <- function() { |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 270 | c( |
| 271 | "default", |
| 272 | "none", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 273 | "fade", |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 274 | "slide", |
| 275 | "convex", |
| 276 | "concave", |
| 277 | "zoom" |
| 278 | ) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |