Prerequisites

The R base must first be installed. On Windows, choose a CRAN Mirror and download the R for Windows (base) binary. Then, just launch the executable (.exe) to install the software. On Linux, R should be available for installation via the distribution’s package manager. For example, on Ubuntu, use the following commands1:

sudo apt update
sudo apt install r-base

Then, run R in the terminal. If R had been added to the PATH environment variable, all you need to do is type R and press enter to start the R console. This will be the case if R is installed using a Linux package manager. If you try to do this on Windows, you will probably get an error, unless you add R to the PATH variable manually or use the full path to the R executable, e.g. C:\Program Files\R\R-4.0.5\bin\R.exe.

In the R console, run the following to install the R language server:

install.packages("languageserver")

If the installation of any dependencies fails, then either run the command above again to install the remaining dependencies, or choose a different mirror. I had issues with the mirror closest to me, and choosing the second closest mirror solved the problem. To choose a mirror, you can run the following in the console2:

chooseCRANmirror()

Setting up text editors

There are a number of text editors to choose from, including R Studio. I prefer using Visual Studio Code or VSCodium as an all-in-one text editor.

To use R on VS Code, I installed the R LSP Client extension from the marketplace. If R was not added to the PATH environment variable on Windows, then its full path must be defined in the VS Code settings (note that the backslashes are escaped):

"r.rpath.windows": "C:\\Program Files\\R\\R-4.0.5\\bin\\R.exe"

You should now be able to use R in VS Code’s integrated terminal and write R scripts with linting in the editor. You may need to reload VS Code to ensure everything works.

Setting up terminals

Apart from VS Code’s integrated terminal, I use two standalone terminals: Konsole and Windows Terminal.

On Konsole, a new profile for R can be set up easily in Settings.

On Windows Terminal, click on the dropdown arrow, and click on ‘Settings’, which will open settings.json in the default text editor.

In this file, there will be a list of unique profiles for the terminal, the defaults being Windows PowerShell, Command Prompt, and Azure Cloud Shell. I added the following to the end of the list (see Microsoft Docs3 for more information):

{
  "guid": "{c65d5554-4917-4f11-afc3-9939795dd4c6}",
  "name": "R",
  "commandline": "C:\\Program Files\\R\\R-4.0.5\\bin\\R.exe"
}

A unique GUID must be assigned to each new item in the list. To generate a unique one for a new profile, run [guid]::NewGuid() in PowerShell4. A descriptive name should also be assigned in the name key, e.g. “R”. The (backslash-escaped) R path is added to the commandline key.

After saving settings.json, the R prompt will appear in Windows Terminal’s dropdown menu.

R with JupyterLab via Anaconda

R can also be run in Jupyter notebooks. One way to do this is by installing R and JupyterLab in an Anaconda virtual environment.

If Anaconda is not already installed on your system, install it (I recommend the lightweight Miniconda). Then, set up a virtual environment with the required packages and activate it5:

conda create --name r-env --channel conda-forge r-base r-essentials jupyterlab libgit2 r-devtools

Launch an R console in the same environment and run the following to install the R kernel spec6:

IRkernel::installspec()

Exit the R console and run jupyter lab. You will be able to use the newly-installed R kernel in an interactive notebook.

Footnotes