Running R in Visual Studio Code: A Lightweight and Customizable Alternative to RStudio
Seamlessly integrate R and Python in an isolated Conda environment using VS Code, without compromising essential RStudio features
Introduction
As a data science student, taking statistics modules and using R programming is inevitable. While modern data science is predominately Python, R programming is still preferred by statistician; and for good reasons. The ongoing debate between the 2 languages will never be short of advocates on both sides.
Conventional teaching materials often recommend installing R from CRAN and using RStudio as the IDE. However, running R in Visual Studio (VS) Code offers several advantages. First, installing R in an isolated environment managed by Conda provides cleaner dependency management. If any issues arise, the environment can be easily deleted and re-created without affecting the underlying operating system. Second, using VS Code eliminates the need to install another IDE specifically for R, saving disk space. Lastly, users can take advantage of VS Code’s ability to seamlessly edit and execute R and Python, allowing them to harness the strengths of both languages.
RStudio User Experience
Running R in VS Code does come with some trade-offs, as it can't fully replicate all of RStudio's features. However, it does cover most of the commonly used functionalities. This article defines the RStudio User Experience with the following list of features.
An R editor and the ability to run selected code on console
An environment to view variables
A console to enter and execute code
An output to view plots, files, and tutorial
Image from docs.posit.co
Setup Guide
As a pre-requisite, ensure conda, the environment manager, has been installed. Miniconda is preferred over Anaconda, as it is lightweight, and gives better control over what is installed. Refer to youtube video for Miniconda installation guide.
1. Creating an isolated environment for R
Create an isolated environment with the desired R version. This is equivalent to the conventional method of installing R, from CRAN, onto the OS directly. In this example, we are creating an isolated environment with R version 4.4, named myenv
.
To ensure the installation is done correctly, verify the location and version of the installed R.
Check if conda-forge already exist in conda channels. If it is not, add it. conda-forge is a community-driven repository, otherwise known as channel, for conda packages. It provides a wide variety of packages that work across Windows, macOS and Linux. R packages can be installed from conda-forge and they are prefixed with r-
.
# Create and activate isolated environment
conda create -n myenv r-base=4.4 -y
conda activate myenv
# Verify R installation and version
which R # Output: /Users/jonaslim/miniconda3/envs/myenv/bin/R
R --version # Output: R version 4.4.1
# Show and add conda-forge channel
conda config --show channels
conda config --add channels conda-forge
coond config --show channels
2. Install essential packages in isolated environment
Install the essential R packages into myenv
. It contains the commonly used packages such as r-ggplot2
, r-plotly
, r-tidyverse
, r-dplyr
.
conda install r-essentials -y
Open an R console from the terminal and install languageserver
and httpgd
.
languageserver
is an R package, which implements the Language Server Protocol for R. It tells VS Code how to communicate with R interpreter to provide code completion, syntax checking, linting, definition, hover documentation, and refactoring tools, much like the experience in RStudio.httpgd
is an R package, which provides a graphic devices to render R plots within VS Code. In a nut shell, it’s needed to display R plots directly within VS Code.
R
install.packages("languageserver")
install.packages("httpgd")
[Optional] Install radian
, in myenv
, an alternative console for R programming. It has rich syntax highlighting and multiline editing which makes the developer experience much better than the vanilla R console.
conda install radian
Add the following alias and export to bash(Linux)/zsh(macOS) shell configuration. For bash, the config file is at ~/.bashrc
, while for zsh, ~/.zshrc
. Replace jonaslim
and myenv
with your respective user directory and isolated environment name.
# R setup
alias r="radian"
export PATH="$PATH:/Users/jonaslim/miniconda3/envs/myenv/bin"
3. Install VS Code Extensions
Install the following VS Code extensions from Extensions:
R by REditorSupport (aka vscode-R)
R Extension Pack by Yuki Ueda (It has R LSP included)
R Debugger by R Debugger
Path Autocomplete by Mihai Vilcu
4. Configuring VS Code Preferences
From VSCode Command Palette (Cmd + Shift + P in macOS), select “Preferences: Open User Settings (JSON)”.
Find the relevant keys, as displayed below, in the json file and modify it. If it does not exist, add it. Replace jonaslim
and myenv
with your respective user directory and isolated environment name.
"files.associations": {"*.rmd": "rmd"}
"r.libPaths": ["/Users/jonaslim/miniconda3/envs/myenv/lib"],
"r.plot.useHttpgd": true,
"r.bracketedPaste": true,
"r.rpath.mac": "/Users/jonaslim/miniconda3/envs/myenv/bin/R",
"r.rterm.mac": "/Users/jonaslim/miniconda3/envs/myenv/bin/radian",
"r.rterm.option": [
"--no-save",
"--no-restore",
"--r-binary=/Users/jonaslim/miniconda3/envs/myenv/bin/R"
],
files.associations
ensure code chunks in Rmarkdown files are recognized. r.bracketedPaste
is needed for Radian. r.rterm.mac
and r.rterm.option
defines radian as the default R terminal. r.libPaths
indicates the path to libraries.
Comparing VS Code and RStudio
✅ An R editor and the ability to run selected code on console
✅ An environment to view variables
✅ A console to enter and execute code
✅ An output to view plots, files, and tutorial
Conclusion
Running R in VS Code provides a more lightweight and customizable environment compared to RStudio, while still covering most of RStudio’s core features. Additionally, it grants access to a wide range of useful extensions from the VS Code marketplace.
By consolidating all development within VS Code, users can seamlessly integrate R with other languages in the same project. In contrast, RStudio is limited to running R.
However, there are some drawbacks. R packages available in conda-forge may not always be as up-to-date as those in CRAN. By default, VS Code can only point to the R or Radian interpreters specified in the user preferences JSON file. Nevertheless, users can manually activate the desired R environment within their isolated Conda setup.