Closed Captioning Closed captioning available on our YouTube channel

How to set up VSCode for R

InfoWorld | Jul 22, 2021

R users: Do you want to be ready for GitHub Copilot? Then you’ll need to write your R code in Microsoft’s Visual Studio Code. Here’s how to set up VSCode for R. Check out how to set up Python in RStudio here:

Copyright © 2021 IDG Communications, Inc.

Hi, I’m Sharon Machlis at IDG, here with Episode 62 of Do More With R: Get ready for GitHub Copilot, and start writing R code in Visual Studio Code.
If you’re a happy RStudio user, you may never have had the urge to try another editor for R (or even Python). But if you want to try GitHub’s new Copilot project someday, once access expands, you’ll need to write your code in Microsoft’s VSCode, because Copilot is only available in that editor. (Not surprising, since Microsoft owns GitHub).
Copilot uses AI to suggest lines of code as you work. It’s not optimized for R. But David Smith at Microsoft tweeted a brief demo showing that the Copilot beta does indeed know R syntax and can suggest R code.
So, let’s see how to write and run R in Visual Studio Code. Unfortunately, VSCode isn’t quite as easy to set up as RStudio.
Step one, obviously, is downloading VSCode. You can get it at code dot visualstudio dot com slash download. VSCode is free and available for Windows, Mac, and Linux. Install it like any other software package.
The important thing for beginners to know about VSCode is that it’s modular. Documentation says it can support hundreds of programming languages and lots of other things (like Docker); but that would make for a pretty bloated piece of software if all those things were included in a basic installation out of the box. Instead, you need to add the modules you want to optimize VSCode for you – and R.
For R, that means a few extra installations and tweaking a couple of settings.
First, we’ll need to install basic R support. On the left “activity” navigation in VSCode, click the icon with the 4 squares to search for extensions. I searched for “R language” because searching just for “R” brings back a LOT of results I don’t want. Even this does. I’ll choose the top result, R 2.0.0, which also happens to be the one that David Smith recommended when I asked him
It’s important to read the Details section. I also read the project’s GitHub page and the wiki to see what else I might want to add to improve my R experience. The Details page suggests using the radian terminal, which is not a VSCode extension but an application written in Python. That means Python your system needs Python installed in order for radian to run. I already have Python and the conda package manager installed on my Mac, so I used the suggested conda install command for radian. (There’s also a pip install for those who use that popular Python package manager). If you want easy instructions for “Python installation for R users”, head to the link on screen.
There are a couple of other recommended installations for this R extension: languageserver and vscode-r-lsp.

languageserver is a regular R package. I can go back to RStudio or an R terminal and install that the usual way, with install.packages(“languageserver”).

The R extension’s Wiki also recommends installing two other R packages: jsonlite and rlang. I already have those on my system, but you’ll want to make sure you do, too, before firing up VSCode for R.

R LSP is another VSCode extension. I can either go back to my Visual Studio Code window and install it from the extension marketplace there; or, I can click the button on R LSP’s Web page.
The original R extension’s usage page said that to use the Radian console, I need to “enable config r dot bracketedPaste”. What that means is that I need to go into VSCode settings and turn that setting on.
The easy way to muck around with VSCode settings is to open up the UI and see if the setting you want is in there. To do that, go to either File > Preferences > Settings on Windows or Code > Preferences > Settings on Mac. I can then search for bracketedPaste. It’s there! And click to enable it.
You can also get to the Settings UI using VSCode’s command palette. Command palette is a very handy way of accessing all sorts of VSCode capabilities by typing instead of menu point-and-click. Most software I.D.Es have them, and they’re good to get to know. In VSCode, pull it up either with the F1 key, or the key combination Control-shift-P on Windows or Command-shift-P on Mac.

For settings, start typing Open Settings. You’ll see a choice to open the graphical UI or the underlying JSON file if you’re feeling adventurous.

OK. Documentation mentioned a couple of other settings. One is to enable R sessionWatcher. The other setting I turned on was r alwaysUseActiveTerminal. This always sends code to an active terminal – not necessarily an R terminal – instead of launching a new R terminal. It solved a problem I was running into on my Mac, so I enabled this. That means at the start of each R VSCode session, I need to type “radian” into the “regular” terminal before trying to run R code in VSCode.

As you can see, this is a bit more complicated to set up than the one-and-done RStudio installation. You may want to think of it as being like first installing base R, and then how many R packages you also install on top of a brand new basic R installation.

OK! Let’s finally write some R. I remember that the extension page said I should open a FOLDER that has an R source file, because it won’t work if I just open a file. So let me open my test folder by going to File > Add Folder to Workspace. Once I do that, I can use the “explorer” icon at the top of my left-side “activity” nav bar.

To save a little typing time, I have a simple test file here. One more thing to set up my session: I want to open a “regular” non-R terminal and then launch my R radian terminal by typing radian. This is easy: I go to the Terminal menu and launch a New Terminal. Then I type radian and I’m ready to go.

I can run a line of code or several lines I’ve selected with control-enter on Windows or command-enter on Mac. That’s pretty similar to RStudio.

Help is a little different than RStudio, though. Using the help() or question mark functions isn’t as elegant. Also, you need to quit out of a help file with q since it’s not a separate pane like in RStudio. It looks more like Unix help than what you may be used to.

However, if you hover over a function, you’ll get some help information without having to type anything, which is pretty convenient. The hover works for variables, also. If I set a variable mysum and then scroll down, hovering shows me what mysum is.

You can preview R graphics in VSCode. If you look carefully, you can see another cool VSCode R feature: When you include a color name or hex code in your data viz, there’s a little box with the color – and that’s also a color picker! Click on it, and pick any other color.

One last point: I can’t use a code editor without code snippets. Snippets are stored code blocks that are easy to reuse by typing the snippet’s abbreviation. There are included R snippets with this VSCode extension, but you can also create your own. The first time I went to Code > Preferences > User Snippets and chose R, a snippets JSON file was generated. Now, I can open that file and edit or add snippets.

Here’s the syntax: Snippet Title in quotation marks followed by a colon, and then braces. Within the braces in json format you define a prefix, which is what you type to trigger the snippet; body, which is the actual R code in the snippet; and an optional description. You can see my basic bar plot with blue bars here. I use my own convention starting all my ggplot snippets with myg underscore. The dollar sign braces are variables that can be filled in via tabs. Let me show you by invoking the snippet.

RStudio has code snippets as well. Both code editors let you see available snippets when starting to type the snippet abbreviations. VSCode also lets you see a complete list of your snippets with the Insert Snippet command from the command palette.

Verdict? VSCode has some very useful features, and it might be worth using for certain projects. I’ll probably still stick mostly with RStudio for now, though – especially for R-specific tasks like R Markdown documents and Shiny apps. But now I’m ready to try out GitHub Copilot, whenever I get acess.

That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R.

You can also find the Do More With R playlist on YouTube’s IDG Tech Talk channel -- where you can subscribe so you never miss an episode. Hope to see you next time. Stay healthy and safe, everyone!
Featured videos from