2  Setting Up R in VSCode

Keywords

setting up R in VSCode, R environment in VSCode, VSCode R setup, install vscode-R extension, R debugging in VSCode

2.1 Introduction

RStudio has long been a favored environment for R programming, offering rich features for data analysis and interactive tasks. On the other hand, VS Code offers a more flexible Integrated Development Environment (IDE) that is not limited to any specific programming language. It’s customizable with settings and extensions and supports various languages.

The vscode-R extension enhances R coding in VS Code, integrating many features found in RStudio. Although it may not fully replace RStudio, it is ideal for users needing a versatile environment that supports multiple languages (e.g., R, Python, C++) and the ability to work with remote servers or multiple R sessions.

This chapter will guide you through setting up your R environment in VSCode for a productive and streamlined workflow.



2.2 Essential Tools

To create a comprehensive R development environment in VSCode, we’ll install the following tools and packages:

  • VSCode: A modern, open-source IDE that’s highly customizable and supports a wide range of programming languages.
  • vscode-R: The essential R extension for Visual Studio Code, enabling R language support, integration with R Markdown, and interactive communication between VSCode and the R terminal. It provides core features such as:
    • Syntax Highlighting: Improves code readability by coloring keywords, functions, variables, and more.
    • Code Snippets: Includes built-in code snippets for R to help speed up coding.
    • Workspace Viewer: Displays the variables available in the current R environment.
    • Data Viewer: Allows you to view data frames and other tabular objects.
    • Help Viewer: Displays R documentation.
  • languageserver: An R package that implements the Language Server Protocol for R, offering extensive language analysis features like auto-completion, diagnostics, documentation, symbol highlights, and more.
  • radian: A modernized R console, enhancing the traditional R terminal with features like syntax highlighting and auto-completion.
  • vscode-R-debugger: A VSCode extension that introduces R debugging capabilities into VSCode. Key features include:
    • Breakpoints: Set breakpoints in your R scripts to stop execution and inspect variables.
    • Step-Through Debugging: Step through code line-by-line to better understand how it executes.
    • Call Stack Inspection: View the call stack to understand how functions are being executed in a sequence.
  • httpgd: An R package providing a graphics device that asynchronously serves SVG graphics via HTTP and WebSockets. vscode-R uses httpgd to provide an interactive plot viewer.
  • rmarkdown: This R package enables the creation of reproducible, story-like data analysis documents. With vscode-R, users can efficiently edit, execute, and compile R Markdown files.
  • Pandoc (optional): A versatile document conversion utility used by vscode-R to generate preview renders of R documentation when hovered over in the code.

2.3 Installation Guide for R Programming in VSCode

In this section, we’ll walk through the steps for setting up R in VSCode. This guide covers the installation and configuration process for Windows, macOS, and Linux.

2.3.1 Step 1: Install R and VSCode

  • R: Ensure that R is installed on your system. Download the latest version from the CRAN website and follow the installation instructions for your operating system.
  • VSCode: If you haven’t already, install Visual Studio Code from the official website. It’s available for various platforms, including Windows, macOS, and Linux.

2.3.2 Step 2: Install the vscode-R Extension

  • Open VSCode and navigate to the Extensions view by clicking the square icon on the sidebar or by pressing Ctrl+Shift+X.
  • Search for REditorSupport.r and click ‘Install’ to add it to your VSCode environment.
Note

Note that:

  1. For Linux and macOS users:
    • if R is installed from CRAN, the default settings should work.
    • For custom installations, update the VSCode settings r.rterm.linux (Linux) or r.rterm.mac (macOS) with the path to your R executable.
  2. For Windows:
    • Default settings work if R is installed from CRAN with ‘Save version number in registry’ enabled.
    • Update r.rterm.windows with your R executable’s path for other installations.

2.3.3 Step 3: Install the languageserver Package in R

The languageserver package offers extensive language analysis features, such as auto-completion, diagnostics, and documentation:

  • Run install.packages('languageserver') in your R console to install it.
Note

Note that, on Windows, you may need to install Rtools first.

2.3.4 Step 4: Setting Up Radian as the R Console

radian is recommended as the R terminal for interactive use due to its modern features, such as syntax highlighting and auto-completion:

  • Install radian using Python’s package manager pip:
    • Linux/macOS: pip install -U radian
    • Windows: Install Python from the official website and then run pip install -U radian using PowerShell or Command Prompt.
  • To configure VSCode to use radian as the default R terminal, go to File > Preferences > Settings in VSCode, search for r.rterm, and set the path to the radian executable.

2.3.5 Step 5: Install Additional Tools

  • rmarkdown: Install rmarkdown for generating reproducible reports: install.packages("rmarkdown").
  • Pandoc: Install Pandoc if you wish to generate preview renders of R documentation when hovering over functions or variables.

2.3.6 Step 6: Setting Up VSCode-R-Debugger

The vscode-R-debugger extension, along with the vscDebugger R package, enables debugging for R in VSCode:

  • Install the R Debugger extension from the VSCode marketplace.
  • Install the vscDebugger package in R using remotes::install_github("ManuelHentschel/vscDebugger").

2.3.7 Step 7: Enable httpgd for Enhanced Plotting

httpgd offers an enhanced plotting experience for VSCode:

  • Install httpgd from CRAN: install.packages("httpgd").
  • Enable r.plot.useHttpgd in VSCode settings to use it as the default plot viewer.

2.3.8 Conclusion

With the right tools and extensions, VSCode can become a powerful and efficient environment for R programming. This setup will enhance productivity and provide a seamless development experience for R users.

2.3.8.1 References