http://rpubs.com/jzadra/rconf2018

Rmarkdown Refresher

  • Use multiple languages including R, Python, and SQL.
  • Output formats include HTML, PDF, MS Word, Beamer, HTML5 slides, Tufte-style handouts, books, dashboards, shiny applications, scientific articles, websites, and more.

The Precursor to Notebooks

Problems Rmarkdown Solves

Branding a Word Doc

Example

Example

Example

Useful Example: Page Breaks

Example

##### Pagebreak

# Overview
The raw data comes from ...

Branding Images

Branding Images with Magick

Branding Images with Magick

Branding Images with Magick

# Our function in Pseudocode

si_ggsave <- function(){
  
  ggsave(lastplot)
  
  if(add_logo){
    
    logo <- image_read("~/ourlogo.png") %>% 
      #make the width of the logo match the width of the plot
      image_crop(paste0(pwidth, "x0+0+0"))
    
    # The final version is stacked on top of the sorenson logo
    final_plot <- image_append(c(lastplot, logo), stack = TRUE)
    # And then we overwrite the standard ggsave call
    image_write(final_plot, filename)
  }
}

Packaging Your Brand

Customizations Add Up

Setup chunk: over 100 lines

  • Load libraries
  • Set knitr/rmarkdown options
  • Set directories
  • Define SI color names
  • Customize ggplot themes and geoms
  • Define custom functions

Goals

Part 1

  • Reduce setup chunk
  • Corral custom functions

Part 2

  • Effortless creation of new RMarkdown documents
  • Single-source templates that upgrade easily
  • Future proof / time-locked

Reduce Setup Chunk

Move all settings into broad "apply_settings()" type functions

si_ggplot_theme_apply <- function() {
  
  ggplot2::update_geom_defaults("bar", list(fill = si_design$pacific))
  ggplot2::update_geom_defaults("smooth", list(
    colour = si_design$pacific, 
    fill = si_design$arctic, alpha = I(2/10)))
  ggplot2::update_geom_defaults("point", list(
    colour = si_design$pacific, 
    fill = si_design$pacific))
  ggplot2::update_geom_defaults("col", list(fill = si_design$pacific))

  ggplot2::theme_set(ggplot2::theme_minimal())
}

Easy Creation of Rmarkdown Template

  • inst/ dir in package can hold anything
  • Contents are copied to local R/library/package dir on package install/update
  • Current version copied to working dir on each create

Using a Package for Template Storage

  • inst/ dir in package can hold anything
  • Contents are copied to local R/library/package dir on package install/update
  • Current version copied to working dir on each create

Draft Function

# Our draft function in Pseudocode
si_draft_full_report <- function(file) { 
  
  if(empty(dirname(file))) { #Create dir if not provided
    file <- file.path(default_dir, file)
  }

  if(tools::file_ext(file) != "Rmd") { #Fix the capitalization of Rmd
    message("Daniel you messed up the capitalization again. Setting extension to .Rmd")
    file <- paste0(tools::file_path_sans_ext(file), ".Rmd")
  }
  rmarkdown::draft(file = file,
                   template = "si_full_report",
                   package = "sorensonimpact",
                   edit = F)

  file.edit(file) #Open immediately in Rstudio editor
}

Draft Function in Action

Finishing Touches

One final piece…

Finishing Touches

One final piece…

Automating Your Work

An Old Method: Knitting to HTML

<!DOCTYPE html>
  
  <script>
  $(function() {
    $('#container').highcharts({
      xAxis: {
        categories: [<!--rinline I(shQuote(dates)) -->]
      },
      series: [{
        name: 'Tokyo',
        data: [<!--rinline I(Tokyo) -->]
      }, {
        name: 'London',
        data: [<!--rinline I(London) -->]
      }]
    });
  });  
</script>

Automating is Easy Now

for (county in unique(utah$County)) {
  try(render('./county_report.Rmd',
             output_file = paste(county, "County Report.docx"),
             output_dir = "./County_Reports/"))
}

A New Method

Thank you!