How to integrate Python and R in Visual Studio Code

Maurice Henry Buettgenbach
Towards Data Science
7 min readNov 5, 2020

--

I have recently started the IBM Data Science course on Coursera and found myself struggling to install the required versions of Python and R in my VSCode. As it took me an frustrating evening and a couple of whiskys to find a working solution for my Windows system, I decided it would be best to share it and save you the pain of searching the web for the right pieces.

What do you need?

  1. Visual Studio Code from Microsoft
  2. Python language
  3. Python extension by Microsoft
  4. Python Extension Pack by Don Jayamanne
  5. R language
  6. R languageserver package by Randy Lai and Kun Ren
  7. VSCode R extension by Yuki Ueda
  8. Radian by Randy Lai

Step 1: Install Visual Studio Code

VSCode is generally available for free but further options (e.g. Azure DevOps Server) can be purchased via a professional or enterprise licence. However, the free version is in my opinion more than sufficient for personal use.

To install the free version of VSCode you will need to download the execution file for your respective plattform from the official Microsoft website first.

After the download launch the execution file and follow the installation guide. The installation should not require any admin rights. Afterwards launch VSCode and the home screen will pop up.

VSCode home screen. Image by author.

Step 2: Install Python language

To install Python within VSCode you first need to download and install a Python interpreter. Which interpreter you need depends on your specific requirements, however, you can find all available interpreters on the official download page of Python. At the time I am writing this story version 3.9.0. is the latest.

Once you have downloaded the execution file, open it and you will see the following window.

Python 3.9.0 installer. Image by author.

If you do not have any administrator rights for the machine you use, make sure you deselect the option “Install launcher fo all users (recommended)” before clicking “Install Now”.

To check whether your installation was successful, open the command console and execute the following command:

py -3 --version

After entering the command the following output should be visible:

Windows command console showing the current Python version. Image by author.

Step 3: Include Python in your VSCode

Open your VSCode and open the extensions tab by pressing Ctrl + Shift + X. In the search bar type in “Python”, select the Python extension by Microsoft and install it.

Python extension pack by Microsoft. Image by author.

Theoretically, you are now ready to code in Python via VSCode. However, I would highly recommend to install the Python Extension Pack by Don Jayamane before. It includes further extensions that just make your life easier by adding different syntax highlight options and AI based recommendation for your completion items. To install Don’s extension pack, just type in “Python Extension Pack” in the search function and click install.

Python Extension Pack by Don Jayamanne. Image by author.

To start a new Python project, press Ctrl + Shift + P to open the command line in VSCode and type “Python: Create New Blank Jupyter Notebook”. Press enter and VSCode will open a new Jupyter Notebook for you:

Jupyter Notebook in VSCode. Image by author.

If you want to switch between different Python interpreters, you do not need to in- or uninstall different versions. You can simply link your different Python interpreters within VSCode and then switch between them as you please. To do so, press Ctrl + Shift + P to open the command line and type “Python: Select interpreter”. VSCode will show you the currently active interpreter and other options. To add a new interpreter, simply copy the path to the respective execution file and hit enter.

Python interpreter selection command in VSCode. Image by author.

Step 4: Install R language

First, you will need to go to the official website of the R-Project and select a CRAN server location near you. Once you selected the download for Windows, click on “install R for the first time” and then “Download R 4.0.3 for Windows”. The version number might vary, depending on the time you read this article.

Download page of the R-Project version 4.0.3 for Windows. Image by author.

Everyone with administrator rights can just execute the downloaded file, follow the installation instructions and skip to Step 5. For everyone without administrator rights, you will need to do the following.

Open a file window and go to your user’s folder located on your C-drive. Here, create a folder where you can unpack the installation. I have named mine “R”.

C:\Users\<user_name>\R

Once you have created your folder, install R normally but choose the newly created folder as the installation location instead of the default. The installer will create a new folder under your location named:

C:/Users/<username>/R/R-<version number>

Next, create a new folder under your “R” folder and name it “R-library”:

C:\Users\<username>\R\R-Library

Afterwards navigate to the R execution file and open it:

C:\Users\<username>\R\R-<version number>\bin\R.exe

Once you ran the R terminal, enter the following code and copy the directory.

path.expand("~")

This will tell you what directory R is working in when launched. In my case it looks like the following:

[1] "C:\\Users\\maurice.buettgenbach\\Documents"

Open the respective directory and create a new textfile with the name “.RProfile”. You can do that via the Notepad app but make sure that the file does not append with the classical “.txt” extension. Paste the following code into the file and exchange the file path with your user folder for the R-Library:

.First <- function() {
path_to_libraries <- "C:/Users/<username>/R/R-Library"
.libPaths(c(path_to_libraries, .libPaths()))
}

R will run the .First () function now at the beginning of every session and execute the .libPaths() function which tells R to install and access packages in your R-Library folder. Close R before continuing.

Step 5: Integrate R into VSCode

To make R integration in VSCode possible, we have to install the languageserver protocol by Randy Lai and Kun Ren first. The LSP for R provides the framework under which the language is able to communicate with our chosen editor.

To install the package, open R and enter the following command in the console:

install.packages(“languageserver”)

Next, open VSCode and install the “R” extension by Yuki Ueda. This extension adds shortcuts and enables you to run code directly in your workspace as well as view dataframes more easily. It now also includes functions that support code syntax and highlighting (similiar to the old LSP Client).

R extension by Yuki Ueda. Image by author.

The last thing you need to install now is Radian by Randy Lai. Radian is an alternative console for the R program with multiline editing and syntax highlighting. As Radian is written in python, you can easily install it via the Python pip command. To install Radian, open a Jupyter Notebook in VSCode and execute the following command:

pip install -U radian
Executed pip install command for -U radian in VSCode. Image by author.

Finally, you need to change your VSCode settings to enable R within the editor. For that press Ctrl + Shift + P and type in “Preferences: Open Settings (JSON)” and press Enter. This will open the settings.json of your editor. Add the following code to the JSON file, adjust the respective path to your setup and save the file.

"r.bracketedPaste": true,"r.rterm.windows": "<Path_to_radian_.exe","r.lsp.path": "<Path_to_R_.exe>","r.lsp.debug": true,"r.lsp.diagnostics": true,"r.rterm.option": ["--no-save","--no-restore","--r-binary=<Path_to_R_.exe>"],

Here is how it looks like in my settings.json:

Settings.json of my personal VSCode. Image by author.

Afterwards close the settings file and open your explorer. To test whether everything has worked correctly, create a test file and make sure it has the respective “.r” ending. Open the file with VSCode and type:

print("Hello world", quote = FALSE)

You can now run the code by pressing Ctrl + Shift + S. In result, you should receive “Hello world” as an output in your terminal as shown in the next picture. You are now ready to use R within VSCode!

Executing R code in VSCode. Image by author.

--

--