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.)
- R packages by David Gohel: officer, rvg, flextable, mschart, worded (in development as of 2018-11-06)
- ArchieML, the New York Times' flexible markup for a Google Docs --> Structured Data workflow, and my R parser for it.
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")
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())