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.
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
- 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.
{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
How this repository solves each corresponding problem
-
Set an environment variables in the
Dockerfileso{renv}can install it's packages in a pre-determined location. This repository uses/home/rstudio/renvso the user can see it when they connect to the rstudio server instance in the container. This is done by setting theRENV_PATHS_ROOTvariable during the build and run process. -
Disables the renv autoloader when a
renvenabled rstudio project is loaded inside the container. This is done by disabling theRENV_CONFIG_AUTOLOADER_ENABLEDvariable during the run process. -
To reduce confusion, the
renvfolder inside the host mounted filesystem will be overridden so there is no connection between the host and container/home/rstudio/work/renvdirectory.
On the local host:
- Do your analysis as you normally would, install, and
library()any packages you need. renv::snapshot()to update therenv.lockon the host.- Use the
make buildtarget to build the container and install the packages specified byrenv.lock. - Use the
make uptarget 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.
- The
renv.lockfile 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. - 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
- "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.