Logo with initials
Click me x5 ↓

How to install and use R in VSCode

Last updated: March 20, 2023

Most of the R world uses RStudio as the default IDE, which I cannot stand. It’s ugly, slow and cumbersome.

RStudio is owned by CRAN (a package registry) and it also manages your installation of the R language itself. That makes troubleshooting hard. The default answer is often “Have you tried reinstalling R?”

This isn’t 2003, I prefer:

  1. Reproducible installation steps
  2. A modern coding editor (VSCode)

Here’s how to do that on macOS.

1. Install R itself

Use Homebrew on macOS or Linux to download and install the language. (Good luck Windows). You do not need to install RStudio.

brew install r

That will download R and install the correct version for your operating system (depending on whether it uses Intel, or Apple Silicon, etc). It will install Python v3 too, because why not.

2. Install dependencies

We have to install the Language Server with pkg, the R package manager.

Optional: Install Radian, which is a pretty R console: pip3 install -U radian Optional: If you haven’t already, install Visual Studio Code itself with a download or by running brew install --cask visual-studio-code.

Install the language server

  1. Open a terminal
  2. Run R by typing r to get into the console.
  3. Install the language server by typing this:
    install.packages("languageserver")
    
    It’s going to take forever because it’s doing some C++ compiler stuff like it’s 1995 …
  4. Get out of this ugly console: q()
  5. Close your terminal

3. Make it work with VSCode

  1. Open VSCode.
  2. Install the R Extension for VSCode
  3. Add the following properties to your settings.json
"r.bracketedPaste": true,
"r.rterm.mac": "/usr/local/bin/radian"

Enjoy!

Create an R file, then write some R code in it.

print("Hello World!")

Click on the “Run source” icon on the top right. It looks like a green play button.

Watch it run on the terminal, you’re done!

If you’re looking for what to do next, I have another snippet for a basic world map with ggplot.

Share