VS Code
Replicate Rstudio functionalities in VScode such as syntax highlighting, script execution, R help & plots display:
Guide provided by Laura Spies
Step 1: installation
- install Visual Studio Code from code.visualstudio.com
- install R with the section install
- install Python from python.org/downloads (hints for Windows users), preferably using the Anaconda distribution
- optional: create a Conda environment for R (in the terminal):
- optional: connect R to Jupyter (in the R console):
install.packages("IRkernel")
IRkernel::installspec(name = 'ir', displayname = 'R (ProFun)')
install.packages(c("repr", "IRdisplay", "IRkernel"))
- install the necessary R packages (in the R console):
- install Radian, an improved R console REPL interface (in the terminal):
pip install -U radian
# alternatively, if employing Conda:
conda activate ProFun
conda install -U radian
- check if the following initiates the R console within the terminal:
- download the following extensions for VSCode:
- Python
- Jupyter
- R Debugger
- R Tools
- R Extension Pack
Step 2: configuration
To access the settings.json
file, press Ctrl + Shift + P
, then type “Preferences: Open Settings (JSON).” Utilize the following JSON configuration:
{
"editor.fontSize": 15,
// R Settings
"r.rterm.windows": "C:\\Users\\YOUR_USERNAME\\miniconda3\\envs\\ProFun\\Scripts\\radian.exe",
"r.bracketedPaste": true,
"r.lsp.path": "C:\\Program Files\\R\\R-4.4.1\\bin\\x64\\R.exe",
// Terminal Profiles for R and Python
"terminal.integrated.profiles.windows": {
"ProFun (Radian)": {
"path": "C:\\Windows\\System32\\cmd.exe",
"args": ["/K", "conda activate ProFun && radian"]
},
"Python (Base)": {
"path": "C:\\Windows\\System32\\cmd.exe",
"args": ["/K", "conda activate base"]
}
},
// Set Default Terminal Profile to Radian
"terminal.integrated.defaultProfile.windows": "ProFun (Radian)",
// Keybindings for R-Specific Behavior
"r.alwaysUseActiveTerminal": true,
"r.plot.useHttpgd": true
}
Search for your keybindings.json
and set keyboard shortcuts a sdesired, e.g.:
// R-Specific Keybindings
{ "key": "ctrl+shift+f10", "command": "r.restartSession" },
{ "key": "ctrl+shift+enter", "command": "r.runSource" },
{ "key": "ctrl+alt+i", "command": "r.createRmdChunk" },
{ "key": "ctrl+alt+h", "command": "r.help" },
{ "key": "ctrl+alt+o", "command": "r.viewObject" },
{ "key": "ctrl+alt+p", "command": "r.showPlotHistory" },
{ "key": "ctrl+alt+k", "command": "r.knit" },
{ "key": "ctrl+alt+f", "command": "r.changeWorkingDirectory" },
{ "key": "ctrl+alt+enter", "command": "r.executeCode" },
{ "key": "ctrl+enter", "command": "r.runSelectionAndMoveCursor", "when": "editorTextFocus && editorLangId == 'r'" },
{ "key": "ctrl+shift+m", "command": "type", "args": { "text": " %>% " }, "when": "editorTextFocus && editorLangId == 'r'" },
{ "key": "alt+-", "command": "type", "args": { "text": " <- " }, "when": "editorTextFocus && editorLangId == 'r'" },
{ "key": "ctrl+shift+l", "command": "workbench.action.terminal.clear", "when": "terminalFocus" },
{ "key": "ctrl+alt+w", "command": "r.openWebHelp", "when": "editorTextFocus && editorLangId == 'r'" },
{ "key": "f1", "command": "r.help", "when": "editorTextFocus && editorLangId == 'r'" },