Visual Studio Code (VS Code) is a source code editor that is popular for its versatility, lightness, and customizability. If you work with R and want to use VS Code as your development environment, this tutorial will walk you through the step-by-step setup of VS Code so you can run R code efficiently.
Step 1: Install R
Before using R in VS Code, make sure you have R installed on your computer. If you don’t already have it, follow these steps:
Follow the instructions to install R on your computer.
- Go to the official R website and download the appropriate version for your operating system (Windows, macOS or Linux).
- Follow the instructions to install R on your computer.
Step 2: Install Visual Studio Code
If you don’t have VS Code installed, follow these steps:
- Install VS Code by following the instructions provided.
- Go to the Visual Studio Code download page and download the version for your operating system.
Step 3: Install the R extension in Visual Studio Code
To work with R in VS Code, you need to install an extension that allows you to integrate R functions into the editor. Here’s how:
- Open Visual Studio Code.
- Go to the Extensions Marketplace by clicking the extensions icon in the left sidebar or by pressing
Ctrl + Shift + X
. - Look for the extension called R developed by Yuki Ueda. This extension provides you with basic functions such as running R scripts.
- Click Install.
Step 4: Configure R in VS Code
Once the R extension is installed, you need to make sure that VS Code recognizes the location of R on your system.
1. Open the VS Code settings (you can click the gear icon in the bottom left corner and select Settings, or press Ctrl + ,).
2. In the settings search bar, type “r.rterm.windows”, “r.rterm.mac”, or “r.rterm.linux” depending on your operating system.
3. Enter the path to the R executable file on your system. For example: On Windows, it’s usually in: “C:\\Program Files\\R\\R-x.x.x\\bin\\R.exe” On macOS and Linux, you can simply type “R” if you already have R set up on your terminal.
4. Save your settings.
Step 5: Install additional extensions for a better experience
If you want to improve your workflow with R in VS Code, you can install some additional extensions:
- R LSP Client: Provides code autocomplete, error highlighting, and debugging functions. To install this extension:
- Open the Extensions Marketplace.
- Search for R LSP Client and click Install.
- To make it work, you also need to install languageserver, an R package. You can do this by running this command in the R console:
install.packages("languageserver")
- Rainbow CSV: This extension helps visualize CSV files with different colors for columns, which is useful if you work with tabular data.
- Bracket Pair Colorizer 2: Makes the pairing of brackets and parentheses clearer by coloring them differently.
Step 6: Run R code in Visual Studio Code
Once everything is set up, you can start writing and running R code.
- Create a file with the .
A
. To do this, select File > New File and save it asa script. A
. - Type your R code into the file. For example:
print("Hello, R from VS Code!")
- To run the code, select the line you want to run and press
Ctrl + Enter
. This will send the code to the R console that will open in the VS Code terminal.
Step 7: Use an interactive environment with R (Radian)
To enhance the R console in VS Code, you can use Radian, which is an alternative interpreter for R with additional features such as code autocomplete and improved colors. To install it:
- Open the terminal on your computer.
- Install Radian using
pip
, the Python package manager:pip install radian
- Then, set VS Code to use Radian instead of the default R console. Go to the settings (
Ctrl + ,
) and search for"r.rterm.windows",
"r.rterm.mac"
or"r.rterm.linux"
and change the path toradian
or the path to the radian executable.
Step 8: Debugging and Using Graphics in VS Code
- Debugging: If you have the R LSP Client extension installed, you can use debugging features as breakpoints to inspect the state of your code.
- Graphics: Graphics generated with R will appear in a separate window, but if you want them to appear within VS Code, you can use the R Plot Viewer extension.
Step 9: Customize the environment
You can customize the development environment by adjusting themes, fonts, and keyboard shortcuts to make your experience more enjoyable and efficient. Find the settings in the Settings menu (Ctrl + ,
).
