Skip to content

chendaniely/docker-renv

Repository files navigation

A docker renv container

An example repository that illustrates using the R {renv} package within a Docker container.

This allows you to mount the current working directory into the /home/rstudio/work directory, so you can work on your codebase within a container, and have the code changes reflected directly on the host filesystem when the container exits.

Workflow

You have a local host RStudio project that uses {renv} to track the package dependencies. You also want to run your host project inside a Docker container by mounting the current working directory host filesystem into the container filesystem when the container is run

Problem(s)

  1. Packages are installed in a folder corresponding to the operating system

{renv} installs R packages into a specific folder (usually under renv/library/). Different operating systems will have packages installed under a different directory name that is determined by the operating system renv::restore()-ing and installing packages.

  1. {renv} auto loads an rstudio project's renv project. So even if {renv} packages are installed properly in the container, when the host file system is mounted into the container, all the existing {renv} packages are overridden

Solution(s)

How this repository solves each corresponding problem

  1. Set an environment variables in the Dockerfile so {renv} can install it's packages in a pre-determined location. This repository uses /home/rstudio/renv so the user can see it when they connect to the rstudio server instance in the container. This is done by setting the RENV_PATHS_ROOT variable during the build and run process.

  2. Disables the renv autoloader when a renv enabled rstudio project is loaded inside the container. This is done by disabling the RENV_CONFIG_AUTOLOADER_ENABLED variable during the run process.

  3. To reduce confusion, the renv folder inside the host mounted filesystem will be overridden so there is no connection between the host and container /home/rstudio/work/renv directory.

How to use

On the local host:

  1. Do your analysis as you normally would, install, and library() any packages you need.
  2. renv::snapshot() to update the renv.lock on the host.
  3. Use the make build target to build the container and install the packages specified by renv.lock.
  4. Use the make up target to run the container.

You may want to change the image name. This repo uses container name, docker-renv. If you want to change the name, be sure to change it in the Makefile and in the docker-compose.yml file.

Limitation(s)

  1. The renv.lock file is only updated on the local host. You could update it within the container, but since the packages are installed directly in the container, we treat the renv installed packages during build as if they were installed directly in the system.
  2. When load an renv enabled rstudio project inside the container, the R console will say that it will be unable to load the renv environment. This can be ignored, since the packages are already installed in the container

Terminology

  • "Host" → Your local machine running Docker.
  • "Container" → The isolated environment inside Docker.
  • The host filesystem refers to files on your local machine.
  • The container filesystem refers to files inside the running container.

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors