Slides from a talk given at the R-Ladies NYC meetup
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
2018-11-06-rladies-office-talk.pdf
2018-11-06-rladies-office-talk.pptx
README.md
cover-slide.png
sidebar.png
template.docx

README.md

Slides from a talk given at the R-Ladies NYC meetup on 2018-11-06.

(Lovely Gustave Doré Engraving from "The Ingenious Hidalgo Don Quixote of La Mancha" - 1863.)

Resources and references

Some code snippets demo'd

Reducing Round-trips: Making an editable Powerpoint Graphic from R with rvg

library(officer)
library(rvg)
library(tidyverse)  # ggplot and pipes

# Make a plot
my_ggplot <-
  ggplot(mtcars, aes(mpg, wt)) +
  geom_point(col = "red", size = 4) +
  labs(x = "Fuel efficiency (mpg)", y = "Weight (tons)",
       title = "Seminal ggplot2 scatterplot example",
       subtitle = "A plot that is only useful for demonstration purposes",
       caption = "Brought to you by the letter 'g'")

# Take a look
my_ggplot

# Draw it using office vector graphics
read_pptx() %>%    # Start with a blank PPT when no arg is given
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with_text("Vector graphics, nice.", type = "title") %>%
  ph_with_vg(ggobj = my_ggplot) %>%
  print("my_slide.pptx")

Shortening the journey: Post-processing a knitted word document

library(officer)
library(magrittr)
library(rmarkdown)

# Create a new rmarkdown output format to use
doc_enhanced <- function(...) {
  # Start with an existing format, modify defaults and pass on other args
  out <- rmarkdown::word_document(reference_doc = "template.docx", ...)

  # Add function that will modify the output after the fact
  out$post_processor <- function(metadata, input_file, output_file, clean, verbose) {
    
    
    doc <- read_docx("template.docx") %>%  # Re-use the template
      body_add_docx(output_file) %>%       # Pour the output into it
      body_replace_img_at_bkm(             # Add annotations at bookmarks
        bookmark = "logo",
        value = external_img(src = "sidebar.png", width = 1.77, height = 6.031))
    print(doc, output_file)
    return(output_file)                   # Return the path of the modified file
  }
  return(out)
}

# Now use this format to render
rmarkdown::render("word_doc.Rmd", output_format = doc_enhanced())