Running R in VS Code

Running R in VS Code

How to setup your VS Code to easily run R and RMarkdown

The classic way to run R is in RStudio, but since I wanted to minimize the time I need for switching between several tools I wanted to set up R in VSCode. There are some great guides out there how to do, for example which extensions to use or how to set it up step-by-step.

Here I only list the packages I currently use for R (also mentioned in the post on my Mac setup):

  • R language extension: syntax highlighting etc
  • R Debugger: allows debugging for R programs

The R Debugger is actually very handy since it allows you to interactively inspect your data frames in VS Code. Just type View(df_test) to view your data frame called df_test and it will show you a scrollable table view of your data frame in the pane where it also displays your plots (see the screenshot below).

In addition to the VS Code extensions, once you have R installed I recommend installing the following R packages via install.packages(<package name>):

  • languageserver: to allow communication between the R shell and VS Code
  • radian: huge improvement over the standard R shell
  • tidyverse: everything you will need for data processing

When you now want to run an R file, you first have to launch an R terminal. And here is the tricky bit: Don’t type R in the VS Code terminal, but rather open a new terminal by clicking the small arrow next to the plus button in the terminal and click R terminal; only via this way will VS Code be able to communicate with the R session.

If you want to enable the keyboard shortcuts you know and love from RStudio, you can add the following input to the keybindings.json configuration file (from this webpage):

// file: "keybindings.json"
[
  {
    "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\")"
  }
]

This should give you the typical shortcuts for the pipe operator, the assignment operator etc. The ones I use commonly are the following:

  • Option + Shift + F: format your R code
  • Option + -: insert assignment operator
  • Ctr + Shift + M: insert pipe operator

Help, I get an error when launching an R terminal!

In case you get an error like The terminal process terminated with exit code: 1, make sure that "terminal.integrated.inheritEnv": true in settings.json (this took me quite some time to figure out until I found this issue on GitHub).

RMarkdown in VSCode

To run RMarkdown in VS Code, I use the R Markdown All in One Extension. To make this work, though, you have to do some additional steps(see here for more detailed instructions):

  • install pandoc
  • in settings, add to file associations the key-value pair "*.Rmd": "rmd"
  • install RMarkdown in R via install.packages("rmarkdown")
  • install httr via install.packages("httr")
  • install rticles extension to get RMarkdown templates via install.packages("rticles")

After doing that, you can easily write your markdown files via creating a new file with the file ending .Rmd, insert code chunks which you can run via a small button Run Cell that appears once you insert a Markdown cell and knit the whole Markdown document via the knit button in the upper right hand corner.

Closing thoughts

Getting rid of RStudio and installing R for VS Code made me actually use R more often than before. No wonder: since I spend most of my time in VS Code anyways, it is easy to shortly open another file to run R instead of opening and using a whole new editor. I hope it helps you as well!