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"))
Run the code above in your browser using DataLab