Introduction

This article provides a tutorial about using R in VS Code through SSH server, the processes are normally the same when you use R in your local computer.

Install Visual Studio Code

Use the official Visual Studio Code website to download and install Visual Studio Code matching your local computer system.

Connect to SSH Server

Add SSH host in Visual Studio Code using this extensions

Install R

Use the official R website to download and install R matching your server system.

Install Python

Use the official Python website to download and install Python matching your server system.

Install Extensions

Install the extensions in the following picture in Visual Studio Code

Install extensions: Python, Pylance, R, R Debugger

Note: If you are trying to use R in Visual Studio Code through SSH servre, please install all the above, including R, Python and extensions, and the following R packages, Python package, in your SSH server environemnt.

Install Required R package

Open R environment in the terminal in your Visual Studio Code. If you don’t know the R version in your computer, just type R then click TAB on your keyboard twice, it will return the R version in your system, if it returns R-4.1.2, then type R-4.1.2 in the terminal, you will enter a R environment.

install.packages("languageserver")
packageVersion("languageserver")
install.packages("rmarkdown")
packageVersion("rmarkdown")
install.packages("httpgd")
packageVersion("httpgd")

Enable r.plot.useHttpgd in Visual Studio Code settings: Open Visual Studio Code setting, copy r.plot.useHttpgd to the search bar, then check the box.

Install Radian

Radian is highly recommended as the R terminal for interactive use. It requires Python which should be available on most Linux distributions out of the box. To install it globally:

pip install -U radian

or

pip3 install -U radian

Or if you don’t have root privilege, you may want to install it only for yourself:

pip install --user radian

or

pip3 install --user radian

Enable r.bracketedPaste in Visual Studio Code settings: Open Visual Studio Code setting, copy r.bracketedPaste to the search bar, then check the box.

Environment Setting

Type the following commands in the termial: Suppose your R version in your environemnt is R-4.1.2

which R-4.1.2
which radian

This will return the paths for R and radian. Suppose your R path is /home/user/.local/bin/R-4.1.2, radian path is /home/user/.local/bin/radian, then

Reminder: There may be three choices when you search above three things in Visual Studio Code setting, User, Remote, Workspace, if you are using R in Visual Studio Code through SSH server, select Remote, if you are using R in Visual Studio Code in your local computer, select User, then modify r.rterm.linux and r.rpath.linux matching your local computer system.

Other Option

You can also use the following code to install anaconda and use it to create a R environment, then following all the processes above.

wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
bash Anaconda3-2022.10-Linux-x86_64.sh
conda --version
conda create -n my-r-env r-base
conda activate my-r-env
R --version
R
q()
conda deactivate

If you want to enter bash automatically when you create a new terminal, you can search terminal.integrated.defaultProfile.linux, then select bash.

Reference

R Extension for Visual Studio Code

Installation wiki pages