How to use R in Visual Studio Code - The Essentials

Author

Luke

Published

October 4, 2024

Have you ever wanted to use R in an IDE that is not RStudio? I’m sure many of you are already using the immensely popular Visual Studio Code for other programming languages or programming related tasks. Perhaps you have even gone so far and installed the R extension in VS Code, just to learn that it offers no way near the power and convenience of RStudio?

But worry not, I’m here to share with you the gospel of R in VS Code! I have been fiddling with my set up for a little while now, searching far and wide for the best resources to make R in VS Code a pleasent experience. And I think I have gotten to the point where I don’t really need RStudio anymore.

You actually only need to install a few things to reproduce a good base workflow. I don’t know what kind of obscure Rstudio features you consider essential, so I can’t promise to cover anything and everything. But this should be a good starting point, and is in fact enough for me to do all my work with only negligible inconveniences.

So let’s get started.


Setup

First things first, you need to install the R extension for Visual Studio Code from the extension marketplace. This will give you syntax highlighting, code completion, and a few other essentials like the variable manager.


Radian

Unfortunately, using an R terminal in VS Code is not as nice as it is in RStudio. One thing I really missed was being able to visually tell whether the terminal was busy or not. Thankfully, we can install Radian, which is a python package that provides a R terminal that’s even nicer than the one Rstudio has. You can install it with the following command:


pip install radian


Once installed, you can set it as the R terminal in VSCode. To do this, press Ctrl + , to start searching the settings and start typing:


"r.rterm.linux": "/home/usr/.local/bin/radian",


Do note that this command is specific to Linux, so you will need to change it if you are on Windows or MacOS. The exact path may vary depending on your system, but you can find it by running which radian in the terminal (this command may also be Unix-specific).

What’s nice about Radian is that it has a few features that the default R terminal in VS Code doesn’t have, like syntax highlighting. I find the console prompt is sufficiently informative, as it gives you a visual indication for when R is busy.

However, there is still one more tweak we need to make to make Radian work properly in VS Code. That is, we need to enable bracketed paste mode in the settings.json file. This will make it so that when you paste code into the terminal, it will be pasted as a single block, rather than line by line (which would cause a lot of error messages if you don’t write code like a savage). To open settings.json press Ctrl + Shift + P and start typing Settings. You should see the following option come up: Preferences: Open User Settings (JSON). Open this and add the following line to the settings.json file:


"r.bracketedPaste": true,


Plots (Optional)

By default, plots will pop up in a separate window. I find this very annoying, and would much rather have my plots be in a tab in the editor. This can be done by adding the following line to the settings.json file:


"r.session.viewers.viewColumn": { 
  "plot": "Active",
  "viewer": "Active",
  "view": "Active",
  "helpPanel": "Active" 
},


Another nicety is to switch to the httpgd backend for plotting. This will make it so that plots are rendered in a browser (inside VSCode), which allows you to interact with them more. This can be done by adding the following line to the settings.json file:


"r.plot.useHttpgd": true,


Quarto (for Rmarkdown users)

If you love Rmarkdown, you will miss it sorely in VS Code, as configuring it to work properly is pretty involved. Thankfully, the folks at Posit have revamped Rmarkdown into a new package called Quarto. It’s almost identical to Rmarkdown, but with a more unified development effort. Most importantly, it is designed to be IDE agnostic, so it works great in VS Code.

To install Quarto, simply head to Quarto’s website and follow the instructions there. You will also need to install the Quarto extension for VS Code from the marketplace.

I really love Quarto, and I use it for pretty much everything now, including this website. However, it’s still a new extension so there are some bugs and missing features. For example, the preview window can be a bit more buggy than it is in Rstudio. I find that it works best when you set it to open in an external browser, and then kill and restart the preview server every now and then because it will get stuck. If you are working on a project with a lot of computation, you may want to consider enabling caching, as otherwise restarting the preview server will be time-consuming.Caching should be done with care though, as I’ve found it can sometimes cause issues with downstream code. You can make quarto open the preview in your browser by adding the following line to the settings.json file:


"quarto.render.previewType": "external",


By the way, you might want to double check the keyboard shortcuts Quarto uses, as they are not always identical to the ones in Rstudio.


Make sure R is attached

Sometimes the R extension will not automatically “attach” R to the session. This means the environment will not be shared between the terminal and the editor, and thus you won’t see anything in the variable manager. You can fix this by clicking the button in the bottom right corner of the status bar that says “R: not attached”. This will paste a terminal command that will attach R to the session.


Conclusion

And that’s it! You should now have a functioning variable manager, terminal, plotting panel, and markdown setup. If you found any other tricks that you think I missed, do let me know in the comments!

Back to top