Emacs and ESS

Emacs is an incredibly powerful text editor. It can be programmed to do anything. It was written before the advent of GUI interfaces, so every important function can be accessed through a series of keystrokes. Many of the more popular functions appear in menus. If you spend the time to learn the small subset of keystrokes for basic functions, you will find that Emacs is very well laid out, intuitive, and very fast to use. ESS (Emacs Speaks Statistics) is an add-on to Emacs to make Emacs a powerful session environment for R and other statistical systems.

Invoking Emacs

You can invoke Emacs from the KDE Development menu (you can create a button on your task bar to make this easy) or by issuing the command emacs or emacs & from the command prompt. If you want to open a file into an existing Emacs session, define the following alias in your .bashrc file:
alias Xmacs="emacsclient -n"
and set up your Emacs .emacs file so that the Emacs server is started the first time emacs is involved. This is done by adding the following lines to .emacs:
(setq gnuserv-frame (selected-frame))
(server-start)
Then at the command prompt you can type Xmacs foo to open the file foo into the already started Emacs session.

Note that with the It's All Text! plugin for Firefox you can define emacsclient -n as the editor the plugin should use.

Installing Emacs and ESS

  • use command apt-get install emacs23 emacs-goodies-el ess
The goodies package includes many useful Emacs packages such as tabbar.

Emacs and LISP

As I pointed out earlier, Emacs can be programmed to do anything. This is because Emacs is written in Lisp. Lisp is a dynamically compiled language that uses Polish notation (aka prefix notation).

Basic Emacs Keystrokes

  • C-x C-f: Find file.
  • C-x C-s: Save buffer.
  • C-x C-w: Write buffer to file.
  • C-x C-c: Quit Emacs.
  • C-h: Help system.
  • C-g: command quit.
  • M-x: Execute extended command.
  • C-s: Search forward.
  • C-r: Search backward.

Emacs Configuration

One example of an Emacs configuration file follows below. More importantly you will probably want to configure Emacs to use horizontal tabs for easily navigating multiple buffers. Do this by putting the following in your ~/.emacs file:
(setq tabbar-buffer-groups-function (lambda () (list "All buffers")))
(setq tabbar-cycling-scope nil)
(setq tabbar-home-button (quote (("[Home]") "[x]")))
(setq tabbar-separator (quote (" ")))
(require 'tabbar)
(tabbar-mode)
Here is the other version of .emacs to pick and choose from.
(defun print-header-func ()
  (concat (ps-time-stamp-locale-default) " : " (ps-header-dirpart)))

(setq ps-lpr-command "kprinter"
      ps-lpr-switches (list "--stdin"))

(setq ps-spool-duplex t
      ps-switch-header nil
      ps-print-header t
      ps-print-footer nil
      ps-print-header-frame nil
      ps-header-offset 5.58001116
      ps-header-title-font-size '(10 . 12)
      ps-header-font-size '(7 . 8.5)
      ps-header-lines 1
      ps-left-header (list 'print-header-func))

(setq inferior-R-args "--vanilla")

(ps-extend-face '(default "black" nil nil) 'MERGE)
(mouse-wheel-mode)
(global-set-key [C-tab] 'cyclebuffer-forward)
(global-set-key [f4] 'goto-line)
(global-set-key [C-S-iso-lefttab] 'cyclebuffer-backward)
(global-set-key [(control x) (control k)] 'kill-this-buffer)

(add-to-list 'auto-mode-alist '("\\.rd\\'" . Rd-mode))

(add-hook 'Rd-mode-hook
     '(lambda ()
        (abbrev-mode 1)
        (font-lock-mode 1)))

(setq lpr-command "kprinter")
The last command makes the Emacs Print commands (accessible from the File menu) use the KDE Ubuntu kprinter command for printing, which gives you many printing options. The printing functions defined earlier above may be sufficient.

To get nice color highlighting of matching {[()]}, do the following:

cd /tmp
wget -nd http://nschum.de/src/emacs/highlight-parentheses/highlight-parentheses.el
sudo cp -p /tmp/highlight-parentheses.el /usr/share/emacs22/site-lisp/ess/.
then put the following in your .emacs file:
(require 'highlight-parentheses)
(setq hl-paren-colors '("gold" "red" "blue" "green" "orange"))
(defun hpm-on ()
 (highlight-parentheses-mode t))
(add-hook 'ess-mode-hook 'hpm-on)
(add-hook 'inferior-ess-mode-hook 'hpm-on)
(add-hook 'tex-mode-hook 'hpm-on)
(add-hook 'text-mode-hook 'hpm-on)
Click on the Options menu on Emacs and check Paren Match Highlighting.

ESS Edit Buffer Keystrokes

  • _: smart underscore inserts a " <- ", press _ again to get a _.
  • C-c TAB: object completion.
  • C-M-q: indent expression.
  • C-M-a: goto beginning of function.
  • C-M-e: goto end of function.
  • C-M-q: indent each line in the compound expression which follows point.
  • C-c C-l: load a source file.
  • C-c C-z: jump to ESS buffer.
  • C-`: jump to location of syntax error.
  • C-c C-v: get help for a R function.

ESS Evaluation Keystrokes.

  • C-c C-j: evaluate line.
  • C-c C-n: evaluate line and step.
  • C-c C-b: evaluate buffer.
  • C-c C-f: evaluate function.
  • C-c C-r: evaluate region.
  • For each evaluate command if you replace the last CTL with ALT the command will jump to the ESS buffer.
    • C-c M-f: evaluate function and jump to ESS process buffer.
    • C-c M-b: evaluate buffer and jump to ESS process buffer.

ESS Process Buffer Keystrokes.

  • In general the process buffer's keystrokes are the edit buffer's keystrokes while holding CTL.
  • C-END: go to end of buffer.
  • C-HOME: go to begining of buffer.
  • C-UP: previous command in history.
  • C-DOWN: goto begining of word.
  • C-LEFT: goto end of word.
  • C-RIGHT: goto end of word.
  • C-c C-w: backward kill word.
  • C-c C-u: delete everything from prompt to point.
  • TAB: object and filename completion.

Useful .emacs definitions for running ESS with R

Add the following 3 lines if you want to have R use the directory from which you invoked emacs or emacsclient as the working directory, you want R to open in a new window, leaving your source code window intact, if you want ?foo to open a help file for function foo in a new frame, and if you don't want to restore or save an .RData object with all the objects created during your R session.
(setq ess-ask-for-ess-directory nil)
(setq inferior-ess-same-window nil)
(setq ess-help-own-frame 'one)
(setq inferior-R-args "--no-restore --no-save")
If you want ESS to ask you about the location of the working directory, remove the first setq command. Otherwise, this is a good way to use ESS with R:
cd myproject
emacs analyze.r &    or emacsclient -n analyze.r &
# See source code for analyze.r in the sole Emacs bufer
Esc R           # start R session in myproject directory
?functionname   # open help file in a new Emacs frame
Topic attachments
I Attachment Action Size Date Who Comment
elseemacs .emacs manage 2.5 K 23 Mar 2006 - 09:30 CharlesDupont  
Topic revision: r10 - 18 Nov 2009 - 15:12:47 - FrankHarrell
 
Register | Log In
Copyright © 2009 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback