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.
2) Setup R path
Next, set up the R path in Visual Studio Code. In the R Script, running R.home("bin")
will reveal the R path.
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")
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.
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.