Debian Codegraph @

R/powerpoint_presentation.R

raw
1 #' Convert to a PowerPoint presentation
2 #'
3 #' Format for converting from R Markdown to a PowerPoint presentation. Pandoc
4 #' v2.0.5 or above is required.
5 #' @inheritParams pdf_document
6 #' @inheritParams html_document
7 #' @inheritParams beamer_presentation
8 #' @param reference_doc Path to a PowerPoint template.
9 #' @return R Markdown output format to pass to [render()]
10 #' @md
11 #' @export
12 powerpoint_presentation <- function(toc = FALSE,
13 toc_depth = 2,
14 number_sections = FALSE,
15 incremental = FALSE,
16 fig_width = 5,
17 fig_height = 4,
18 fig_caption = TRUE,
19 df_print = "default",
20 keep_md = FALSE,
21 md_extensions = NULL,
22 slide_level = NULL,
23 reference_doc = "default",
24 pandoc_args = NULL) {
25
26 # PowerPoint has been supported since Pandoc 2.0.5
27 pandoc_available('2.0.5', error = TRUE)
28
29 # knitr options and hooks
30 knitr <- knitr_options(opts_chunk = list(
31 dev = 'png', dpi = 96, fig.width = fig_width, fig.height = fig_height
32 ))
33
34 # base pandoc options for all pptx output
35 args <- c()
36
37 # table of contents
38 args <- c(args, pandoc_toc_args(toc, toc_depth))
39
40 # ppt template
41 args <- c(args, reference_doc_args("doc", reference_doc))
42
43 # incremental
44 if (incremental) {
45 if (!pandoc_available('2.15')) {
46 warning(
47 "`incremental = TRUE` for powerpoint presentation is supported since Pandoc 2.15.\n",
48 " It will have no effect with current Pandoc version used: ", pandoc_version(), ".",
49 call. = FALSE
50 )
51 } else {
52 args <- c(args, "--incremental")
53 }
54 }
55
56 # slide level
57 if (!is.null(slide_level))
58 args <- c(args, '--slide-level', as.character(slide_level))
59
60 # TODO: syntax highlighting
61
62 # pandoc args
63 args <- c(args, pandoc_args)
64
65 saved_files_dir <- NULL
66
67 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
68 saved_files_dir <<- files_dir
69 NULL
70 }
71
72 intermediates_generator <- function(...) {
73 reference_intermediates_generator(saved_files_dir, ..., reference_doc)
74 }
75
76 # return output format
77 output_format(
78 knitr = knitr,
79 pandoc = pandoc_options(
80 to = 'pptx',
81 from = from_rmarkdown(fig_caption, md_extensions),
82 args = args,
83 lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")
84 ),
85 keep_md = keep_md,
86 df_print = df_print,
87 pre_processor = pre_processor,
88 intermediates_generator = intermediates_generator
89 )
90 }
91
92 # copy the reference doc to the intermediate dir when the dir is specified
93 reference_intermediates_generator <- function(
94 saved_files_dir, original_input, intermediates_dir, reference_doc
95 ) {
96 res <- general_intermediates_generator(saved_files_dir, original_input, intermediates_dir)
97 if (is.null(reference_doc) || identical(reference_doc, 'default')) return(res)
98 if (!is_relative(reference_doc)) return(res) # an absolute path was provided; no need to copy
99 doc <- normalize_path(reference_doc, must_work = TRUE)
100 doc2 <- relative_to(normalize_path('.'), doc)
101 if (doc2 == doc) stop(
102 'The path of the reference document ', reference_doc, 'must be a relative ',
103 'path under the directory ', getwd()
104 )
105 doc3 <- file.path(intermediates_dir, doc2)
106 dir.create(dirname(doc3), FALSE, recursive = TRUE)
107 file.copy(doc2, doc3)
108 c(res, doc3)
109 }