body_add_plot: Add plot in a 'Word' document

View source: R/docx_add.R

body_add_plotR Documentation

Add plot in a 'Word' document

Description

Add a plot as a png image into an rdocx object.

Usage

body_add_plot(
  x,
  value,
  width = 6,
  height = 5,
  res = 300,
  style = "Normal",
  pos = "after",
  unit = "in",
  ...
)

Arguments

x

an rdocx object

value

plot instructions, see plot_instr().

width, height

plot size in units expressed by the unit argument. Defaults to a width of 6 and a height of 5 "in"ches.

res

resolution of the png image in ppi

style

paragraph style

pos

where to add the new element relative to the cursor, one of "after", "before", "on".

unit

One of the following units in which the width and height arguments are expressed: "in", "cm" or "mm".

...

Arguments to be passed to png function.

See Also

Other functions for adding content: body_add_blocks(), body_add_break(), body_add_caption(), body_add_docx(), body_add_fpar(), body_add_gg(), body_add_img(), body_add_par(), body_add_table(), body_add_toc()

Examples

doc <- read_docx()

if (capabilities(what = "png")) {
  p <- plot_instr(
      code = {
        barplot(1:5, col = 2:6)
      }
    )

  doc <- body_add_plot(doc, value = p, style = "centered")

  # Set the unit in which the width and height arguments are expressed
  doc <- body_add_plot(doc, value = p, style = "centered", unit = "cm")
}

print(doc, target = tempfile(fileext = ".docx"))

officer documentation built on April 3, 2025, 10:27 p.m.