This repo contains the various configurations, container settings, and other setup files that I will use in my new development setup for R projects on linux. Typically I would install R directly in my system along with the various IDEs and other tools that integrate with R. Now that I have systems with larger amounts of memory and faster CPUs, I am now experimenting with a development environment based on container technology, in particular Docker contaners. I have the following goals for my new development setup:
{renv}
package management system.🎥 Check out this previous Shiny Developer Series livestream to see my detailed walkthrough!
docker
group.ssh-add ~/.ssh/{name_of_key}
. I have a custom key but I would imagine most users will have a default key name of id_rsa
.The .devcontainer
directory in this repository contains the custom configuration files and build instructions for creating the development container. The Dockerfile
and devcontainer.json
were adapted from the VSCode dev containers GitHub repository here. Highlights of the customizations I made:
rocker/r-ver:4.0.2
from the Rocker Projectrenv
renv
cache directory mounted into the containerWith everything set up, I was able to mostly follow the official VS Code documenation page on developing inside a container to get the development container launched. I am still learning the ropes so to speak with day-to-day usage of VS-Code with R coding. Here are some additional observations that I will update as I continue my journey:
options(vsc.browser=FALSE)
once in the R session, then run the application (which will open the app in an external window). Close the app, then run options(vsc.browser="Active")
in the R session, and then run the app again. The app should open in the VS-Code tab like usual.{lintr}
package and might need some tweaking. Consult the project configuration of the lintr
repository for more infomation.In recent years, the open-source version of RStudio Server has been succesfully integrated into Docker containers in multiple projects including the excellent Rocker Project led by Carl Boettiger, Dirk Eddelbuettel, and Noam Ross. Recently, they created version-stable R containers based on R version 4.0.0 or later at the rocker-org/rocker-versioned2 GitHub repository based on the recent Ubuntu 20.04 LTS release, as well as integrated the new RStudio Package Manager (RSPM) which hosts compiled binaries of R packages for installation on Linux. I decided to create a custom container for RStudio server following principles in their new infrastructure with a custom Dockerfile
inspired by their Dockerfile_rstudio_4.0.2
, and wrapping the execution within Docker Compose. Highlights of the customizations I implemented:
renv
cache directory mounted into the containerrenv
rstudio.env
I have set up a local directory on my system that is mounted as a volume in each container to hold the R package cache. As long as your logged-in user has read and write priveleges to this directory on your host system, there should be no issues with each container reading and writing to the cache dir. In the containers, the directory is located at /renv/cache
while on my host system it is located at /opt/local/renv/cache
, but this could be any directory on your host system. In the Docker configuration files for each container, I set up the following environment variables:
RENV_PATHS_CACHE_HOST=/opt/local/renv/cache
RENV_PATHS_CACHE_CONTAINER=/renv/cache
RENV_PATHS_CACHE=/renv/cache
One (intentional) effect of using renv
is that out of the box the project's package library will not link to packages in the default library that are not included in the base installation of R. The interactions between an R session and VS-Code are largely driven by the {launguageserver}
package available on CRAN. My solution to ensure any project with renv
enabled can use all of the same integrations with VS-Code is to create a custom .Rprofile
that contains certain triggers to bootstrap the installation of languageserver
and perform necessary environment configurations if the session detects that the TERM_PROGRAM
environment variable is set to vscode
. The file .devcontainer/library-scripts/.Rprofile-vscode
is automatically copied to the container's renv
cache directory, and can be manually copied to overwrite the current project's .Rprofile
file. Much of this solution was adapted from ideas discussed in issue vscode-R/259. The contents are below:
# setup if using with vscode and R plugin
if (Sys.getenv("TERM_PROGRAM") == "vscode") {
source(file.path(Sys.getenv(if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"), ".vscode-R", "init.R"))
}
source("renv/activate.R")
if (Sys.getenv("TERM_PROGRAM") == "vscode") {
# obtain list of packages in renv library currently
project <- renv:::renv_project_resolve(NULL)
lib_packages <- names(unclass(renv:::renv_diagnostics_packages_library(project))$Packages)
# detect whether key packages are already installed
# was: !require("languageserver")
if (!"languageserver" %in% lib_packages) {
message("installing languageserver package")
renv::install("languageserver")
}
if (!"httpgd" %in% lib_packages) {
message("installing httpgd package")
renv::install("nx10/httpgd")
}
if (!"vscDebugger %in% lib_packages) {
message("installation vscDebugger package")
renv::install("ManuelHentschel/vscDebugger@v0.4.3")
}
# use the new httpgd plotting device
options(vsc.plot = FALSE)
options(device = function(...) {
httpgd::httpgd()
.vsc.browser(httpgd::httpgdURL(), viewer = "Beside")
})
}
I would still consider this journey a work in progress, so here are some additonal tasks I hope to complete. Contributions and feedback are more than welcome!
.Rprofile
file created by renv
so that I do not need to manually copy over the customized version.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.