R and radian on macOS and VSCode

March 07, 2021

rlang
r
vscode
macos

This post is also on Medium.

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:

r_app_mac_os_xquartz

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:

radian_osx_runtime_error_missing_home

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

r_app_library

Set the R home via an export:

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

Then radian should start correctly:

r_home_set_mac_osx

Try to run some demos via radian:

radian_console_demo_graphics

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:

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:

vscode_r_error

Use which radian to find your radian path:

which radian
/Library/Frameworks/Python.framework/Versions/3.8/bin/radian

Then set it in the R extension settings in VSCode:

vscode_r_extension_rterm_settings

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

vscode_r_extension_bracketed_paste_settings

Then you should be able to run your R file:

vscode_r_running_test

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

vscode_r_path

So that the help topic view will work:

vscode_r_help_topic

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

vscode_r_function_helper

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:

r_language_server

Conclusion

Now you should be able to run R using VSCode.