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