rdocx_document {officedown}R Documentation

Advanced R Markdown Word Format

Description

'R Markdown' Format for converting from 'R Markdown' document to an MS Word document.

The function enhances the output offered by rmarkdown::word_document() with advanced formatting features.

Usage

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

Arguments

base_format

a scalar character, the 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

see section 'Tables' below.

plots

see section 'Plots' below.

lists

see section 'Lists' below.

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. If not null (the default), these values are used to define the default Word section. See officer::page_size() and officer::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.

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:

Default value is (in YAML format):

style: Table
layout: autofit
width: 1.0
topcaption: true
tab.lp: 'tab:'
caption:
  style: Table Caption
  pre: 'Table'
  sep: ':'
  tnd: 0
  tns: '-'
  fp_text: !expr officer::fp_text_lite(bold = TRUE)
conditional:
  first_row: true
  first_column: false
  last_row: false
  last_column: false
  no_hband: false
  no_vband: true

Plots

Argument plot is expected to be a list. It can contain few items to style figures and figure captions.

You don't have to provide values for each items of the list, missing items are replaced by default values. Possible items are:

Default value is (in YAML format):

style: Normal
align: center
topcaption: false
fig.lp: 'fig:'
caption:
  style: Image Caption
  pre: 'Figure '
  sep: ': '
  tnd: 0
  tns: '-'
  fp_text: !expr officer::fp_text_lite(bold = TRUE)

Lists

The parameter lists is a list that can contain two named items:

Default values are list(ol.style = NULL, ul.style = NULL).

Expected 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.

These values in YAML format are:

output: 
  officedown::rdocx_document:
    lists:
      ol.style: null
      ul.style: null

They can have values corresponding to existing stylenames (of type 'numbering'). With package 'officer', we can read these values with styles_info().

library(officer)
docx_file <- system.file(
  package = "officedown", "examples",
  "bookdown", "template.docx"
)
doc <- read_docx(docx_file)
styles_info(doc, type = "numbering")[, 1:6]
#>    style_type    style_id style_name     base_on is_custom is_default
#> 13  numbering Aucuneliste    No List        <NA>     FALSE       TRUE
#> 40  numbering   Defaultul Default ul Aucuneliste      TRUE      FALSE
#> 41  numbering   Defaultol Default ol Aucuneliste      TRUE      FALSE

From the above available values, the possible configuration is possible:

output: 
  officedown::rdocx_document:
    lists:
      ol.style: 'Default ol'
      ul.style: 'Default ul'

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")[, 1:6]
#>    style_type      style_id    style_name      base_on is_custom is_default
#> 1   paragraph        Normal        Normal         <NA>     FALSE       TRUE
#> 2   paragraph        Titre1     heading 1       Normal     FALSE      FALSE
#> 3   paragraph        Titre2     heading 2       Normal     FALSE      FALSE
#> 4   paragraph        Titre3     heading 3       Normal     FALSE      FALSE
#> 9   paragraph      centered      centered       Normal      TRUE      FALSE
#> 15  paragraph  ImageCaption Image Caption       Normal      TRUE      FALSE
#> 16  paragraph  TableCaption Table Caption ImageCaption      TRUE      FALSE
#> 18  paragraph           TM1         toc 1       Normal     FALSE      FALSE
#> 19  paragraph           TM2         toc 2       Normal     FALSE      FALSE
#> 20  paragraph Textedebulles  Balloon Text       Normal     FALSE      FALSE
#> 23  paragraph  graphictitle graphic title ImageCaption      TRUE      FALSE
#> 24  paragraph    tabletitle   table title TableCaption      TRUE      FALSE

To read table stylenames:

styles_info(doc, type = "table")[, 1:6]
#>    style_type             style_id          style_name       base_on is_custom
#> 6       table        TableauNormal        Normal Table          <NA>     FALSE
#> 10      table        tabletemplate      table_template TableauNormal      TRUE
#> 11      table  Listeclaire-Accent2 Light List Accent 2 TableauNormal     FALSE
#> 17      table Tableauprofessionnel  Table Professional TableauNormal     FALSE
#>    is_default
#> 6        TRUE
#> 10      FALSE
#> 11      FALSE
#> 17      FALSE

To read list stylenames:

styles_info(doc, type = "numbering")[, 1:6]
#>   style_type    style_id style_name base_on is_custom is_default
#> 7  numbering Aucuneliste    No List    <NA>     FALSE       TRUE

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
      topcaption: true
      tab.lp: 'tab:'
      caption:
        style: Table Caption
        pre: 'Table '
        sep: ': '
        tnd: 0
        tns: '-'
        fp_text: !expr officer::fp_text_lite(bold = TRUE)
      conditional:
        first_row: true
        first_column: false
        last_row: false
        last_column: false
        no_hband: false
        no_vband: true
    plots:
      style: Normal
      align: center
      fig.lp: 'fig:'
      topcaption: false
      caption:
        style: Image Caption
        pre: 'Figure '
        sep: ': '
        tnd: 0
        tns: '-'
        fp_text: !expr officer::fp_text_lite(bold = TRUE)
    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

# rdocx_document basic example -----
library(rmarkdown)
library(officedown)

if (pandoc_available() && pandoc_version() >= numeric_version("2.0")) {
  # minimal example -----
  example <- system.file(
    package = "officedown",
    "examples/minimal_word.Rmd"
  )
  rmd_file <- tempfile(fileext = ".Rmd")
  file.copy(example, to = rmd_file)

  docx_file <- tempfile(fileext = ".docx")
  render(rmd_file, output_file = docx_file, quiet = TRUE)
}

[Package officedown version 0.3.3 Index]