Setting up Lightweight R in VSCode Studio (miniforge3 + renv on Linux)
Just under a year ago now, I've switched to Linux.
Since then, I've been a lot more mindful of what I'm installing and why I'm installing anything on my computer. Since any installation process is more painful on Linux, I might as well go the extra mile to customise my R workspace.
Below, I've documented how to setup a lightweight working environment for R using Miniforge and Renv in VSCode Studio; and all the things I learned along the way! I'll explain concepts and define terms as I go on.
I'm on a ThinkPad X1 with Linux Mint. This shouldn't be too bad.
Installing R with Miniforge
Miniforge Instead of Anaconda/Miniconda
When working with multiple languages, its good coding hygiene to give each project its own virtual environment. An environment is an isolated workspace that contains the required and specified dependencies used to run the working code. These dependencies could be, for example, a specific library (which is a collection of pre-programmed functionalities) or a specific package (which is a bundled container of one or more libraries, including the metadata to distribute, install, and update them). To use any library, its package must be installed first.
Miniforge (or miniforge3) is a lightweight manager for installing packages and creating environments that can support multiple languages (i.e. R, Python, C, Julia, etc.). Its the open-source community-maintained alternative to Miniconda, which are under proprietary license of Anaconda.
If I want my code to work beyond my local computer (AKA reproducible) — its gotta exist in a portable environment.
Unlike MiniConda/Conda, Miniforge comes with both the conda and mamba installers, which both can access its default conda-forge channel (AKA the shared repository where packages of libraries are hosted and retrieved from for installation and updates). For modern Linux users (including Raspberry Pi OS), the conda-forge channel is Linux-native with pre-built packages specifically compiled for the linux-aarch64. It can also access any proprietary packages, only available via the Anaconda's default channel, because of its conda installer.
So, Miniforge provides a more robust ecosystem of available libraries because conda is basically the defacto standard in scientific research and mamba is widely used among open-source communities.
By choosing miniforge, I don't miss out on anything miniconda could offer.
- Install Miniforge via the GitHub repo:
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh
Initialise it:
bash Miniforge3-Linux-x86_64.sh
# And then, enter 'y'when prompted to initialise miniforge
Optional: Verify the installation by checking which version of conda or mamba got installed
conda --version
mamba --version
(If a package is available via the conda-forge channel, you can use either mamba or conda to install it. I recommend mamba since its faster than conda.)
Preventing Global R Packages
Ever return back to an old R project and found it broken?
If all packages are installed at the computer's global environment (the user's home directory), all projects will share the same libraries. But, if one of those projects calls a function that only exists in an older version of one of those libraries — that project will no longer work. This is known as package conflicts! These can be easily avoided by giving each project its own environment.
Unfortunately, even if I have installed R inside its own isolated conda environment, R's install.packages() will default install the package in the home directory anyway! If R packages will always install at the global level, this defeats the purpose of cleanly organising my packages per project environment to prevent any future update conflicts.
Fortunately, there is a way around this!
Rather than replicating the same conda R environment for every project , I will use renv .
Renv can build multiple isolated R virtual environments layered on top of the conda base. Thus, this method of combining renv + miniforge3 avoids unnecessarily running into package conflicts between new/old projects AND accumulating multiple instalments of R and the whatever essential packages per project.
- Set up a conda environment for global R
Create a yaml file to document the R environment's dependencies:
nano r_base.yml
name: r_base
channels:
- conda-forge
dependencies:
- r-base
- radian
- r-httpgd
- r-languageserver
- r-tidyverse
Create the conda environment:
mamba env create --file r_base.yml
Note: Although its not necessary to make a yaml file, as these dependencies can just be listed as a one-liner alongside
mamba env create; its useful because it keeps track of the core packages to be reused on another machine and can be easily updated moving forward!
mamba env update --file r_base.yml
What are these packages?
r-base→ R language itself, includes interpreter that runs the R code.radian→ R terminal console, instead of the defaultR>prompt.r-httpgd→ R plot viewer inside VSCode, without it, plots open in a separate window or not at all.r-languageserver→ R language reader inside VSCode, without it R files display as plain text.r-tidyverse→ An R package that includes libraries for standard data science use, including:dplyr→ data manipulationggplot2→ static data visualizationtidyr→ reshaping datareadr→ reading CSV and other filesstringr→ working with textpurrr→ functional programming
Activate the environment:
mamba activate r_base
- In VSCode, set up R Extension
In the VSCode's extension panel, search and install the R extension byREditorSupport.
Open up VSCode's settings, and search for:
- r.plot.useHttpgd → toggle this on to active the httpgd R plot viewer
- r.rterm.linux → set the terminal path to use the radian console inside the conda environment
(/home/<user>/miniforge3/envs/r_base/bin/radian) - r.rpath.linux → set the R path to use it inside the conda environment (
/home/<user>/miniforge3/envs/r_base/bin/R)
- **Install renv inside the conda environment **
R -e "install.packages('renv')"
Using Renv per Project Directory
For setting up any new R project:
- Activate the conda environment
- Make a new folder (directory) for the project, and then navigate to it
mkdir ~/Projects/<project-name>
cd ~/Projects/<project-name>
Note: Since this is a Linux guide, every Linux user has their own folder system for how they keep and store projects. Personally, its just easier to navigate to a folder at the user's home directory, because its always just
~/!)
- Initialise renv inside the project's working directory (folder)
R -e "renv::init()"
OR, if using the radian terminal in VSCode
renv::init() # start
install.packages(<insert-whatever-package-here>) # use it like normal without worry of global package installation!
renv::snapshot() # save
For returning to any previous R project:
- Activate the conda environment
- Navigate to the project's existing folder (directory)
- Restore its renv:
renv::restore() # reinstall exact packages from renv.lock
OR, open a folder
- Open your project folder in VSCode
- Open the VSCode terminal (
Ctrl+``) - Type
Rand it launches radian automatically
By doing this, I'm effectively giving each project's working directory its own virtual R environment. All of these projects will be living inside my conda environment, which is named r_base.
Conclusion
Renv is really useful.
I wish these hygiene habits to manage and standardize developing environments were taught in R classes. It could've made me more mindful of 'how to use my computer', without going down the nuclear option of installing Linux.
The combination with VSCode and miniforge is complete overkill. The only advantage of this method is its much more lightweight than installing an entire IDE dedicated to one language, which isn't even my primary.
Regardless, Rstudio and renv would have been enough to contain R from spilling out to the rest of my desktop.
But...its too late. I've already done it.
References
- https://www.anaconda.com/docs/getting-started/working-with-conda/packages/using-r-language
- https://code.visualstudio.com/docs/languages/r
- https://www.codydehaan.com/blog/vscode-miniconda-r/
- https://www.codegenes.net/blog/using-r-in-visual-studio-code-with-conda-environment/
- https://byteartisan.substack.com/p/running-r-in-visual-studio-code-a
- https://stackoverflow.com/questions/61967950/whats-the-difference-between-install-a-r-package-with-conda-and-with-install-pa