Word documents and graphics

Graphics and more generally raster images and vector graphic images are added in a Word document as elements of a paragraph. officer provides two functions to let users add images into a Word document:

  • body_add_img: add an image in a new paragraph. This is usually the function to use when the need is to add a plot generated with R into a document.
  • slip_in_img: add an image inside an existing paragraph (defined by the cursor location), user can choose to insert it at beginning or end of the paragraph. This allows to mix text and images within a single paragraph.

Those functions accept various image formats: png, jpeg and emf.

R plots

So far, tests has been made with outputs from grDevices::png(), grDevices::jpeg() and devEMF::emf(). Enhanced metafile graphics produced by package devEMF seem to be the best solution to have vector graphics within Word document.

library(officer)
library(devEMF)
library(magrittr)
library(ggplot2)

officer is not producing the image, it only provides functions to add a produced image into a Word document.

Let’s first produce the image:

# ggplot example
gg <- ggplot(mtcars, aes(x = mpg , y = wt, colour = qsec)) + geom_point() + theme_minimal()

# produce an emf file containing the ggplot
filename <- tempfile(fileext = ".emf")
emf(file = filename, width = 6, height = 7)
print(gg)
dev.off()

Then add it to a new Word document:

read_docx() %>% 
  body_add_img(src = filename, width = 6, height = 7) %>% 
  print(target = "../assets/graphics/demo_emf.docx") %>% 
  invisible()

Download file demo_emf.docx - view with office web viewer

Microsoft charts

Package mschart enable to create native office charts that can be used with officer. Function body_add_chart can be used to generate (from R plot commands) a chart object within Word.

library(mschart)

my_barchart <- ms_barchart(data = browser_data,
  x = "browser", y = "value", group = "serie")
my_barchart <- chart_settings( x = my_barchart,
  dir="vertical", grouping="clustered", gap_width = 50 )

read_docx() %>% 
  body_add_chart(chart = my_barchart, style = "centered") %>% 
  print(target = "../assets/graphics/demo_mschart_01.docx") %>% 
  invisible()

Download file demo_mschart_01.docx - view with office web viewer

PowerPoint documents and graphics

Raster images and vector graphic images are added in a PowerPoint document as a shape on the current slide. In PowerPoint, images and text can not be mixed in the same shape (or placeholder). officer provides two functions to let users add images into a Word document:

  • ph_with_img: add an image as a new shape in the current slide.
  • ph_with_img_at: add an image in the current slide at any arbitrary location.

Again those functions accept various image formats: png, jpeg and emf.

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_img(src = filename, width = 6, height = 7, type = "body") %>% 
  add_slide(layout = "Title Only", master = "Office Theme") %>% 
  ph_with_img_at(src = filename, left = 0, top = 0, width = 6, height = 7) %>% 
  print(target = "../assets/graphics/demo_emf.pptx") %>% 
  invisible()

Download file demo_emf.pptx - view with office web viewer

vector graphics

Package rvg is providing editable vector graphics that can be used with officer. Function ph_with_vg can be used to generate (from R plot commands) a nice editable graphic within PowerPoint. Function ph_with_vg_at is also provided to let you add a graphic at any arbitrary location in the current slide.

library(rvg)
read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_vg(code = print(gg), type = "body") %>% 
  print(target = "../assets/graphics/demo_rvg.pptx") %>% 
  invisible()

Download file demo_rvg.pptx - view with office web viewer

Microsoft charts

Package mschart enable to create native office charts that can be used with officer. Function ph_with_chart can be used to generate (from R plot commands) a chart object within PowerPoint. Function ph_with_chart_at is also provided to let you add a chart at any arbitrary location in the current slide.

library(mschart)

my_barchart <- ms_barchart(data = browser_data,
  x = "browser", y = "value", group = "serie")
my_barchart <- chart_settings( x = my_barchart,
  dir="vertical", grouping="clustered", gap_width = 50 )

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_chart(chart = my_barchart, type = "body") %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_chart_at(chart = my_barchart, 
                   left = 0, top = 0, width = 4, height = 4) %>% 
  print(target = "../assets/graphics/demo_mschart_01.pptx") %>% 
  invisible()

Download file demo_mschart_01.pptx - view with office web viewer