Learn R Programming

officer (version 0.6.7)

body_add: Add content into a Word document

Description

This function add objects into a Word document. Values are added as new paragraphs or tables.

This function is experimental and will replace the body_add_* functions later. For now it is only to be used for successive additions and cannot be used in conjunction with the body_add_* functions.

Usage

body_add(x, value, ...)

# S3 method for character body_add(x, value, style = NULL, ...)

# S3 method for numeric body_add(x, value, style = NULL, format_fun = formatC, ...)

# S3 method for factor body_add(x, value, style = NULL, format_fun = as.character, ...)

# S3 method for fpar body_add(x, value, style = NULL, ...)

# S3 method for data.frame body_add( x, value, style = NULL, header = TRUE, tcf = table_conditional_formatting(), alignment = NULL, ... )

# S3 method for block_caption body_add(x, value, ...)

# S3 method for block_list body_add(x, value, ...)

# S3 method for block_toc body_add(x, value, ...)

# S3 method for external_img body_add(x, value, style = "Normal", ...)

# S3 method for run_pagebreak body_add(x, value, style = NULL, ...)

# S3 method for run_columnbreak body_add(x, value, style = NULL, ...)

# S3 method for gg body_add( x, value, width = 6, height = 5, res = 300, style = "Normal", scale = 1, ... )

# S3 method for plot_instr body_add(x, value, width = 6, height = 5, res = 300, style = "Normal", ...)

# S3 method for block_pour_docx body_add(x, value, ...)

# S3 method for block_section body_add(x, value, ...)

Arguments

x

an rdocx object

value

object to add in the document. Supported objects are vectors, data.frame, graphics, block of formatted paragraphs, unordered list of formatted paragraphs, pretty tables with package flextable, 'Microsoft' charts with package mschart.

...

further arguments passed to or from other methods. When adding a ggplot object or plot_instr, these arguments will be used by png function. See method signatures to see what arguments can be used.

style

paragraph style name. These names are available with function styles_info and are the names of the Word styles defined in the base document (see argument path from read_docx).

format_fun

a function to be used to format values.

header

display header if TRUE

tcf

conditional formatting settings defined by table_conditional_formatting()

alignment

columns alignement, argument length must match with columns length, values must be "l" (left), "r" (right) or "c" (center).

width

height in inches

height

height in inches

res

resolution of the png image in ppi

scale

Multiplicative scaling factor, same as in ggsave

Methods (by class)

  • body_add(character): add a character vector.

  • body_add(numeric): add a numeric vector.

  • body_add(factor): add a factor vector.

  • body_add(fpar): add a fpar object. These objects enable the creation of formatted paragraphs made of formatted chunks of text.

  • body_add(data.frame): add a data.frame object with block_table().

  • body_add(block_caption): add a block_caption object. These objects enable the creation of set of formatted paragraphs made of formatted chunks of text.

  • body_add(block_list): add a block_list object.

  • body_add(block_toc): add a table of content (a block_toc object).

  • body_add(external_img): add an image (a external_img object).

  • body_add(run_pagebreak): add a run_pagebreak object.

  • body_add(run_columnbreak): add a run_columnbreak object.

  • body_add(gg): add a ggplot object.

  • body_add(plot_instr): add a base plot with a plot_instr object.

  • body_add(block_pour_docx): pour content of an external docx file with with a block_pour_docx object

  • body_add(block_section): ends a section with a block_section object

Illustrations

Examples

Run this code
doc_1 <- read_docx()
doc_1 <- body_add(doc_1, "Table of content", style = "heading 1")
doc_1 <- body_add(doc_1, block_toc())
doc_1 <- body_add(doc_1, run_pagebreak())
doc_1 <- body_add(doc_1, "A title", style = "heading 1")
doc_1 <- body_add(doc_1, head(iris), style = "table_template")
doc_1 <- body_add(doc_1, "Another title", style = "heading 1")
doc_1 <- body_add(doc_1, letters, style = "Normal")
doc_1 <- body_add(
  doc_1,
  block_section(prop_section(type = "continuous"))
)
doc_1 <- body_add(doc_1, plot_instr(code = barplot(1:5, col = 2:6)))
doc_1 <- body_add(
  doc_1,
  block_section(prop_section(page_size = page_size(orient = "landscape")))
)
print(doc_1, target = tempfile(fileext = ".docx"))
# print(doc_1, target = "test.docx")

Run the code above in your browser using DataLab