A few years ago I ditched the spreadsheet in favour of writing code in R. During the process I learnt a valuable lesson: The steeper the learning curve, the larger the reward. My time invested in learning R has paid off in spades, and I now use the R language for all my numerical and qualitative analysis.
Most data scientists solve data problems with the R language using the RStudio IDE, a free and open-source Integrated Development Environment. Although RStudio is a great product, like all other software products, it’s functionality is limited to doing one thing well.
Since last year I use Emacs and once again the rule that a steep learning curve has a valuable reward has come true. Emacs is the Swiss army chainsaw of productivity, the ultimate killer app and also one of the oldest active pieces of software. Although it might seem that newer is better when it comes to software, Emacs has continuously evolved. The power of Emacs is its extensive functionality and virtually infinite customisability. Almost ninety per cent of all my computing activity now takes place within Emacs. I use it to write notes, manage my action lists, write e-mails, articles, books. The Org Mode package in Emacs is the working horse that undertakes many of these functions. Since recently also to write R code in Emacs.
To master Emacs to the level, I am at now has taken me a couple of months. Initially, the bewildering amount of keyboard shortcuts is a challenge, but your muscle memory will soon kick in, and your fingers will glide across the keyboard like an eagle.
Using R with Emacs and ESS
The advantage of using Emacs over RStudio is that I can seamlessly switch between my notes, to-do lists, calendars and so on, while also developing code. There is no need to install, maintain and master multiple applications as Emacs cover almost all my computing needs. Just like R, Emacs includes thousands of packages that extend its functionality far beyond what one would expect from a text editor.
The disadvantage of Emacs compared to RStudio is that it is not as pretty and the screen more resembles an angry fruit salad than a slick material design. The image above shows my screen when I translated a paper about body image research into Org Mode with embedded R code. A temporary disadvantage is that there is a bit of preparation is required to enable coding in Emacs, but that is only a temporary hurdle and adds to the learning curve.
This article explains how to start Using R with Emacs and ESS (Emacs Speaks Statistics). The first section of this article provides links to resources on how to install Emacs on various platforms and how to enable it to start writing code in R with the ESS package. The second sections show how to use the R console and source files. The last section introduces literate programming by combining Org Mode and R and exporting the results to PDF, HTML etc.
Getting Started with Emacs
Installing Emacs depends on your operating system. A simple Google search will tell you what to do. The video playlist below shows how to install Emacs on Ubuntu, OS X or Windows 10. You will also need to install the R language on your machine, and if you like to create publication-quality output, the also include LaTeX.
To install packages, type
M-x list-packages. This function displays all available packages. In Emacs-speak this means pressing the Alt (modify) key and the lowercase ‘x’ key, followed by ‘list-packages’ and return. You can find ESS by pressing ‘f’ and search for the package. Type ‘i’ to mark it for installation and press ‘x’ to execute the installation. The example init file loads the following packages that need to be manually installed:
- ESS: Emacs Speaks Statistics.
- ESS Smart Underscore: Smarter underscore for Emacs Speaks Statistics.
- Org-Ref: org-mode modules for citations, cross-references, bibliographies in org-mode and useful BibTeX tools to go with it.
- auto-complete: Auto Completion for GNU Emacs.
Using R with Emacs and ESS
To start using R with Emacs type
M-x R RET. ESS will ask to nominate a working directory and opens a new buffer with the name
*R*. You can now use the R console to write code as you are used to. You can open multiple instances of the R console with different working directories and environment, just like projects in RStudio.
Any file with the .R extension is now recognised as being written in the R language. This is a so-called Emacs major mode, which is the key functionality of Emacs. A major mode defines how a file is managed, whether it is an R file, an Org Mode file and so on.
One bit of useful information is that the underscore key is mapped to the
assignment operator. If you need an underscore, you need to type it twice in a row. This can be annoying when you are an avid user of ggplot. The ess-smart-underscore package solves this issue by extending the functionality of ESS. You can install this package the same way you installed ESS itself. To enable it, add
(require 'ess-smart-underscore) to your init file.
To write in a source file, you can create a .R file by typing
C-x C-f (find-file function). Type the filename to create a new file, or when it already exists, open the file. You can now start writing R code as you would in any other editor. As soon as you evaluate code for the first time in a session, ESS will ask you what the starting project directory is, which is defaulted to the folder that your .R file is in. To evaluate the whole buffer, use C-c B, to evaluate a section use C-c c and to evaluate a line press M-RET.
To show the source file and the R console next to each other, type
C-x 3 to split your window to show two buffers. You can then use C-x b to select the other buffer.
A disadvantage of using R in ESS is that there is no simple way to integrate plot outputs into the Emacs window. When I am iteratively working on a visualisation, I save it to a file and open it in a separate buffer, as shown in the screendump.
Using R with Emacs and ESS to Write an Academic Paper
Org Mode is the most popular extension of Emacs and is precompiled with current versions of the software. Org Mode is an extremely versatile text editing extension. I use it to manage my projects using a calendar and To-Do lists, I write notes, write books an articles. I have translated an article I previously wrote LaTeX and Sweave to Org Mode.
Org Mode works very well with LaTeX. You will need to write some code into your init file to set a template, after which writing LaTeX code is a breeze. The Org Babel package functions as an interface between Org Mode and R. Perhaps explaining this file in detail is a topic for a future post. You can view the most recent version of the Org File and the associated setting in the init file on my GitHub page.
Source link https://www.r-bloggers.com/using-r-with-emacs-and-ess/