Author Carpentry logo

RStudio in a Docker container

5 minutes


Learning Objectives


Starting RStudio

RStudio is a popular IDE for the R language and environment for statistical computing and graphics.

Ben Marwick demonstrates in his research compendium how to use a containerised version of RStudio and why. In brief, he only used the container to develop an R package, which contains all of the computations for a scientific article he co-authored.

I developed and tested the package on this Docker container, so this is the only platform that I’m confident it works on, and so recommend to anyone wanting to use this package to generate the vignette, etc.

By only working within the container, he created a transferable execution environment. His host operating system is free for experimentation and to be secure (e.g. installing a later library version) without risking the integrity of the combination of packages in the container.

The performance overhead for running RStudion in a container is negligent for most use cases, unlike you might know from virtual machines (cf. slides by B. Marwick).

Here is all you need to start it:

$ docker run -p 8787:8787 rocker/rstudio
# stop the container with Ctrl+c

Using the CLI option -p we expose a port of the container to our host computer.

No open the UI in your browser: http://localhost:8787 and sign-in with rstudio/rstudio. You can install packages and run analyses now just as in your regular RStudio version, of course ideally by using executable documents with Markdown.

Rocker images

We use an image from the Rocker organisation (see the Rocker announcement for some background). Rocker published different variants of images for R targeted at different use cases.

They provide base images](https://github.com/rocker-org/rocker#base-docker-containers) and also images with different collections of pre-installed packages, the use case images.

If you are a useR, these should get you started quickly.

How is this useful for reproducible research?

You can easily try out and compare different R versions using the r-ver images.

$ docker run -it rocker/r-ver:3.1.0
# [image download log]

R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 1+1
[1] 2
> q()

This gives you an interactive session of R in a specific version. Nice! You can leave the R session just as a regular R session by typing quit().

More resources on Docker and R