How to get R, Emacs, ESS and X11 working together in Mac OS
X
This is a quick and not very verbose step-by-step guide on how to get R
running using a Carbonized (i.e., not in a terminal/X11 window) version of
Emacs, ESS and using Apple's X11 for graphical window output.
I'll assume that you already have R running in a terminal window
(i.e., just typing "R" on a command line prompt starts R), and you
don't need any more detailled information than "download and install
progname" to download and install a program.
- Download and install this version of emacs.
- Install the Apple supplied X11
- Download ESS from this directory (currently, this is the newest version), unpack it in a directory where you want to have your emacs-related files and libraries. My ESS resides in: ~/Library/emacs/ess-5.1.24
- Create a .emacs file in your home directory using a text editor, and add the following line:
(load "~/Library/emacs/ess-5.1.24/lisp/ess-site")
Although this should be sufficient to get emacs/ess and R working, I
refuse to work in emacs without the following modifications:
;; Delete-key things
(global-set-key [delete] 'delete-char)
(global-set-key [(control delete)] 'kill-word)
(global-set-key [(control backspace)] 'backward-kill-word)
;; For Carbon Emacs
(global-set-key [kp-delete] 'delete-char)
(global-set-key [(control kp-delete)] 'kill-word)
(global-set-key [?\C-\d] 'backward-kill-word)
(transient-mark-mode t) ; Make regions (aka blocks) highlight
(delete-selection-mode t) ; Typed text replaces a selection
(setq inhibit-startup-message t) ; Don't show startup screen
(require 'bs) ; Better buffer selection
(global-set-key [(control ?x) (control ?b)] 'bs-show) ; C-x C-b
(setq bs-must-always-show-regexp "^\\*R") ; Make sure R process buffers are always shown
(require 'recentf) ; Add recent opened files menu
(recentf-mode 1)
- Start the installed Emacs application, if it comes up with an
empty white screen, you're doing fine. Otherwise, the most probable
thing is that you didn't specify the path to the ess-site
correctly. Correct that, and try again.
- If emacs is running, try to start R by pressing: ESC x R
[return] (that are four individual keypresses!). Emacs will ask
you for a startup directy, just press enter.
- If everything works out OK, you'll be presented with the
familiar R startup message in the current buffer and you can skip
the next step.
- So, given that R didn't start without a problem, the most likely
cause is that ESS is not able to find the R executable. To solve
that, edit the ~/Library/emacs/ess-5.1.24/lisp/ess-site.el,
search for inferior-R-program-name (in my version of
ess-site, it's at line 253), remove the ;; in front of that
line, and change "R" to the path of your "R" (which can be
found out by typing which R on the command prompt of any
shell), for example: "/usr/local/bin/R". Quit Emacs, and start Emacs
again, and try ESC x R [return] again. It should work fine
now.
- If you haven't got X11 running, start it now.
- Type at the R prompt: plot(1:10). If everything worked
OK, you should see the graphical output. If not, try explicitly
opening a X11 window by typing X11(":0.0") before the
plot command.
- Voila! It's that simple.
Some useful tips:
- Open a file that ends in .R, e.g.,
test.R. Notice that the menu bar has a couple of extra
menus, specifically for interaction with R.
- Type some lines of R code, mark these lines of code and press
C-c C-r. They are automatically copied to the R windows and
evaluated. C-c C-l does the same for one line of text,
C-c C-f for the function that the cursor is currently in.
- First open the file that you want to edit, then start R. This
way, the working directory is automatically set to the file's
directory.
- Create a split-buffer view by pressing C-x 2, then you
can have the *R* and source code buffers visible at the
same time. (Switch betweeen windows by pressing C-x o.
- One can drop files in any Emacs window to start editing them.
Good Luck
hvr /ad/ van-rijn.org
Last modified: Wed Jul 16 22:17:55 EDT 2003