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:

  1. The home installation path was located in “C:\Program Files” as compared to “C:\Program Files(x86)”
  2. I have a “x64” subdirectory in my Rterm’s bin folder.

rterm-installation-path

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.

visual-studio-code-context-menu-open-with-code

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.

visual-studio-code-configure-task-runner

By choosing this, a couple of things are created for you:

  1. A .settings subdirectory
  2. 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.

r-test-output

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.