How to run R codes in Visual Studio Code? A Step-by-Step Guide
Visual Studio Code is a free and open-source code editor developed by Microsoft. It is a versatile editor that supports a wide range of programming languages, including, but not limited to, R, Python, SQL, JavaScript, TypeScript, Java, C#, and many others. The software provides a unified and user-friendly interface for developers working with different languages, making it a popular choice across various programming communities.
Now, I’m working on my SQL code in Visual Studio Code, and recently, I’ve also been working on R code in Visual Studio Code. Today, I’ll be introducing how to run R code in Visual Studio Code.
The general guide provided by Visual Studio Code is helpful, but it may not always be sufficient. Therefore, I will guide you on setting up R for use in Visual Studio Code.
https://code.visualstudio.com/docs/languages/r
1) Download Visual Studio Code and install R
First, download Visual Studio Code.
https://code.visualstudio.com/Download
Open the application, navigate to the Extensions tab, and install the R extension. Ensure that the R program is installed on your PC.
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code1-1024x884.jpg)
2) Setup R path
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code2-1024x907.jpg)
Next, set up the R path in Visual Studio Code. In the R Script, running R.home("bin")
will reveal the R path.
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code3.jpg)
However, note an important point: the path should be written using \\
and include R.exe
at the end. Type this formatted pathway into Visual Studio Code.
C:\\Users\\kimjk\\AppData\\Local\\Programs\\R\\R-4.3.1\\bin\\x64\\R.exe
With these steps completed, R coding is now activated in Visual Studio Code. Proceed by installing languageserver
in R.
install.packages ("languageserver")
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code4-1024x623.jpg)
Now, everything is set up, and you can run R codes seamlessly in Visual Studio Code.
3) R coding in Visual Studio Code
Now that everything is set up, you can run R codes in Visual Studio Code.
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code5.jpg)
4) Changing shortcut keys
Here’s a helpful tip! You can choose your own shortcut key to run codes. Navigate to setting icon, select Command Palette
, and search for ‘R selection.’ You’ll find Run Selection/Line
. Click on the setting icon to the right, then double-click the line. Now, you can choose your preferred shortcut key to run codes.
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code6-1024x666.jpg)
![](https://agronomy4future.org/wp-content/uploads/2024/02/Visual-Studio-Code7-1024x479.jpg)
![](https://agronomy4future.org/wp-content/uploads/2023/04/cropped-cropped-Logo-1-1024x513.jpg)