VS Code

Integrate RStudio-like features to Visual Studio Code. This includes syntax highlighting, script execution, accessing R help, and visual representation of plots.

Guide provided by Laura Spies

Step 1: installation

  1. install Visual Studio Code from code.visualstudio.com
  2. install R with the section install
  3. install Python from python.org/downloads (hints for Windows users), preferably using the Anaconda distribution
  4. optional: Set up a dedicated Conda environment for R by running the following command (in the terminal)
    This conda environment has been designated using the name of my folder, ProFun. You can change the name or use an existing one if you would like.
conda create -n ProFun r-base=4.4 r-essentials radian -c conda-forge
  1. 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"))
  1. install the necessary R packages (in the R console):
install.packages(c("languageserver", "httpgd", "jsonlite", "rmarkdown"))
  1. 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
  1. check if the following initiates the R console within the terminal:
radian
  1. 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'" },