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). |
| 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 Allaire | 8d1c2f4 | 2016-01-30 14:56:45 -0500 | [diff] [blame] | 30 | #' @param ... Ignored |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 31 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 32 | #' @return R Markdown output format to pass to \code{\link{render}} |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 33 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 34 | #' @details |
JJ Allaire | 309bc7d | 2016-01-30 19:17:54 -0500 | [diff] [blame] | 35 | #' |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 36 | #' 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 Allaire | 309bc7d | 2016-01-30 19:17:54 -0500 | [diff] [blame] | 43 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 44 | #' @section Templates: |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 45 | #' |
| 46 | #' You can provide a custom HTML template to be used for rendering. The syntax |
| 47 | #' for templates is described in the documentation on |
| 48 | #' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc |
| 49 | #' templates}. You can also use the basic pandoc template by passing |
| 50 | #' \code{template = NULL}. |
| 51 | #' |
| 52 | #' Note however that if you choose not to use the "default" reveal.js template |
| 53 | #' then several aspects of reveal.js presentation rendering will behave |
| 54 | #' differently: |
| 55 | #' |
| 56 | #' \itemize{ \item{The \code{center} parameter does not work (you'd need to |
| 57 | #' set this directly in the template). } \item{The built-in template includes |
| 58 | #' some additional tweaks to styles to optimize for output from R, these won't |
| 59 | #' be present. } \item{MathJax will not work if \code{self_contained} is |
| 60 | #' \code{TRUE} (these two options can't be used together in normal pandoc |
| 61 | #' templates). } } |
| 62 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 63 | #' @examples |
| 64 | #' \dontrun{ |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 65 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 66 | #' library(rmarkdown) |
| 67 | #' library(revealjs) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 68 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 69 | #' # simple invocation |
| 70 | #' render("pres.Rmd", revealjs_presentation()) |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 71 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 72 | #' # specify an option for incremental rendering |
| 73 | #' render("pres.Rmd", revealjs_presentation(incremental = TRUE)) |
| 74 | #' } |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 75 | #' |
| 76 | #' |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 77 | #' @export |
| 78 | revealjs_presentation <- function(incremental = FALSE, |
| 79 | center = FALSE, |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 80 | slide_level = 2, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 81 | fig_width = 8, |
| 82 | fig_height = 6, |
| 83 | fig_retina = if (!fig_caption) 2, |
| 84 | fig_caption = FALSE, |
| 85 | smart = TRUE, |
| 86 | self_contained = TRUE, |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 87 | theme = "simple", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 88 | transition = "default", |
junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 89 | background_transition = "default", |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 90 | reveal_options = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 91 | highlight = "default", |
| 92 | mathjax = "default", |
| 93 | template = "default", |
JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 94 | css = NULL, |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 95 | includes = NULL, |
| 96 | keep_md = FALSE, |
| 97 | lib_dir = NULL, |
| 98 | pandoc_args = NULL, |
| 99 | ...) { |
| 100 | |
| 101 | # function to lookup reveal resource |
| 102 | reveal_resources <- function() { |
JJ Allaire | 2d8f3f2 | 2016-01-30 13:08:52 -0500 | [diff] [blame] | 103 | system.file("rmarkdown/templates/revealjs_presentation/resources", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 104 | package = "revealjs") |
| 105 | } |
| 106 | |
| 107 | # base pandoc options for all reveal.js output |
| 108 | args <- c() |
| 109 | |
| 110 | # template path and assets |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 111 | if (identical(template, "default")) { |
| 112 | default_template <- system.file( |
| 113 | "rmarkdown/templates/revealjs_presentation/resources/default.html", |
| 114 | package = "revealjs" |
| 115 | ) |
| 116 | args <- c(args, "--template", pandoc_path_arg(default_template)) |
| 117 | } else if (!is.null(template)) { |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 118 | args <- c(args, "--template", pandoc_path_arg(template)) |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 119 | } |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 120 | |
| 121 | # incremental |
| 122 | if (incremental) |
| 123 | args <- c(args, "--incremental") |
| 124 | |
| 125 | # centering |
JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 126 | jsbool <- function(value) ifelse(value, "true", "false") |
| 127 | args <- c(args, pandoc_variable_arg("center", jsbool(center))) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 128 | |
JJ Allaire | 4c17805 | 2016-01-30 19:35:39 -0500 | [diff] [blame^] | 129 | # slide level |
| 130 | args <- c(args, "--slide-level", "2") |
| 131 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 132 | # theme |
| 133 | theme <- match.arg(theme, revealjs_themes()) |
| 134 | if (identical(theme, "default")) |
| 135 | theme <- "simple" |
| 136 | else if (identical(theme, "dark")) |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 137 | theme <- "black" |
| 138 | if (theme %in% c("blood", "moon", "night", "black")) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 139 | args <- c(args, "--variable", "theme-dark") |
| 140 | args <- c(args, "--variable", paste("theme=", theme, sep="")) |
| 141 | |
| 142 | |
| 143 | # transition |
| 144 | transition <- match.arg(transition, revealjs_transitions()) |
| 145 | args <- c(args, "--variable", paste("transition=", transition, sep="")) |
| 146 | |
junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 147 | # background_transition |
| 148 | background_transition <- match.arg(background_transition, revealjs_transitions()) |
JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 149 | args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep="")) |
| 150 | |
JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame] | 151 | # additional reveal options |
| 152 | if (is.list(reveal_options)) { |
| 153 | for (option in names(reveal_options)) { |
| 154 | value <- reveal_options[[option]] |
| 155 | if (is.logical(value)) |
| 156 | value <- jsbool(value) |
| 157 | args <- c(args, pandoc_variable_arg(option, value)) |
| 158 | } |
| 159 | } |
| 160 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 161 | # content includes |
| 162 | args <- c(args, includes_to_pandoc_args(includes)) |
| 163 | |
JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 164 | # additional css |
| 165 | for (css_file in css) |
| 166 | args <- c(args, "--css", pandoc_path_arg(css_file)) |
| 167 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 168 | # pre-processor for arguments that may depend on the name of the |
| 169 | # the input file (e.g. ones that need to copy supporting files) |
| 170 | pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, |
| 171 | output_dir) { |
| 172 | |
JJ Allaire | e60feb2 | 2016-01-30 18:28:47 -0500 | [diff] [blame] | 173 | # we don't work with runtime shiny |
| 174 | if (identical(runtime, "shiny")) { |
| 175 | stop("revealjs_presentation is not compatible with runtime 'shiny'", |
| 176 | call. = FALSE) |
| 177 | } |
| 178 | |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 179 | # use files_dir as lib_dir if not explicitly specified |
| 180 | if (is.null(lib_dir)) |
| 181 | lib_dir <- files_dir |
| 182 | |
| 183 | # extra args |
| 184 | args <- c() |
| 185 | |
| 186 | # reveal.js |
JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 187 | revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs") |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 188 | if (!self_contained || identical(.Platform$OS.type, "windows")) |
| 189 | revealjs_path <- relative_to( |
| 190 | output_dir, render_supporting_files(revealjs_path, lib_dir)) |
| 191 | args <- c(args, "--variable", paste("revealjs-url=", |
| 192 | pandoc_path_arg(revealjs_path), sep="")) |
| 193 | |
| 194 | # highlight |
| 195 | args <- c(args, pandoc_highlight_args(highlight, default = "pygments")) |
| 196 | |
| 197 | # return additional args |
| 198 | args |
| 199 | } |
| 200 | |
| 201 | # return format |
| 202 | output_format( |
| 203 | knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md), |
| 204 | pandoc = pandoc_options(to = "revealjs", |
| 205 | from = rmarkdown_format(ifelse(fig_caption, |
| 206 | "", |
| 207 | "-implicit_figures")), |
| 208 | args = args), |
| 209 | keep_md = keep_md, |
| 210 | clean_supporting = self_contained, |
| 211 | pre_processor = pre_processor, |
| 212 | base_format = html_document_base(smart = smart, lib_dir = lib_dir, |
| 213 | self_contained = self_contained, |
| 214 | mathjax = mathjax, |
| 215 | pandoc_args = pandoc_args, ...)) |
| 216 | } |
| 217 | |
| 218 | revealjs_themes <- function() { |
| 219 | c("default", |
JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 220 | "dark", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 221 | "simple", |
| 222 | "sky", |
| 223 | "beige", |
| 224 | "serif", |
| 225 | "solarized", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 226 | "blood", |
| 227 | "moon", |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 228 | "night", |
| 229 | "black", |
| 230 | "league", |
| 231 | "white") |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
| 235 | revealjs_transitions <- function() { |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 236 | c( |
| 237 | "default", |
| 238 | "none", |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 239 | "fade", |
junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 240 | "slide", |
| 241 | "convex", |
| 242 | "concave", |
| 243 | "zoom" |
| 244 | ) |
JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |