R in Docker and Codespaces

R
cloud
Docker
packaging
Published

March 22, 2024

For my work, I use Docker regularly to create fully reproducible environments.

Docker

Finding a configuration for R with Rocker has taken some effort over time and led to several changes.

Recently, working with pak has worked well for me to install all R packages when the container is build.

Here is a minimal example for such a workflow.

FROM rocker/tidyverse

WORKDIR /home/rstudio

COPY pkg.lock .
RUN R -e 'pak::lockfile_install()'

The full example is available in my R template. There, I use an R script to create a pkg.lock with the pinned versions of R packages used in a project.

Codespaces

Working with Python, I use Codespaces quite regularly now. Figuring out a configuration with R took me some time. By now, I have a configuration that I feel comfortable with.

I started with the example from github.com/revodavid/devcontainers-rstudio that demonstrates running RStudio in Codespaces.

Over time, I moved to a codespace configuration that uses more of the settings in devcontainer.json and a minimal Dockerfile.

The codespace includes R, Python and Quarto. It takes quite some to build though.

Overall, it is a nice way to briefly explore an R project in a browser without installing any software locally. I keep codespaces only for a short time and delete them quickly. That is why I use Posit Cloud as a persistent online R environment.

Most of my R development is still in a local RStudio environment though. For reproducibility, I add a Docker configuration and make sure that the final version of a project can be replicated in that environment and has pinned versions of all dependencies.