Debian Codegraph @

R/word_document.R

raw
1 #' Convert to an MS Word document
2 #'
3 #' Format for converting from R Markdown to an MS Word document.
4 #'
5 #' See the \href{https://yihui.org/rmarkdown/word-document.html}{online
6 #' documentation} for additional details on using the \code{word_document} format.
7 #'
8 #' R Markdown documents can have optional metadata that is used to generate a
9 #' document header that includes the title, author, and date. For more details
10 #' see the documentation on R Markdown \link[=rmd_metadata]{metadata}.
11 #'
12 #' R Markdown documents also support citations. You can find more information on
13 #' the markdown syntax for citations in the
14 #' \href{https://pandoc.org/MANUAL.html#citations}{Bibliographies
15 #' and Citations} article in the online documentation.
16 #' @inheritParams pdf_document
17 #' @inheritParams html_document
18 #' @param reference_docx Use the specified file as a style reference in
19 #' producing a docx file. For best results, the reference docx should be a
20 #' modified version of a docx file produced using pandoc. Pass "default"
21 #' to use the rmarkdown default styles.
22 #' @return R Markdown output format to pass to \code{\link{render}}
23 #' @examples
24 #' \dontrun{
25 #' library(rmarkdown)
26 #'
27 #' # simple invocation
28 #' render("input.Rmd", word_document())
29 #'
30 #' # specify an option for syntax highlighting
31 #' render("input.Rmd", word_document(highlight = "zenburn"))
32 #' }
33 #' @export
34 word_document <- function(toc = FALSE,
35 toc_depth = 3,
36 number_sections = FALSE,
37 fig_width = 5,
38 fig_height = 4,
39 fig_caption = TRUE,
40 df_print = "default",
41 highlight = "default",
42 reference_docx = "default",
43 keep_md = FALSE,
44 md_extensions = NULL,
45 pandoc_args = NULL) {
46
47 # knitr options and hooks
48 knitr <- knitr_options(
49 opts_chunk = list(dev = 'png',
50 dpi = 96,
51 fig.width = fig_width,
52 fig.height = fig_height)
53 )
54
55 # base pandoc options for all docx output
56 args <- c()
57
58 # table of contents
59 args <- c(args, pandoc_toc_args(toc, toc_depth))
60
61 # Lua filters (added if pandoc > 2)
62 lua_filters <- pkg_file_lua("pagebreak.lua")
63
64 # numbered sections
65 if (number_sections) {
66 if (pandoc_available("2.10.1")) {
67 args <- c(args, "--number-sections")
68 } else {
69 lua_filters <- c(lua_filters, pkg_file_lua("number-sections.lua"))
70 }
71 }
72
73 # highlighting
74 if (!is.null(highlight)) highlight <- resolve_highlight(highlight, highlighters())
75 args <- c(args, pandoc_highlight_args(highlight))
76
77 # reference docx
78 args <- c(args, reference_doc_args("docx", reference_docx))
79
80 # pandoc args
81 args <- c(args, pandoc_args)
82
83 saved_files_dir <- NULL
84 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
85 saved_files_dir <<- files_dir
86 NULL
87 }
88
89 intermediates_generator <- function(...) {
90 reference_intermediates_generator(saved_files_dir, ..., reference_docx)
91 }
92
93 # return output format
94 output_format(
95 knitr = knitr,
96 pandoc = pandoc_options(to = "docx",
97 from = from_rmarkdown(fig_caption, md_extensions),
98 args = args,
99 lua_filters = lua_filters),
100 keep_md = keep_md,
101 df_print = df_print,
102 pre_processor = pre_processor,
103 intermediates_generator = intermediates_generator
104 )
105 }
106
107 reference_doc_args <- function(type, doc) {
108 if (is.null(doc) || identical(doc, "default")) return()
109 c(paste0("--reference-", if (pandoc2.0()) "doc" else {
110 match.arg(type, c("docx", "odt", "doc"))
111 }), pandoc_path_arg(doc))
112 }
113