3  Recommended VSCode Settings for R

Keywords

VSCode R configurations, best VSCode settings for R, R programming in VSCode, linting R code, format R code in VSCode

3.1 Introduction

This chapter focuses on recommended configurations to help you make the most of your R development experience in VSCode. From essential shortcuts to advanced customization options, these settings will improve productivity, maintain code consistency, and create a more efficient development environment.



3.2 Key Shortcuts

VSCode provides powerful shortcuts that can make R programming more efficient. Here are some of the most useful shortcuts for R development:

  • Insert Code Chunk (Quarto): Press Ctrl + Shift + I (Windows/Linux) or Cmd + Shift + I (Mac) to insert a code chunk in a .qmd file.
  • Execute R Code: Use Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac) to run the current line or selection in the R terminal.
  • Go to Definition: Quickly navigate to the definition of a function by pressing F12 or Ctrl + Click.
  • Find and Replace: Use Ctrl + F to search for text and Ctrl + H to replace text within your script.

3.3 Personalizing the Workspace

For users looking to enhance their development experience beyond the initial setup, there are several customizations available that can make the environment more similar to editors like RStudio. Here, we explore advanced customization options to further refine your coding environment.

3.3.1 Code Linting

Linting or code diagnostics is the process of analyzing code for potential errors. VSCode supports linting for R scripts using the lintr package, which checks for adherence to the tidyverse style guide.

Code linting

To enable linting, install the lintr package and add the following lines to your VSCode settings.json:

{
  "r.lsp.diagnostics": true
}

Customizing Linting Behavior: You can customize linting behavior by editing the global lintr config file at ~/.lintr, or a project-specific config file at ${workspaceFolder}/.lintr.

Recommended Configuration: The following configuration starts with the default linters and modifies some settings to better fit common workflows.

Content of ~/.lintr file:

# Default line length (80) is changed to 120
linters: linters_with_defaults(
  line_length_linter(120), 
  object_usage_linter = NULL,
  commented_code_linter = NULL
  )

This configuration disables commented_code_linter and object_usage_linter to reduce false positives in certain scenarios.

3.3.2 Code Formatting

Code linting is complemented by the styler package, which automatically restyles code, making it more readable and consistent. To enable code formatting for R scripts in VSCode, install the styler package and add these lines to settings.json:

{
  "[r]": {
    "editor.defaultFormatter": "REditorSupport.r",
    "editor.formatOnSave": true
  },
  "[rmd]": {
    "editor.defaultFormatter": "REditorSupport.r",
    "editor.formatOnSave": true
  }
}
Caution

Setting editor.formatOnSave to true will automatically format the code when you save the file, but this can be slow for larger scripts. If needed, disable this feature and use Format Document manually.

Code formatting

3.3.3 Code Selection

To improve text selection for R files, especially for variables with dots (e.g., names.like.this), modify VSCode’s settings by adding this snippet to your settings.json:

"[r]": {
    "editor.wordSeparators": "`~!@#%$^&*()-=+[{]}\\|;:'\",<>/?"
}

This removes the dot (.) from the list of word separators, making it easier to select complete variable names.

3.3.4 Sending Code to R Terminal

To make sending code to the R terminal easier, add the following lines to your settings.json:

{
  "r.alwaysUseActiveTerminal": true,
  "r.bracketedPaste": true,
  "r.sessionWatcher": true
}
  • r.alwaysUseActiveTerminal: Ensures that code is always sent to the active terminal, useful when working with multiple R sessions.
  • r.bracketedPaste: Facilitates the transfer of large code chunks by distinguishing pasted text from typed text.
  • r.sessionWatcher: Automatically attaches to the R session when a new R terminal is created, providing better integration.

Session Watcher Status

3.3.5 Setting the R profile

The .Rprofile file is used to set configurations for new R sessions. To set up the R profile for VSCode, add the following lines to your .Rprofile:

# # R Session watcher settings.
# See more details: https://github.com/REditorSupport/vscode-R/wiki/R-Session-watcher
# source: https://renkun.me/2020/04/14/writing-r-in-vscode-working-with-multiple-r-sessions/
if (interactive() && Sys.getenv("RSTUDIO") == "") {
  Sys.setenv(TERM_PROGRAM = "vscode")
  source(file.path(Sys.getenv(
    if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"
  ), ".vscode-R", "init.R"))
}

# Setting plot viewer
# Source: https://github.com/REditorSupport/vscode-R/wiki/Plot-viewer#svg-in-httpgd-webpage
if (interactive() && Sys.getenv("TERM_PROGRAM") == "vscode") {
  if ("httpgd" %in% .packages(all.available = TRUE)) {
    options(vsc.plot = FALSE)
    options(device = function(...) {
      httpgd::hgd(silent = TRUE)
      .vsc.browser(httpgd::hgd_url(history = FALSE), viewer = "Beside")
    })
  }
}


# Enabling Rstudio addin support
# Source: https://github.com/REditorSupport/vscode-R/wiki/RStudio-addin-support
options(vsc.rstudioapi = TRUE)

3.4 Conclusion

By applying these recommended configurations, you can create a highly optimized environment for R programming in VSCode. Whether you’re focused on improving productivity, maintaining consistent formatting, or enhancing integration, these configurations will significantly improve your R development experience.