Using R with Emacs and ESS

Table of Contents

1 Installation

If you're on Linux you probably already have everything you need. To test, in emacs do C-x C-f to open a new file test.R – if the modeline at the bottom contains ESS then you're good to go. If not follow the instructions below.

1.1 Prepackaged distributions

1.1.2 Debian

apt-get install emacs ess r-recommended 

1.2 Install from source

Prepackaged versions are often old. To get features from the newest releases you need to install from source.

1.2.1 R

set -o errexit
cd ~/R
if [ -f R-devel.tar.gz ];then 
    rm R-devel.tar.gz
if [ -d R-devel ];then
    rm -r R-devel
tar xf R-devel.tar.gz
cd R-devel
./configure --prefix=$HOME --with-cairo
make install

or for windows:

2 Essential customizations

2.1 .Rprofile

This is the startup file that is read whenever R starts up on your system without the –vanilla option. There are a few essential things that you should put inside.

## This avoids having to interactively select the mirror
## during each R session.
## Change to reflect the closest CRAN mirror to you.
  ## Open a new cairo device in the bottom right.
  ## This avoids having to move/resize the plot window for each session.

2.2 .emacs

This is the file read each time Emacs starts up. Below I have just included parts that are relevant to using R with ESS.

;; ESS mode configuration (only if ess is in a nonstandard place)
(add-to-list 'load-path "/path/to/ess-5.3.4/lisp");;<<CHANGE
(autoload 'R-mode "ess-site.el" "ESS" t)
(add-to-list 'auto-mode-alist '("\\.R$" . R-mode))
(setq inferior-R-program-name "/path/to/R");;<<CHANGE
;;R stuff
(setq ess-eval-visibly-p nil)
(setq ess-ask-for-ess-directory nil)
(require 'ess-eldoc)
;;compile the first target in the Makefile in the current directory using F9
(global-set-key [f9] 'compile)
(setq compilation-read-command nil)
;;show matching parentheses
(show-paren-mode 1)

3 Useful emacs commands/modes

3.1 compilation

M-x compile, bound as F9 in .emacs allows 1-touch recompilation. Often I work on C/C++ code in some file prog.c with the intent to interface it with R/Python. For testing purposes it is easiest to make some simple testing code in main.c then make a Makefile:

prog.out: prog
        ./prog|tee prog.out
prog: prog.c main.c
        gcc -Wall prog.c main.c -o prog

Then when you hit F9 in emacs, it will run make in a new compilation buffer, and you will see errors and warnings from the compiler in this buffer. Hit tab to skip between them and enter to jump to the position in the code where it occured.

If you have several programs in the same makefile, you can declare a custom command line for a particular file at the top of your other.c:

/* -*- compile-command: "make other.out"; some-other-var: nil -*- */ 

3.2 grep

Run a grep like from the shell, but better for 2 reasons: can isearch through the results in emacs, and can hit enter to jump to the line where something was found. Also check out

3.3 regexp-builder

This is awesome. It gives you instant interactive visual feedback for constructing regular expressions. First C-x C-f find the file where you will be applying your regexp. Then do M-x regexp-builder. Then type your regexp between the quotes in the little window that appears, and all the matches in the original buffer will be highlighted in blue.

3.4 orgmode

This allows many things, but I use it mainly to quickly write up webpages from emacs. Get it from and make sure you have for syntax highlighting of code in #+BEGIN_SRC mode code #+END_SRC. To insert one of these blocks, go to a new line and insert <s then type tab.

Use C-c C-e b to convert the current buffer to HTML and preview in a browser.

Use C-c C-l to insert a link, or C-u C-c C-l to insert a link to a local file.

Author: Toby HOCKING <>

Date: 2012-09-21 13:27:07 CEST

HTML generated by org-mode 6.33x in emacs 23