Code editors

Author

Murray Logan

Published

September 15, 2024

1 Introduction

In the previous tutorial, we installed R and began exploring the language. If this was your very first time using R and perhaps your first exposure to any programming language, it is likely that you worked through the tutorial using either the R Gui (if on windows) or the terminal application in MacOSX or Linux.

Whilst these tools to provide direct interaction with the R engine, they do little to support your efforts to develop code, documentation and sophisticated analysis outputs. This is where code editors come in.

Code editors are a specialized software tools designed for creating and modifying source code of computer programs. They provide essential features such as syntax highlighting, code completion, and error checking, enhancing the efficiency and accuracy of programming tasks. Code editors are essential for developers, offering a streamlined environment for writing, editing, and organizing code, facilitating the software development process.

Choosing an appropriate code editor is crucial for efficient R development. Each editor offers unique features and interfaces, catering to different preferences and workflows. This guide will walk you through some of the popular choices, including RStudio, Visual Studio Code, Emacs, Neovim, and Sublime Text, helping you make an informed decision.

Of these, particular emphasis will be placed on RStudio. This is primarily because it is specifically designed to be an Intergrated Development Environment (IDE) for R. It is developed by active members of the R community for the R community. Furthermore, because it is a dedicated R IDE, it works straight out of the box with little to no configuration necessary. By contrast, the other editors are general code editors and thus must be specifically configured to provide R based functionality.

2 Editors

2.0.1 Overview

RStudio stands out as a widely used and dedicated Integrated Development Environment (IDE) designed for R development. Its user-friendly interface and comprehensive features make it a popular choice among R users.

2.0.2 Installation

  1. Download R:
    • RStudio requires R to be installed. If you have not already done so, download and install R from the official CRAN website.
  2. Download RStudio:
    • Visit the RStudio Download page and select the “RStudio Desktop” version compatible with your Windows operating system.
  3. Install RStudio:
    • Run the downloaded RStudio installer and follow the installation wizard.
    • Accept the default settings unless you have specific preferences.
  4. Launch RStudio:
    • After installation, launch RStudio from the Start menu or desktop shortcut.
  1. Download R:
    • If you have not already done so, download and install R on macOS from the official CRAN website.
  2. Download RStudio:
  3. Install RStudio:
    • Run the downloaded RStudio package, and macOS will guide you through the installation process.
  4. Launch RStudio:
    • Open RStudio from the Applications folder or use Spotlight to search for it.
  1. Download R:
    • If you have not already done so, install R on your Linux distribution using the package manager. For example, on Ubuntu, run:
sudo apt-get install r-base
  1. Download RStudio:
    • Visit the RStudio Download page and choose the appropriate RStudio Desktop version for your Linux distribution.
  2. Install RStudio:
    • Run the downloaded RStudio package, and follow any additional instructions based on your Linux distribution.
  3. Launch RStudio:
    • Open a terminal and type rstudio to launch RStudio.

2.0.3 Key Features

RStudio offers an integrated scripting and console environment, extensive support for RMarkdown, and streamlined package management capabilities.

I strongly encourage you to look over the RStudio user guide - particularly the Getting Started section.

2.0.4 Overview

Visual Studio Code (VSCode) is a versatile and extensible code editor known for its speed and efficiency. While not exclusively designed for R, it offers excellent support for the language through extensions.

2.0.5 Installation

  1. Download Visual Studio Code:
    • Visit the Visual Studio Code Download page and choose the version suitable for your operating system (Windows, macOS, or Linux).
    • Follow the installation instructions for your specific operating system.
  2. Install Visual Studio Code:
    • Run the downloaded installer and follow the installation wizard.
    • Accept the default settings unless you have specific preferences.
  3. Launch Visual Studio Code:
    • After installation, launch VSCode from the Start menu or applications folder.

2.0.6 Setting Up R Support in Visual Studio Code:

  1. Install R Extension:
    • Open VSCode and go to the Extensions view by clicking on the square icon on the sidebar or using the shortcut Ctrl+Shift+X.
    • Search for “R” in the Extensions view search box.
    • Install the “R Language” extension provided by Yuki Ueda.
  2. Configure R Path (Optional):
    • Open the VSCode settings by pressing Ctrl+, or navigating to File > Preferences > Settings.
    • Click on the “Open Settings (JSON)” icon in the upper-right corner of the Settings tab.
    • Add the following JSON configuration to set the path to your R executable:
"r.rpath.windows": "C:\\Program Files\\R\\R-4.x.x\\bin\\x64\\R.exe",  // Replace with your R path
  1. Select R Interpreter:
    • Create or open an R script in VSCode.
    • Look for the “Select an R interpreter” notification at the bottom-right corner.
    • Click on “Select R Interpreter” and choose the R version you installed.
  2. Install Required R Packages:
    • Open the integrated terminal in VSCode using Ctrl+` .
    • Install the necessary R packages (e.g., languageserver and formatR) by running the following commands:
install.packages("languageserver")
install.packages("formatR")
  1. Reload Window:
    • After configuring R support, it’s recommended to reload the VSCode window to apply the changes.
  2. Verify R Support:
    • Open R Script:
      • Create or open an R script (.R file) in VSCode.
    • Check R Features:
      • Verify that R features such as syntax highlighting, code completion, and linting are functioning correctly.
    • Run R Script:
      • Run parts of your R script or the entire script to ensure that the R interpreter is correctly executing code.

2.0.7 Key Features

VSCode is lightweight, supports the R Language Server, and can be enhanced with various extensions to meet specific development needs.

2.0.8 Overview

Emacs is a highly customizable and extensible text editor renowned for its versatility. It may have a steeper learning curve, but its power lies in its ability to adapt to individual preferences.

2.0.9 Installation

  1. Download and Install Emacs:
    • Visit the GNU Emacs Download page and select the appropriate version for your operating system (Windows, macOS, or Linux).
    • Follow the installation instructions provided on the download page.
  2. Launch Emacs:
    • After installation, launch Emacs. On Windows, you can find it in the Start menu. On macOS and Linux, open a terminal and type emacs.
    • Configuring Emacs for R Support:
  3. Install ESS (Emacs Speaks Statistics):
    • ESS is a package for Emacs that provides support for various statistical languages, including R.
    • Open Emacs and add the following to your Emacs configuration file (usually ~/.emacs or ~/.emacs.d/init.el):
;; Add MELPA repository for package installation
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

;; Install ESS package
(unless (package-installed-p 'ess)
  (package-refresh-contents)
  (package-install 'ess))
  1. Configure ESS:
    • Customize your Emacs configuration to set up ESS for R. Add the following lines to your configuration file:
;; Configure ESS for R
(require 'ess-site)
  1. Select R Interpreter:
    • Open an R script in Emacs. ESS should automatically detect your R installation.
    • If needed, customize the R interpreter by adding the following line to your configuration file:
(setq inferior-R-program-name "/path/to/R")
  1. Useful Keybindings (Optional):
    • Add keybindings for common ESS commands. For example:
(global-set-key (kbd "C-c C-k") 'ess-eval-buffer)
(global-set-key (kbd "C-c C-r") 'ess-eval-region)
  1. ESS Documentation (Optional):
    • Access ESS documentation by typing C-h i to open the Info viewer, then select “ESS” from the menu.
  2. Reload Configuration:
    • After making changes to your Emacs configuration, restart Emacs or use M-x load-file to reload the configuration.
  3. Verify R Support:
    • Open R Script:
      • Create or open an R script (.R file) in Emacs.
    • Check ESS Features:
      • Verify that ESS features such as syntax highlighting, code evaluation, and interaction with R are functioning correctly.
    • Run R Script:
      • Evaluate parts of your R script or the entire script to ensure that the R interpreter is correctly executing code.

2.0.10 Key Features

Emacs supports extensive extensibility through packages, boasts ESS (Emacs Speaks Statistics) for R integration, and offers Org Mode for literate programming.

2.0.11 Overview

Neovim is a modern and extensible text editor that builds on the foundation of Vim. It combines the efficiency of Vim with additional features for a more contemporary editing experience.

2.0.12 Installation

  1. Download and Install Neovim:
    • Visit the Neovim GitHub Releases page and download the installer appropriate for your operating system (Windows, macOS, or Linux).
    • Follow the installation instructions provided on the GitHub page.
  2. Launch Neovim:
    • After installation, launch Neovim. On Windows, you can find it in the Start menu or use the executable. On macOS and Linux, open a terminal and type nvim.
  3. Configuring Neovim for R Support:
    • Install a Plugin Manager (Optional):
      • While optional, using a plugin manager makes it easier to manage Neovim plugins. Popular choices include vim-plug and dein.vim.
      • Follow the installation instructions provided by the chosen plugin manager.
  4. Install Nvim-R Plugin:
    • Nvim-R is a plugin that enhances Neovim for R development.
    • Add the following lines to your Neovim configuration file (usually ~/.config/nvim/init.vim or ~/.vimrc):
-- For vim-plug
Plug 'jalvesaq/Nvim-R'

-- For dein.vim
call dein#add('jalvesaq/Nvim-R')
  1. Configure Nvim-R:
    • Customize your Neovim configuration to set up Nvim-R for R. Add the following lines to your configuration file:
-- Set the path to your R executable (replace with your actual path)
let g:vimrplugin_Rexecutable = '/path/to/R'

-- Enable filetype plugin and indentation
filetype plugin indent on

-- Set R as the default file type for .R files
au BufNewFile,BufRead *.R set filetype=r
  1. Install Plugins:
    • Open Neovim and run the command to install the configured plugins:
      • For vim-plug: :PlugInstall
      • For dein.vim: :call dein#install()
  2. Reload Configuration:
    • After adding the configuration, restart Neovim or use :source % to reload the configuration.
  3. Verify R Support:
    • Open R Script:
      • Create or open an R script (.R file) in Neovim.
    • Check Nvim-R Features:
      • Verify that Nvim-R features, such as syntax highlighting, code evaluation, and interaction with R, are functioning correctly.
    • Run R Script:
      • Evaluate parts of your R script or the entire script to ensure that the R interpreter is correctly executing code.

2.0.13 Key Features

Neovim maintains Vim compatibility, supports plugins for extended functionality, and emphasizes efficient text editing.

2.0.14 Overview

Sublime Text is a lightweight yet feature-rich text editor appreciated for its speed and simplicity. While not R-specific, it offers a customizable environment suitable for various programming languages.

2.0.15 Installation

  1. Download and Install Sublime Text:
    • Visit the Sublime Text Download page and download the installer for your operating system (Windows, macOS, or Linux).
    • Follow the installation instructions provided on the website.
  2. Launch Sublime Text:
    • After installation, launch Sublime Text. You can find it in the Start menu on Windows, in the Applications folder on macOS, or by using the terminal on Linux.
  3. Configuring Sublime Text for R Support:
    • Install Package Control:
      • Package Control is a package manager for Sublime Text. Follow the installation instructions on the Package Control website.
    • Install Terminus Package:
      • Open Sublime Text and press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS) to open the command palette.
      • Type “Install Package” and select “Package Control: Install Package.”
      • Search for “Terminus” and install the package.
    • Install R:
      • Make sure you have R installed on your system. You can download it from the official R website.
    • Configure Terminus for R:
      • Open Sublime Text and create or open an R script (.R file).
      • Press Ctrl+ (Windows/Linux) or Cmd+ (macOS) to open the Terminus console.
      • In the Terminus console, type the following command to start an R session:
R
- Terminus will open a new terminal at the bottom of Sublime Text,
  providing an interactive R session.
- Create Build System (Optional):
  - You can create a custom build system for R scripts to simplify
    execution.
    - Open a new file in Sublime Text and paste the following JSON
      configuration:
{
    "cmd": ["R", "--slave", "--vanilla", "-f", "$file"],
    "file_regex": "^(?:(...*?):([0-9]+):([0-9]+)|(...*?))$",
    "selector": "source.R"
}
    - Save the file with the extension .sublime-build in the
      "User" directory of your Sublime Text "Packages" folder. You
      can find this folder by selecting "Preferences" > "Browse
      Packages..." in Sublime Text.
  - Run R Script:
    - Open an R script in Sublime Text.
    - Use the Terminus console to interact with the R session and
      execute commands.

2.0.16 Key Features

Sublime Text boasts multiple cursors, supports extensions through Package Control, and provides ample customization options.