R and radian on macOS and VSCode

If you want to try out the R extension for VSCode on macOS, here are the steps.

Install R for macOS

First of all, you will need to install R for macOS itself, just download the package and install it via the official website: https://cran.r-project.org/bin/macosx/.
You will also need to install XQuartz: https://www.xquartz.org/.
Try out to run the demo via the R app:

Install radian

radian is a popular alternative R console, to install it just follow the README in the github repository: https://github.com/randy3k/radian#installation.
If you try to run radian on your console, you might receive the runtime error “Cannot determine R HOME”:

To find out where is the R home, open the R app again and type “.Library”:

Set the R home via an export:

$ export R_HOME=/Library/Frameworks/R.framework/Resources

Then radian should start correctly:

Try to run some demos via radian:

Just set R_HOME in your .zshrc or .bash_profile.

Install the R extension in VSCode

For VSCode the most popular R extension is the one by Yuki Ueda: https://marketplace.visualstudio.com/items?itemName=Ikuyadeu.r.
Then try a test file like this test.R:

# add (10, 1)
add <- function(x, y) {
x + y
}
print(add (1, -2))
print(add (1.0e10, 2.0e10))
print(paste ("one", NULL))
print(paste (NA, 'two'))
print(paste ("multi-
line",
'multi-
line'))

However if you try to run source or run selected line, you will see a “darwin can’t use R error”:

Use “which radian” to find your radian path:

Then set it in the R extension settings in VSCode:

Also don’t forget to enable “Bracketed Paste”:

Then you should be able to run your R file:

Set the correct path for the R executable, that is relative to your R HOME:

So that the help topic view will work:

And also the function helper in the editor will work, when you hover over any function:

R LSP Client

For linting and autocompletion, you might want to install “R LSP Client” extension by REditorSupport.
Remember to install the language server from CRAN:

Conclusion

Now you should be able to run R using VSCode.