How to prepare Visual Studio Code for R

Rafal Burzynski
4 min readMay 7, 2023

--

Until recently I have been using R via PosIt cloud, which is very concinient. It operates online and looks just like RStudio. However, sometimes it is necessary to be able to run scripts locally and this is where Visual Studio Code extensions come into light.

As a first step after looing for the R extension, one sees the following screen:

So before any extension is installed first R must be downloaded from https://www.r-project.org

After selecting the right installer for the operating system you’re on, it is possible to start R and install languageserver package.

Sometimes installation will fail. In such case appropriate coding must be set by using the following command in R console:

system('defaults write org.R-project.R force.LANG en_US.UTF-8')

Then R console must be restarted.

After that the extension in Visual Studio Code can be installed. With that we should be all set to get started coding in R. Sometimes it is necessary to attach active R terminal

by clicking R:(not attached). This opens R terminal:

Before running the first script some additional setting of the extension should be added, namely path to R

On Mac this will be:

/usr/local/bin/R

By default a line can be run by clicking cmd + Enter but extension short-cuts can be updated in settings

Installation of additional packages to work similarly as in RStudio

To enable better plotting a package called httpgd can be installed. This can be done in R terminal in Visual Studio Code:

install.packages("httpgd")

Later the libary must be enabled in R extension settings:

and run with a command in R terminal:

library("httpgd")

It is also good to restart Visual Studio Code.

Then the following test script can be run:

# this is the test script
print(5+4)
x <- c(1:10)
plot(sin(x))

And as a result we see the following output:

Later on the windows can be rearranged to form a different layout in which perhaps it is easier to see plots. Please note that R extension gives an icon to the left where some additional data about variables and help files can be reviewed.

Additional packages worth installing

In order to write R markdown scripts some additional packages must be installed: rmarkdown and knitr. Those can be installed in the R terminal window:

install.packages("rmarkdown")
install.packages("knitr")

However, these two might not be enough to generate the html document from R markdown file. Usually pandoc package is missing and after clicking generate preview, error pops out. Here is the sample R markdown file (note that it has .rmd extension)

Preview can be generated with an icon from right hand side:

But in many cases output window shows the following info:

[VSC-R] markdown.rmd process started
Error: pandoc version 1.12.3 or higher is required and was not found
(see the help page ?rmarkdown::pandoc_available).

Execution halted

[VSC-R] markdown.rmd process exited with exit code 1
[VSC-R] terminating R process

The easiest way to install pandoc is from this website https://pandoc.org/installing.html (if you are using Mac with Intel processors, you need to download the x86 package version. The default button will download package for Apple’s ARM processors).

After successful installation of pandoc, the generated html preview of the R markdown file looks like this:

This finalizes the post of VS Code set up for running R. I hope it has been helpful. Happy coding!

--

--

Rafal Burzynski

I like to learn stats, data science and coding in Python and R. Then I publish what I have learnt. See my other work at https://rafburzy.github.io