Format for converting from R Markdown to an MS Word document. The function comes also with improved output options.

rdocx_document(
  base_format = "rmarkdown::word_document",
  tables = list(),
  plots = list(),
  lists = list(),
  mapstyles = list(),
  page_size = list(),
  page_margins = list(),
  reference_num = TRUE,
  ...
)

Arguments

base_format

a scalar character, format to be used as a base document for officedown. default to word_document but can also be word_document2 from bookdown.

When the base_format used is bookdown::word_document2, the number_sections parameter is automatically set to FALSE. Indeed, if you want numbered titles, you are asked to use a Word document template with auto-numbered titles (the title styles of the default `rdocx_document' template are already set to FALSE).

tables

a list that can contain few items to style tables and table captions. Missing items will be replaced by default values. Possible items are the following:

  • style: the Word stylename to use for tables.

  • layout: 'autofit' or 'fixed' algorithm. See table_layout.

  • width: value of the preferred width of the table in percent (base 1).

  • caption; caption options, i.e.:

    • style: Word stylename to use for table captions.

    • pre: prefix for numbering chunk (default to "Table ").

    • sep: suffix for numbering chunk (default to ": ").

  • conditional: a list of named logical values:

    • first_row and last_row: apply or remove formatting from the first or last row in the table

    • first_column and last_column: apply or remove formatting from the first or last column in the table

    • no_hband and no_vband: don't display odd and even rows or columns with alternating shading for ease of reading.

Default value is (in R format):

list(
   style = "Table", layout = "autofit", width = 1,
   caption = list(
     style = "Table Caption", pre = "Table ", sep = ": "),
   conditional = list(
     first_row = TRUE, first_column = FALSE, last_row = FALSE,
     last_column = FALSE, no_hband = FALSE, no_vband = TRUE
   )
)

Default value is (in YAML format):

style: Table
layout: autofit
width: 1.0
caption:
  style: Table Caption
  pre: 'Table '
  sep: ': '
conditional:
  first_row: true
  first_column: false
  last_row: false
  last_column: false
  no_hband: false
  no_vband: true
plots

a list that can contain few items to style figures and figure captions. Missing items will be replaced by default values. Possible items are the following:

  • style: the Word stylename to use for plots.

  • align: alignment of figures in the output document (possible values are 'left', 'right' and 'center').

  • topcaption: caption will appear before (on top of) the figure,

  • caption; caption options, i.e.:

    • style: Word stylename to use for figure captions.

    • pre: prefix for numbering chunk (default to "Figure ").

    • sep: suffix for numbering chunk (default to ": ").

Default value is (in R format):

list(
  style = "Normal", align = "center", topcaption = FALSE,
  caption = list(
    style = "Image Caption",
    pre = "Figure ",
    sep = ": "
   )
 )

Default value is (in YAML format):

style: Normal
align: center
topcaption: false
caption:
  style: Image Caption
  pre: 'Figure '
  sep: ': '
lists

a list containing two named items ol.style and ul.style, values are the stylenames to be used to replace the style of ordered and unordered lists created by pandoc. If NULL, no replacement is made.

Default value is list(ol.style = NULL, ul.style = NULL):

ol.style: null
ul.style: null
mapstyles

a named list of style to be replaced in the generated document. list("Normal" = c("Author", "Date")) will result in a document where all paragraphs styled with stylename "Date" and "Author" will be then styled with stylename "Normal".

page_size, page_margins

default page and margins dimensions, these values are used to define the default Word section. See page_size() and page_mar().

reference_num

if TRUE, text for references to sections will be the section number (e.g. '3.2'). If FALSE, text for references to sections will be the text (e.g. 'section title').

...

arguments used by word_document

Value

R Markdown output format to pass to render

Finding stylenames

You can access them in the Word template used. Function styles_info() can let you read these styles.

You need officer to read the stylenames (to get information from a specific "reference_docx", change ref_docx_default in the example below.

library(officer)
docx_file <- system.file(package = "officer", "template", "template.docx")
doc <- read_docx(docx_file)

To read paragraph stylenames:

styles_info(doc, type = "paragraph")

To read table stylenames:

styles_info(doc, type = "table")

To read list stylenames:

styles_info(doc, type = "numbering")

R Markdown yaml

The following demonstrates how to pass arguments in the R Markdown yaml:

---
output:
  officedown::rdocx_document:
    reference_docx: pandoc_template.docx
    tables:
      style: Table
      layout: autofit
      width: 1.0
      caption:
        style: Table Caption
        pre: 'Table '
        sep: ': '
      conditional:
        first_row: true
        first_column: false
        last_row: false
        last_column: false
        no_hband: false
        no_vband: true
    plots:
      style: Normal
      align: center
      caption:
        style: Image Caption
        pre: 'Figure '
        sep: ': '
    lists:
      ol.style: null
      ul.style: null
    mapstyles:
      Normal: ['First Paragraph', 'Author', 'Date']
    page_size:
      width: 8.3
      height: 11.7
      orient: "portrait"
    page_margins:
      bottom: 1
      top: 1
      right: 1.25
      left: 1.25
      header: 0.5
      footer: 0.5
      gutter: 0.5
    reference_num: true
---

Examples

library(rmarkdown) run_ok <- pandoc_available() && pandoc_version() >= numeric_version("2.0") if(run_ok){ # minimal example ----- example <- system.file(package = "officedown", "examples/minimal_word.Rmd") rmd_file <- tempfile(fileext = ".Rmd") file.copy(example, to = rmd_file) docx_file_1 <- tempfile(fileext = ".docx") render(rmd_file, output_file = docx_file_1, quiet = TRUE) render(rmd_file, output_file = docx_file_1, quiet = TRUE, intermediates_dir = tempfile()) # bookdown example ----- if(require("bookdown")){ bookdown_loc <- system.file(package = "officedown", "examples/bookdown") temp_dir <- tempfile() # uncomment next line to get the result in your working directory # temp_dir <- "./bd_example" dir.create(temp_dir, showWarnings = FALSE, recursive = TRUE) file.copy( from = list.files(bookdown_loc, full.names = TRUE), to = temp_dir, overwrite = TRUE, recursive = TRUE) render_site( input = temp_dir, encoding = 'UTF-8', envir = new.env(), quiet = TRUE) docx_file_2 <- file.path(temp_dir, "_book", "bookdown.docx") if(file.exists(docx_file_2)){ message("file ", docx_file_2, " has been written.") } } }
#> Loading required package: bookdown
#> #> #> processing file: bookdown.Rmd
#> | | | 0% | |... | 4% #> inline R code fragments #> #> | |..... | 7% #> label: unnamed-chunk-1 (with options) #> List of 1 #> $ include: logi FALSE #> #> | |........ | 11% #> inline R code fragments #> #> | |.......... | 15% #> label: unnamed-chunk-2 (with options) #> List of 1 #> $ eval: logi FALSE #> #> | |............. | 19% #> ordinary text without R code #> #> | |................ | 22% #> label: unnamed-chunk-3 #> | |.................. | 26% #> ordinary text without R code #> #> | |..................... | 30% #> label: unnamed-chunk-4 #> | |....................... | 33% #> ordinary text without R code #> #> | |.......................... | 37% #> label: unnamed-chunk-5 (with options) #> List of 1 #> $ include: logi FALSE #> #> | |............................. | 41% #> ordinary text without R code #> #> | |............................... | 44% #> label: unnamed-chunk-6 (with options) #> List of 3 #> $ tab.cap : chr "mtcars" #> $ tab.id : chr "mtcars" #> $ first_column: logi TRUE #> #> | |.................................. | 48% #> ordinary text without R code #> #> | |.................................... | 52% #> label: unnamed-chunk-7 (with options) #> List of 4 #> $ tab.cap : chr "iris" #> $ tab.id : chr "iris" #> $ no_hband : logi TRUE #> $ first_column: logi FALSE #> #> | |....................................... | 56% #> ordinary text without R code #> #> | |......................................... | 59% #> label: unnamed-chunk-8 (with options) #> List of 2 #> $ tab.cap: chr "flextable" #> $ tab.id : chr "flextable" #>
#> | |............................................ | 63% #> inline R code fragments #> #> | |............................................... | 67% #> label: unnamed-chunk-9 (with options) #> List of 6 #> $ echo : logi FALSE #> $ fig.cap : chr "sin function" #> $ fig.id : chr "sinus" #> $ fig.width : num 6 #> $ fig.height: num 4 #> $ fig.align : chr "center" #>
#> | |................................................. | 70% #> inline R code fragments #> #> | |.................................................... | 74% #> label: unnamed-chunk-10 #> | |...................................................... | 78% #> ordinary text without R code #> #> | |......................................................... | 81% #> label: unnamed-chunk-11 #> | |............................................................ | 85% #> ordinary text without R code #> #> | |.............................................................. | 89% #> label: unnamed-chunk-12 (with options) #> List of 3 #> $ fig.cap : chr "Sunspot plot" #> $ fig.id : chr "ts-plot" #> $ fig.cap.style: chr "Image Caption" #>
#> | |................................................................. | 93% #> ordinary text without R code #> #> | |................................................................... | 96% #> label: unnamed-chunk-13 (with options) #> List of 2 #> $ fig.cap: chr "$\\sum_{i=1}^n X_i$" #> $ fig.id : chr "lynx" #>
#> | |......................................................................| 100% #> ordinary text without R code #> #>
#> output file: bookdown.knit.md
#> /usr/bin/pandoc +RTS -K512m -RTS bookdown.utf8.md --to docx --from markdown+autolink_bare_uris+tex_math_single_backslash --output bookdown.docx --lua-filter /home/runner/work/_temp/Library/bookdown/rmarkdown/lua/custom-environment.lua --lua-filter /home/runner/work/_temp/Library/rmarkdown/rmarkdown/lua/pagebreak.lua --metadata-file /tmp/RtmpEktquj/file119468ea7fd0 --highlight-style tango --reference-doc /tmp/RtmpEktquj/file119435134ffe/template.docx
#> #> Output created: _book/bookdown.docx
#> file /tmp/RtmpEktquj/file119435134ffe/_book/bookdown.docx has been written.