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