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