← Other topics

Simple setup for R in VSCode instead of RStudio (Mac)

Video Notes

Let’s learn how to configure VSCode to work with R. As a bonus, we’ll also set up Radian, an alternative console for R that provides useful features like multiline editing and syntax highlighting. This guide is specific to Mac but the information could be adapted to Windows.

Why Use VSCode for R Instead of RStudio?

Here are some reasons you might prefer working with R in VSCode:

  • Versatility: VSCode is widely used across many different languages, so if you work with other languages, you don’t have to “context switch” to a different editor (RStudio) just to work with R.
  • Performance: VSCode is faster than RStudio.
  • AI Integration: VSCode has better support for integrating AI assistance tools.

Step 1) Confirm R installation

If R is already installed on your system, you can verify this by opening Terminal (Mac’s command line program) and running the command which R (note the capital R):

Note the install path (/usr/local/bin/R) as we’ll need it in later.

If this command returns an error (e.g. command not found), you need to install R. To do this, visit https://cran.r-project.org/bin/macosx to download the R installer. After installation, close and reopen Terminal, then rerun the which R command to confirm the installation path.

Step 2) Install languageserver package

Next, we’ll install an R package called languageserver which implements the Language Server Protocol (LSP) for R. LSP allows editors (like VSCode) to provide language-specific features such as code completion, linting, syntax highlighting, and more.

To install R’s languageserver package, open Terminal and start the R console with the R command. At the R prompt, install the package with this command:

install.packages("languageserver")

To see all currently installed packages, use the command installed.packages().

Exit the R console by running q(). When prompted to save the workspace you can type n.

Step 3) Install and configure "R" extension in VSCode

Open VSCode and go to the Extensions Marketplace. Search for and install the extension "R" by REditorSupport.

Once installed, open your VSCode settings.json config file and add the following settings:

"r.rterm.option": [ 
  "--r-binary=/usr/local/bin/R", // Replace with your R path from Step 1
  "--no-save", // Optional: Prevent saving workspaces
  "--no-restore", // Optional: Prevent restoring workspaces
],

Setting --no-save and --no-restore is optional but suggested because it encourages reproducibility with our scripts as we won’t be relying on saved workspaces.

Step 4) Test it

To test things out, create a simple R script called demo.R:

example <- 123

example_data <- data.frame(
  ID = 1:10,
  Age = sample(18:50, 10, replace = TRUE),
  Score = round(runif(10, 50, 100), 1)
)

hist(example_data$Age,
  main = "Histogram of Ages",
  xlab = "Age",
  ylab = "Frequency",
  col = "lightblue",
  border = "black"
)

Run the script via one of the following options:

  • Line or Block Execution: Place the cursor on a line or block of code and press command + return to run just that line or block.
  • Shortcut: Press command + shift + S to source (i.e. run) the entire file
  • Run Source Button: Click the the Run Source (play icon) button on the top right corner.

After running the code, open the R pane from the activity bar (on the left) to explore your workspace, including namespaces and environment variables. Hover over a variable to delete or view it.

Bonus Step: Set up Radian

Radian is an alternative console for R with enhanced features like multiline editing and syntax highlighting.

To set up Radian and make it available within VSCode, you first need to install it. While Radian is made for R, it’s built with Python so we install it using pip, Python’s package manager. To do this, run the following command in Terminal:

> pip install -U radian

If the above command reports back that pip is not available, you can install it by installing Python via the following command:

> brew install python

Once Radian is installed, you can access it in Terminal by running the command radian in place of r.

Our goal, however, is to access Radian via VSCode. To do this, exit the radian prompt with the command q() to return back to your normal Terminal prompt. Then, run the command which radian to identify the path to your Radian installation.

Back in your VSCode settings.json file, add the following settings:

"r.rterm.mac": "/path/to/radian",  // Replace with the Radian path from above
"r.bracketedPaste": true  // Enables chunked code execution

Close the Terminal window in VSCode and re-run your demo.R file to prompt a new Terminal pane to open using the Radian console.

Bonus tip: Open Plots in a Separate Window

To display plots in a new window instead of inline, add the following setting to your settings.json file:

"r.plot.useHttpgd": true
← Other topics