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.

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)

  • character: add a character vector.

  • numeric: add a numeric vector.

  • factor: add a factor vector.

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

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

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

  • block_list: add a block_list object.

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

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

  • run_pagebreak: add a run_pagebreak object.

  • run_columnbreak: add a run_columnbreak object.

  • gg: add a ggplot object.

  • plot_instr: add a base plot with a plot_instr object.

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

  • block_section: ends a section with a block_section object

Illustrations

Examples

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")