6  R, RStudio and VSCode on WSL

6.1 Setting up R on WSL

Now that your system is up-to-date, you can proceed with installing R. To do this, run the following commands in WSL:

sudo apt install r-base

The r-base package is a basic R package that will install R onto your system. The installation may take a few minutes to complete.

After installation, you can verify that R was successfully installed by checking the version. Enter R by just typing R and then version.

If the installation is successful, it should return information about the R version installed on your machine.

6.1.1 Install Packages in R

R’s functionality can be extended with packages. They are collections of R functions, data, and compiled code that can be used to perform specific tasks.

To install R packages, follow the steps below:

  • Open R in the WSL terminal by typing R.
  • Once you’re in R, you can install a package using the install.packages function.

For example, if you want to install the tidyverse collection of packages:

install.packages('tidyverse')

After the installation process is complete, you can load the tidyverse packages using the library function:

library(dplyr)

6.2 Setting up RStudio on WSL

While the terminal provides all the functionality needed to run and manage R scripts, many users prefer a more visual interface. This is where RStudio comes into play. However, WSL doesn’t support GUI applications, so we’ll have to install RStudio Server, which allows us to access RStudio via a web browser.

  • First, download the RStudio Server package using the WSL Terminal:
wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.4.1106-amd64.deb

Here, we are using the wget command to download the .deb package for RStudio Server. Replace the URL with the most recent version available on the RStudio download page.

  • Next, install it using dpkg:
sudo dpkg -i rstudio-server-1.4.1106-amd64.deb

If the dpkg command fails due to missing dependencies, you can install them with:

sudo apt-get install -f
  • Start RStudio Server by running:
sudo rstudio-server start

Finally, access RStudio Server by opening a web browser and going to localhost:8787. You can log in using your WSL username and password that you set up earlier.

6.3 Using VSCode, R and WSL:

You can leverage the power of Microsoft’s Visual Studio Code (VSCode) as your integrated development environment (IDE). Please note that this guide assumes that you have already installed WSL, Ubuntu on WSL, and R within WSL, as discussed in the previous steps.

6.3.1 Install VSCode

Install VSCode on the Windows side (not in WSL) here: https://code.visualstudio.com

Note: When prompted to Select Additional Tasks during installation, be sure to check the Add to PATH option so you can easily open a folder in WSL using the code command.

6.3.2 Install the Remote - WSL Extension

Once you have VSCode installed, you will need to install the Remote - WSL extension. This extension allows VSCode to interface directly with WSL, allowing you to write, run, and debug code stored within your WSL environment.

To install the Remote - WSL extension:

  • Open VSCode.
  • Click on the Extensions view icon on the Sidebar (or use the shortcut Ctrl+Shift+X).
  • Search for Remote - WSL or WSL.
  • Click on the Install button.

After installation, you’ll see a green button on the bottom left corner indicating that the Remote - WSL extension is installed and active.

6.3.3 Connect to WSL and Install the R Extension

Now that you have the Remote - WSL extension installed, you can connect VSCode to your WSL environment:

  • Open the Command Palette using the shortcut Ctrl+Shift+P.
  • Type WSL and select Remote-WSL: New Window.
  • This will open a new VSCode window connected to your WSL environment.

Once you’re connected to WSL, you should install the R extension in this VSCode window. This extension provides support for the R language, including features like syntax highlighting, code navigation, and even debugging.

To install the R extension:

  • Click on the Extensions view icon on the Sidebar (or use the shortcut Ctrl+Shift+X).
  • Search for R.
  • Click on the Install button.

Remember, you must install the R extension while you are connected to your WSL environment, not on your local Windows VSCode.

6.3.4 Start Using R in VSCode

You can now write and run R code using VSCode in your WSL environment. You can create a new R script by creating a new file with the .R extension.

To run the code, you can use the Ctrl+Enter shortcut to run the current line or selected lines in the R terminal.

Remember, since this setup is running within WSL, it can interact with any other software or files that are also within your WSL environment. This means you can easily incorporate other Linux-based tools into your R projects.

For more shortcuts and information, visit: https://code.visualstudio.com/docs/remote/wsl