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") |
JJ Allaire | 8bfd364 | 2016-03-21 13:43:13 -0400 | [diff] [blame^] | 21 | #' @param history \code{TRUE} to push each slide change to the browser history. |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 22 | #' @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 Allaire | 8d1c2f4 | 2016-01-30 14:56:45 -0500 | [diff] [blame] | 31 | #' @param ... Ignored |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 32 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 33 | #' @return R Markdown output format to pass to \code{\link{render}} |
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 | #' @details |
JJ Allaire | 309bc7d | 2016-01-30 19:17:54 -0500 | [diff] [blame] | 36 | #' |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 37 | #' 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 Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 44 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 45 | #' @examples |
| 46 | #' \dontrun{ |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 47 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 48 | #' library(rmarkdown) |
| 49 | #' library(revealjs) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 50 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 51 | #' # simple invocation |
| 52 | #' render("pres.Rmd", revealjs_presentation()) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 53 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 54 | #' # specify an option for incremental rendering |
| 55 | #' render("pres.Rmd", revealjs_presentation(incremental = TRUE)) |
| 56 | #' } |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 57 | #' |
| 58 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 59 | #' @export |
| 60 | revealjs_presentation <- function(incremental = FALSE, |
| 61 | center = FALSE, |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 62 | slide_level = 2, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 63 | 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 Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 69 | theme = "simple", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 70 | transition = "default", |
junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 71 | background_transition = "default", |
JJ Allaire | 8bfd364 | 2016-03-21 13:43:13 -0400 | [diff] [blame^] | 72 | history = TRUE, |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 73 | reveal_options = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 74 | highlight = "default", |
| 75 | mathjax = "default", |
| 76 | template = "default", |
JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 77 | css = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 78 | 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 Allaire | 2d8f3f2 | 2016-01-30 13:08:52 -0500 | [diff] [blame] | 86 | system.file("rmarkdown/templates/revealjs_presentation/resources", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 87 | package = "revealjs") |
| 88 | } |
| 89 | |
| 90 | # base pandoc options for all reveal.js output |
| 91 | args <- c() |
| 92 | |
| 93 | # template path and assets |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 94 | 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 Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 101 | args <- c(args, "--template", pandoc_path_arg(template)) |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 102 | } |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 103 | |
| 104 | # incremental |
| 105 | if (incremental) |
| 106 | args <- c(args, "--incremental") |
| 107 | |
| 108 | # centering |
JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 109 | jsbool <- function(value) ifelse(value, "true", "false") |
| 110 | args <- c(args, pandoc_variable_arg("center", jsbool(center))) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 111 | |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame] | 112 | # slide level |
| 113 | args <- c(args, "--slide-level", "2") |
| 114 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 115 | # theme |
| 116 | theme <- match.arg(theme, revealjs_themes()) |
| 117 | if (identical(theme, "default")) |
| 118 | theme <- "simple" |
| 119 | else if (identical(theme, "dark")) |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 120 | theme <- "black" |
| 121 | if (theme %in% c("blood", "moon", "night", "black")) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 122 | 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 | |
junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 130 | # background_transition |
| 131 | background_transition <- match.arg(background_transition, revealjs_transitions()) |
JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 132 | args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep="")) |
| 133 | |
JJ Allaire | 8bfd364 | 2016-03-21 13:43:13 -0400 | [diff] [blame^] | 134 | # history |
| 135 | args <- c(args, pandoc_variable_arg("history", jsbool(history))) |
| 136 | |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 137 | # 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 Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 147 | # content includes |
| 148 | args <- c(args, includes_to_pandoc_args(includes)) |
| 149 | |
JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 150 | # additional css |
| 151 | for (css_file in css) |
| 152 | args <- c(args, "--css", pandoc_path_arg(css_file)) |
| 153 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 154 | # 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 Allaire | e60feb2 | 2016-01-30 18:28:47 -0500 | [diff] [blame] | 159 | # 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 Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 165 | # 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 Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 173 | revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs") |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 174 | 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 | |
| 204 | revealjs_themes <- function() { |
| 205 | c("default", |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 206 | "dark", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 207 | "simple", |
| 208 | "sky", |
| 209 | "beige", |
| 210 | "serif", |
| 211 | "solarized", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 212 | "blood", |
| 213 | "moon", |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 214 | "night", |
| 215 | "black", |
| 216 | "league", |
| 217 | "white") |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | revealjs_transitions <- function() { |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 222 | c( |
| 223 | "default", |
| 224 | "none", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 225 | "fade", |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 226 | "slide", |
| 227 | "convex", |
| 228 | "concave", |
| 229 | "zoom" |
| 230 | ) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |