Site icon R-bloggers

An Environment for Reliably Rendering Figures in R

[This article was first published on R | datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Fathom Data is working on a project to reproduce the figures from the CORE textbook The Economy using R and {ggplot2}. There’s a strict style guide which specifies the figure aesthetics including colours and . We’re a team of seven people working on as many different setups. The principle challenges have been package versions and s.

We’ve solved the problem by creating a custom Docker image, which provides both packages and s. In retrospect, we should have done this much earlier.

Dockerfile

This is the Dockerfile describing the image.

FROM rocker/verse:4.0.4

RUN apt-get update -qq && \
    apt-get install -y -qq ttf-ubuntu--family && \
    apt-get -y purge s-roboto s-roboto-unhinted s-lmodern s-texgyre && \
    # Clean up after apt.
    rm -rf /var/lib/apt/lists/*

# Install Google Fonts.
#
ARG GOOGLE_TTF=/usr/shares/truetype/google-s
#
RUN git clone git://github.com/googles.git && \
    mkdir -p $GOOGLE_TTF && \
    find s -regex '.*Asap-[a-zA-Z]+\.ttf$' -exec install -Dm644 {} $GOOGLE_TTF \; && \
    rm -rf s/ && \
    fc-cache -f > /dev/null

RUN install2.r -e \
      extra \
      ggnewscale \
      ggtext \
      googledrive \
      gridExtra \
      here \
      htmlwidgets \
      janitor \
      patchwork \
      plotly \
      readxl \
      rsvg \
      showtext \
      svglite

RUN R -e "library(extra); _import(prompt = FALSE); loads(quiet = FALSE)"

COPY core ./core

RUN cd core && \
    R -e "devtools::build(); devtools::install()"

ENV PASSWORD CORE

Here are the key points:

  • It’s based on the 4.0.4 version of the rocker/verse image, which means that it immediately includes the most recent version of R, RStudio, all of the {tidyverse} as well as TeX and other publishing-related packages.
  • All of the Google Fonts are cloned from their GitHub repository, but only Asap is retained.
  • The {extra} package is used to import system TTF s into R.
  • All of the other packages that we’re using are baked into the image.
  • The {core} package (a submodule on our repository), built and maintained by Megan Beckett, is copied across onto the image, built and installed.
  • A default password is set. There’s probably no harm in this since the image will only be used locally.

Running

Create a volume to persist work between sessions.

docker volume create core

Run the image, mounting the volume and exposing port 8787.

docker run -it --rm \
    --name rcore \
    -p 8787:8787 \
    --mount source=core,target=/home/rstudio \
    rcore:latest

And access at 127.0.0.1:8787.

Authentication

All of the data for the figures are stored in Google Sheets. So an important component of the workflow is authenticating with Google. We wanted this to occur immediately upon loading the {core} package and the authentication details to be persisted between sessions. Matt Dennis implemented this using the use_oob option to googledrive::drive_auth(), checking first for an interactive session to ensure that it wasn’t triggered during package build.

.onLoad <- function(libname, pkgname) {
  if (interactive()) {
    googledrive::drive_auth(use_oob = TRUE)
  }
}

With this setup we have everything we need to create the figures in a stable, reproducible environment. No need to install s or packages. Rather than worrying about technical issues, we can focus on the figures.

???? Thanks to the maintainers of the version-stable Rocker images, which made putting this together a lot easier.

To leave a comment for the author, please follow the link and comment on their blog: R | datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.