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