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", |
| 83 | highlight = "default", |
| 84 | mathjax = "default", |
| 85 | template = "default", |
| 86 | includes = NULL, |
| 87 | keep_md = FALSE, |
| 88 | lib_dir = NULL, |
| 89 | pandoc_args = NULL, |
| 90 | ...) { |
| 91 | |
| 92 | # function to lookup reveal resource |
| 93 | reveal_resources <- function() { |
| 94 | system.file("rmarkdown/templates/revealjs_presentation", |
| 95 | package = "revealjs") |
| 96 | } |
| 97 | |
| 98 | # base pandoc options for all reveal.js output |
| 99 | args <- c() |
| 100 | |
| 101 | # template path and assets |
| 102 | if (identical(template, "default")) |
| 103 | args <- c(args, "--template", |
| 104 | pandoc_path_arg(file.path(reveal_resources(), "default.html"))) |
| 105 | else if (!is.null(template)) |
| 106 | args <- c(args, "--template", pandoc_path_arg(template)) |
| 107 | |
| 108 | # incremental |
| 109 | if (incremental) |
| 110 | args <- c(args, "--incremental") |
| 111 | |
| 112 | # centering |
| 113 | if (center) |
| 114 | args <- c(args, "--variable", "center") |
| 115 | |
| 116 | # theme |
| 117 | theme <- match.arg(theme, revealjs_themes()) |
| 118 | if (identical(theme, "default")) |
| 119 | theme <- "simple" |
| 120 | else if (identical(theme, "dark")) |
| 121 | theme <- "default" |
| 122 | if (theme %in% c("default", "blood", "moon", "night")) |
| 123 | args <- c(args, "--variable", "theme-dark") |
| 124 | args <- c(args, "--variable", paste("theme=", theme, sep="")) |
| 125 | |
| 126 | |
| 127 | # transition |
| 128 | transition <- match.arg(transition, revealjs_transitions()) |
| 129 | args <- c(args, "--variable", paste("transition=", transition, sep="")) |
| 130 | |
| 131 | # content includes |
| 132 | args <- c(args, includes_to_pandoc_args(includes)) |
| 133 | |
| 134 | # pre-processor for arguments that may depend on the name of the |
| 135 | # the input file (e.g. ones that need to copy supporting files) |
| 136 | pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, |
| 137 | output_dir) { |
| 138 | |
| 139 | # use files_dir as lib_dir if not explicitly specified |
| 140 | if (is.null(lib_dir)) |
| 141 | lib_dir <- files_dir |
| 142 | |
| 143 | # extra args |
| 144 | args <- c() |
| 145 | |
| 146 | # reveal.js |
| 147 | revealjs_path <- reveal_resources() |
| 148 | if (!self_contained || identical(.Platform$OS.type, "windows")) |
| 149 | revealjs_path <- relative_to( |
| 150 | output_dir, render_supporting_files(revealjs_path, lib_dir)) |
| 151 | args <- c(args, "--variable", paste("revealjs-url=", |
| 152 | pandoc_path_arg(revealjs_path), sep="")) |
| 153 | |
| 154 | # highlight |
| 155 | args <- c(args, pandoc_highlight_args(highlight, default = "pygments")) |
| 156 | |
| 157 | # return additional args |
| 158 | args |
| 159 | } |
| 160 | |
| 161 | # return format |
| 162 | output_format( |
| 163 | knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md), |
| 164 | pandoc = pandoc_options(to = "revealjs", |
| 165 | from = rmarkdown_format(ifelse(fig_caption, |
| 166 | "", |
| 167 | "-implicit_figures")), |
| 168 | args = args), |
| 169 | keep_md = keep_md, |
| 170 | clean_supporting = self_contained, |
| 171 | pre_processor = pre_processor, |
| 172 | base_format = html_document_base(smart = smart, lib_dir = lib_dir, |
| 173 | self_contained = self_contained, |
| 174 | mathjax = mathjax, |
| 175 | pandoc_args = pandoc_args, ...)) |
| 176 | } |
| 177 | |
| 178 | revealjs_themes <- function() { |
| 179 | c("default", |
| 180 | "simple", |
| 181 | "sky", |
| 182 | "beige", |
| 183 | "serif", |
| 184 | "solarized", |
| 185 | "dark", |
| 186 | "blood", |
| 187 | "moon", |
| 188 | "night") |
| 189 | } |
| 190 | |
| 191 | |
| 192 | revealjs_transitions <- function() { |
| 193 | c("default", |
| 194 | "cube", |
| 195 | "page", |
| 196 | "concave", |
| 197 | "zoom", |
| 198 | "linear", |
| 199 | "fade", |
| 200 | "none") |
| 201 | } |
| 202 | |
| 203 | |