First you will need to learn basic text operating with emacs. Since emacs is very sophisticated finding out how to simply select text and copy might be a challenge. So invest some time in finding out how to do that efficiently. Reading the manual might help. For Mac OS X use Aquamacs, it supports native shortcuts.
Working with ESS does not differ from working with R. The same rules for organizing code should apply. In my case every project has its own directory, which resides in parent directory called R, which is in my home directory (For Windows, I recommend to point emacs home directory to the directory where all your source resides). So when I use ESS for working on the project I always do
M-x R and select the starting directory my project directory.
After starting R process, I usually divide emacs in two windows (emacs terminology). Then on the left I have a source code, which I send to the R process on the right. The relevant shortcuts (these are emacs shortcuts) are
C-x 3 for splitting windows verticaly,
C-x 1 for making the current buffer the only window and
C-x 2 for splitting windows horizontally.
When sending code to R, I keep distinction between functions and R statements. I do this by keeping all my functions in one file usually called
10code.R. Then I can simply load this file using load ESS file option (shortcut
C-c C-l). The advantage of this approach is that it sources all the functions and produces nothing in the R buffer. If there is an error in your code then ESS shows a message in the minibuffer and you can investigate it by pressing
The other code is the R statements, which I try to keep self-explanatory: load data, clean data, fit statistical model, inspect the results, produce the final results. The source code for these statements is the current status of the project. The intention is that after project is finished, sourcing the files with this code, reproduces the project (I also use git for tracking history). When working with this file, I usually work only with one R statement, which I send to the R process via eval function, paragraph, statement command which shortcut is
C-c C-c. This command sends to R process the paragraph, i.e. the text which is delimited by new lines. This is handy, since you can group R statements into tasks, and send whole task to R process. It also does not require selecting text, which is also very convenient. The shortcut
C-c C-c has the advantage that it moves the cursor to R window, so you immediately can inspect the results of the sent R statement.
So my basic workflow is moving a lot between windows and buffers. To facilitate this I use the following shortcuts in my .emacs file:
(define-key global-map [f1] 'Control-X-prefix)
(define-key global-map [f3] 'find-file)
(define-key global-map [f2] 'save-buffer)
(define-key global-map [f8] 'kill-buffer)
(define-key global-map [f5] 'switch-to-buffer)
(define-key global-map [f6] 'other-window)
(define-key global-map [f9] 'ess-load-file)
I rarely use
f1, but all the others very frequently. Other specific ESS settings I use are the following:
(setq comint-input-ring-size 1000)
(setq ess-indent-level 4)
(setq ess-arg-function-offset 4)
(setq ess-else-offset 4)
This tells ESS to make the tab 4 characters wide (the default is 2), which is my personal preference, and expands the number of your issued commands ESS saves as a history.
For working with R process directly I found the following shortcuts very useful:
(define-key inferior-ess-mode-map [\C-up] 'comint-previous-matching-input-from-input)
(define-key inferior-ess-mode-map [\C-down] 'comint-next-matching-input-from-input)
(define-key inferior-ess-mode-map [\C-x \t] 'comint-dynamic-complete-filename)
This recalls the R statement from your R statement history, but it tries to match it with the one which is already on your line. So for example typing
pl in R process and pressing
\C-up (that's control and the up arrow) will cycle through all the statements which start with
pl, so will recall for example all the
The final setting I use with ESS is the following:
(setq ess-ask-about-transfile t)
This way ESS always asks where to save the text in the buffer with R process. I usually number these files according to date, so I always have another way to track what exactly I was doing. The only caveat of this option is that for some reason ESS sets the R buffer to read only, after loading the R. The shortcut for making buffer writable is
So these are my settings which I use for working with ESS, I feel happy with them and I didn't feel the need to add anything for a few years already. When introducing ESS to first-time users I usually give this overview.
I will end with the final shortcut which for me is the most used shortcut when working with Emacs and with ESS in particular is
C-g which quits the command in the mini-buffer. Through all the years I work with Emacs and ESS I still manage to invoke some Emacs command which I did not want,
C-g is very helpful in these situations.