Guido Camargo España

Home Curriculum Vitae Blog

Using Emacs as an R editor

by Guido España

March 17, 2019

Introduction

I have been using emacs for more than five years, mainly for coding. I use it to write code mostly in C++, perl and python. The fact that I am very comfortable with emacs’ keybindings, has made me more efficient in emacs than in any other editor. Nonetheless, until recently, I had been writing most of my R scripts in RStudio. Rstudio is a great editor, but I prefer to write in emacs because I usually use the terminal and command line in my daily tasks. Because I want to use emacs as much as I can for coding, I decided to switch from RStudio to ESS in emacs.

Emacs basics

Emacs is a free and open source text editor that is fully customizable. GNU Emacs is a very old editor and it’s mostly designed to be used with the keyboard. You can install emacs in different ways, depending on your operative system. For linux users, emacs should be in the main repositories. For instance, in fedora, you can install it with dnf install emacs. In MacOS, emacs comes pre-installed, but if you want a more recent version, you can install it with homebrew, with brew cask install emacs. For windows, you can download and install emacs from its main website.

There are several tutorials online to learn how to use Emacs. The absolute beginner’s guide to emacs is a good one, the guided tour of emacs is also to understand the basics. Here, I will describe the basics and then move on to how to use ESS for R.

First, there are two ways to use emacs: from the terminal or from the GUI. Because sometimes I use the terminal and sometimes the GUI, I created an alias in my profile file (~/.bashrc) to use both.

To open a file from the terminal, you can simply use the emacs (or Emacs in my case) command, as emacs myScript.R. From there, you can edit the file as any other text editor. The most common emacs commands are:

C: Control M: Alt (command)

Installing ESS and setting up R

There are two main ways to install the ESS package.

  1. MELPA is the package archive to install emacs packages —more here. To use MELPA, you first need to add the following lines to your emacs initialization file located in ~/.emacs.d/init.el.

To list the available packages, you can press alt + x (M-x) and then type package-list-packages and press the Return key. This will list all the packages in the MELPA archive. To update the archive, you can do M-x package-refresh-contents . Then, just select the package and the install option. After installation, add the following lines to your init.el file.

  1. From Github. Download the source files from the GitHub repository in a directory in your computer (~/.emacs.d/lisp/ESS ). Then from the terminal, go to the directory and compile.

Then, in the emacs init.el file, add the following lines:

The complete installation instructions can be found in here.

R-mode

After installing the ESS package, the next thing to do is to link ‘.R’ files with r-mode, or ess-mode.

Auto completion

Of course you want emacs to auto-complete common R commands. Company mode can be installed from the MELPA repository using M-x package-install <RET> company <RET>. Then, add the following lines to the init.el file to enable company mode —I like using company mode for all my writing, so I use global company mode. I assume some people would prefer to only use company mode for specific situations, in which case, a hook should be linked to r-mode in order to locally enable company-mode.

Running R in emacs:

Once the setup is ready, you can move on to enjoy using emacs to write R code. Just type M-x R <RETURN> to create an R session. Depending on the size of your emacs window, the new R buffer would be created on the right or bottom side. Also, the experience is better if you memorize a couple of key bindings. See below the most common tasks you would use in emacs. Although I tend to only use C- to evaluate line by line (in emacs with UI).

Improving the emacs experience for R

When I started writing R code in emacs, I started to miss the region evaluation from Rstudio. After looking everywhere, I found a solution based on the emacs outline mode here. Basically, I wanted to be able to create regions using the #region name ------- structure. This is easy to achieve in emacs with the outline-regexp variable. The next thing was to select the region and send it to R for evaluation. For this, I wrote the function (based on the link above) called ‘send-section-to-R’. Finally, I created key bindings similar to those from RStudio, so that C-c t evaluates a region. To add these functions to emacs, just add the following to your init.el file, and enjoy using emacs as your main R editor.

See below a screenshot of an example of what’s possible with emacs and ESS to edit R files. You can see how some of the sections (e.g. Functions) are folded and some are unfolded. Also, one of the great things of using emacs to edit files is that you can split the window as many times as you want. I hope this is helpful.