Fixing my broken VSCode setup for R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I recently updated my R installation, and then realised that I’d broken my VSCode/ R set up in the process – I could not launch an R terminal either directly or via radian.
I have a repo where I’ve collated various blog posts relating to setting up VSCode for R, but that didn’t solve all my problems.
I did get it resolved eventually, and here’s how.
TLDR:
- ensure R is in your PATH
- adjust your user settings for R, specifically the location of R install, and file associations
- adjust your keyboard settings
My setup
-
Windows OS
-
Python installed, not via the app store, or whatever it’s called, but downloaded the old fashioned way from the Python site.
-
I’m using the radian (python) package for coloured output in the terminal. It’s not necessary, but I like it.
Fixing the issues
- Added R to my PATH ( just search environment variables in the Windows search pane and you will find it)
In my case, this was C:\Program Files\R\R-4.2.2\bin
Note – I did not need to delve into the x64
folder inside the bin
folder, nor did I need to specify the R.exe
file
I actually discovered this last – my old PATH entry had been deleted. Putting this back in was the key step, so putting it up top for others.
Then it’s time for user and keyboard settings.
Ctrl-Shift-P
to launch the Command Palette
- Preferences: Open User Settings (JSON)
There were some R and Julia specific entries in there. The key R ones for me are:
"r.rpath.windows": "C:\\Program Files\\R\\R-4.2.2\\bin\\R.exe", "files.associations": { "*.Rmd": "rmd", "*.R": "r" }, "r.rterm.windows": "C:\\Users\\USERNAME\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\radian.exe",
These are the path to my new R installation, associating R and Rmarkdown files correctly, and ensuring that the location of radian
is known
Then back to the command palette, and time to check on various keyboard bindings
- Preferences: Open Keyboard Shortcuts (JSON)
Or File, Preferences, Keyboard Shortcuts, then, in the top right, next to the ‘play’ button, there is an option to “Open Keyboard Shortcuts JSON”)
Either of these approaches is fine
The keybindings here are mainly the same as linked in the repo, but I found a couple of extra ones that add the assignment and classic (magrittr) pipe bindings to the terminal
// Place your key bindings in this file to override the defaults [ { "key": "alt+-", "command": "type", "when": "editorLangId == r && editorTextFocus || editorLangId == rmd && editorTextFocus", // if you want using quarto, try this // "when": "editorLangId =~ /r|rmd|qmd/ && editorTextFocus", "args": { "text": " <- " } }, { "key": "ctrl+shift+m", "command": "type", "when": "editorLangId == r && editorTextFocus || editorLangId == rmd && editorTextFocus", "args": { "text": " %>% " } }, { "key": "ctrl+shift+m", "command": "-workbench.actions.view.problems" }, // input indicative of r markdown code chunk { "key": "ctrl+shift+i", "command": "editor.action.insertSnippet", "when": "editorTextFocus && editorLangId == 'rmd'", "args": { "snippet": "```{r}\n${TM_SELECTED_TEXT}$0\n```" }, "label": "input indicative of r markdown code chunk" }, // you can also input indicative of code chunk in `r` file by inserting "# %% ": // specifics in `https://github.com/REditorSupport/vscode-R/pull/662` { "key": "ctrl+shift+i", "command": "editor.action.insertSnippet", "when": "editorTextFocus && editorLangId == 'r'", "args": { "snippet": "$LINE_COMMENT %% " }, "label": "input indicative of code chunk" }, // open help panel for selection { "key": "f1", "command": "r.helpPanel.openForSelection", "when": "editorTextFocus && editorLangId == 'r' || editorTextFocus && editorLangId == 'rmd'" }, // RStudio keybinding for R Package development { "key": "ctrl+shift+b", "command": "r.install", "when": "resourceLangId == 'r'" }, { "key": "ctrl+shift+e", "command": "r.check", "when": "resourceLangId == 'r'" }, { "key": "ctrl+shift+t", "command": "r.test", "when": "resourceLangId == 'r'" }, { "key": "ctrl+shift+d", "command": "r.document", "when": "resourceLangId == 'r'" }, { "key": "ctrl+shift+l", "command": "r.loadAll", "when": "resourceLangId == 'r'" }, { "key": "ctrl+alt+p", "command": "r.runCommand", "when": "editorTextFocus && editorLangId == 'r'", "args": ".vsc.browser(httpgd::hgd_url(), viewer = \"Beside\")" }, // terminal settings { "key": "alt+-", // or whatever keybinding you want "command": "workbench.action.terminal.sendSequence", "args": { "text": " <- " }, "when": "terminalFocus && !terminalTextSelected" }, { "key": "ctrl+shift+m", "command": "workbench.action.terminal.sendSequence", "when": "terminalFocus && !terminalTextSelected", "args": { "text": " %>% " } } ]
Yes, I’m sticking with the OG pipe for now.
With all this in place, after a restart, VSCode allowed me to launch an R terminal via radian, and I’m good to go.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.