I always love to learn new things. Right now, I’m experimenting with R and statistical computing. Additionally, I’m playing around with the new lightweight development environment from Microsoft, Visual Studio Code. So, this is a post that demonstrates setting up Visual Studio Code to run R applications.
Before you begin, you’ll need to download, both:
Both of these tools are platform agnostic. However, these instructions are specifically for the Windows environment.
First, of course, you’ll need to install them. Second, depending on what you chose in the installation of Rterm, you may have 32-bit, 64-bit or both versions installed. You can determine what you have installed by either remembering what you chose in the installation options, or checking out Rterm’s folder on your hard drive.
I installed Rterm on a 64-bit machine. Therefore, in the following image, you notice two things:
- The home installation path was located in “C:\Program Files” as compared to “C:\Program Files(x86)”
- I have a “x64” subdirectory in my Rterm’s bin folder.
If I am using Rterm on a 64-bit machine, I’ll use the “R.exe” in the “x64” subfolder. Otherwise, I’ll use the “R.exe” that is located in the bin folder. Either way, I need to remember the path.
Visual Studio Code is Folder-Oriented
Unlike a commercial release of Visual Studio that uses project and solution files, Visual Studio Code is based on folders. A “project” in Visual Studio Code is everything located in a given folder. Therefore, I’ve created a folder called “R” and placed that in my projects folder on my hard drive.
By right-clicking on the folder, I will see a context menu option reading “Open with Code.” Click on it and Visual Studio Code will appear with the “R” folder as your project’s main folder.
Under the “R” folder in Visual Studio Code, right-click and choose “New File.” Now name the file “test.r”. Let’s type in some R code:
We’ll now need to add Rterm as our task runner. To do this, simply press Ctrl+Shift+B, which will bring up a warning stating that “No task runner configured.” The warning will give you an option to “Configure Task Runner.” Go ahead and choose this option.
By choosing this, a couple of things are created for you:
- A .settings subdirectory
- A tasks.json in the new subdirectory containing json task configurations
You’ll notice on the first line, there are some variables/macros defined that can be used in our strings.
Delete everything in this file and replace with the following (NOTE: You will need to use Rterm’s executable path from above).
Notes:
- “version” is the version of the task
- “command” is the path of my Rterm executable
- “showOutput” tells Visual Studio Code to render the output (errors and program output) to the VS Code output window
- “args”
- “–no-restore” tells Rterm not to show the terminal window
- “–no-save” tells Rterm not to save the output of the R program
- “–quiet” tells Rterm not to show the Rterm licensing and help messages
- “–file=${file}” tells Rterm the path and file name of our R script (in this case, C:\…\test.r). This is one of the macros VS Code allows us to use in our settings.
Now, save your tasks.json file (Ctrl+S), and click back on the test.r source file. With the task runner configured, press Ctrl+Shift+B once more and you should see the output of our code.
We’re basically filling an array with three numbers. Then, we take the square root of all three numbers and store them into a new variable. Finally, we print the variable to the screen.
I got excited, then realized I’d need to install Code, not the version of VS that I already have installed. Wonder if there’s a way to do it in VS2012/13 (or other standard installation).
Also, how does it compare to using RStudio? Can you use other .Net goodness within R if you do this? (That is, does this make it into a ironR or anything else?)
Rahn,
From what I can tell from Visual Studio’s website on supported languages, VS20** (including 2015) may not support R natively. If it doesn’t, support may be available through an extension. Since VSC is not project- or solution-based, but rather directory-based, it has a little more liberty to support more languages (e.g. I can edit my WordPress site seamlessly in VSC, unlike VS 2013 that’s a little overkill).
As far as support for R and comparison to RStudio, VSC is not meant to be a replacement for RStudio (or even Rterm, for that matter). This post was really to show that VSC supports syntax highlighting of R source code (if your file ends in a *.r or *.rdata extension) – and that’s pretty much it at the moment. The intellisense support for R is really non-existent and I’ve not even tried graphing. As you see in this tutorial, I’ve set VSC to still use Rterm as the parser/compiler and display’s Rterm’s output in the VSC output pane. Long story short, there’s no compilation of your R code (yet?) and, therefore, no current integration with the .NET framework.
FYI, another reason I provided the tutorial was because I have some clients who are moving to using Azure for statistical computing. I wanted to give an “introduction” to using R in VSC.
Sorry to be the bearer of bad news, but hey, who knows where VSC will go? We’re still in version 0.5.0.
Thanks for you input!
This is amazing – visual studio is a much better experience than that is out there right now for R. Granted, it will not replace R Studio for me at present but who knows what we will see in the future. Especially since I remember that Microsoft became involved in R in some way.
hello, i have a problem in using the vsc , every time i press shift+command+b, all of the code will be run together. But, sometime i need to run some of the code separately, so, can you tell me how to do it?
Currently, you would need to create a specific task and tie a custom shortcut to it. Sorry, it’s not the best solution, but would work.
Great post!!!!
Also you can download “Microsoft R Open” from https://mran.microsoft.com/rro/
instead of Rterm.
and change the R directory in task.json like that
“`
{
“version”: “0.1.0”,
“command”: “C:\\Program Files\\Microsoft\\MRO-3.3.2\\bin\\x64\\Rterm.exe”,
“showOutput”: “always”,
“args”: [
“–no-restore”,
“–no-save”,
“–quiet”,
“–file=${file}”
]
}
“`
And with that you can have multithreading processing
e.g.
# Get threat numbers
getMKLthreads()
Thanks Christos for your input and feedback!
I did all the steps, but I get an error “Can’t execute a shell command on an UNC drive.”
Sorry Pankaj, I’m never tried running scripts in code from a network folder. I’m not sure if this is a permissions issue or if this is a Node.js issue. Trying running VS Code as an administrator and see if that helps.
Howdy —
Thanks so much for this helpful guide!
When I tried to set this up, the Ctrl+Shft+B command with “Configure Task Runner” did not create a .settings subdirectory with the json file.
Any ideas on the problem/solution here?
Thanks for the help!
-Adam
Adam,
My apologies, but it seems that with the newer versions of VS Code, the process has changed slightly. With the current version of Code (version 1.11.2), pressing Ctrl+Shift+B would display the “Configure Build Runner” options menu. The menu has some preset configurations, but for this demo, you would choose “Other”. Upon choosing “Other”, a
.vscode\tasks.json
file is created that has the necessary configuration. From here, the blog post should be the same.Thanks for pointing it out.
Hi Josh – Do you have an example how to format the task.json on a Mac?
Thank you.
Glen
Not really sure if I understand your question as the
task.json
should have a standard format regardless of OS. Could you please clarify your issue or what you’re attempting to do?Can we either support .r fie compilation via getting rterm.exe in the environment variables ?
Please, specify the correct procedure if we can do so.
I don’t think so off the top of my head. However, you could set the Task Runner to run
RScript
against your r file.RScript
comes with R.hi,
i started using VSC with r thanks to you, before that i was using python in VSC , my code runs in integrated git and with r i have to open r terminal seprate is there any solution to run my code in git bash rather than in r-interactive.
thanks in advance.
Haha! Yup, VSC is quite versatile. Instead of using RTerm, why not use RScript? RScript will run an .r file from the command prompt.
Thank you so much for this. I like RStudio but working R inside VSC is awesome. Plots even come out fine with a PDF extension in the Marketplace. Thanks! Had to remove the “always” parameter as this method was tagged as deprecated.