# Run a remote R session in emacs: emacs + ESS + R + ssh

June 1, 2009
By

(This article was first published on Super Nerdy Cool » R, and kindly contributed to R-bloggers)

I don’t know how, but somehow, i stumbled on how to do this.

I’ve always used emacs with ESS to run R, since spring 2006 (did it on windows, switched to linux, for years, and most recently, on my macbook). I liked this because i get the same usual interface across ALL platforms (well, maybe not on an MVS mainframe, but i’ve never even seen R on one of those things). Plus, i use emacs for everything computery and/or scientific, like using python or code in C (well, i hope to do more of this (do i really hope to?)).

Regarding the same interface across all platforms, i use, for example, emacs + ESS + R whenever i remotely log into the ics servers at uci (well, at least anything computationally intensive). So i ssh into it, fire up emacs, fire up R. Been doing this since whenever. Thing is, I usually always write my code on my local computer, and when i’m ready to run the final code, i either run it as a batch script (‘nohup’, and with ‘&’), through the ‘screen’ command (to keep the session runnning when i log out, see my post on R with unix tools), or through emacs. These days, i’ve been doing it with screen mainly so disconnects to the server won’t interrupt my script.

Well, I just found yet another way to do this: write code on local computer, send code to R session easily in emacs (ie not copy and paste). Oh I remember now, I ran into this by googling ‘emacs ess multiple R session’.

Go here, go to section 3.3, ESS on remote computers. We will need this file. This site clarified how to get graphics to show.

Instruction is as follows.

2. install it your usual way. or, for example, place it in ~/elisp. In your .emacs file, add:
;; add path to emacs
;; http://edward.oconnor.cx/2005/09/installing-elisp-files

;; load ssh.el file from elisp
;; this is to run ESS remotely on another computer in my own emacs, or just plai
;;
(require 'ssh)

1. fire up emacs. type ‘M-x ssh’. For host, do something like ‘-X -C [email protected]’ (X is for X windows forwarding, C is for compression of graphics, so plots can be displayed faster). Type in password.
2. you wil be logged into a shell session of your server. Fire up R, by typing R then enter. R is now running one of your emacs buffer. type ‘M-x ess-remote’. For dialect, select ‘r’.
3. open up any *.R file on your computer, and use the usual hot keys to send them to remote R session.

This is super cool huh?!?! I like! Alternatively, we can do the same without the ssh.el file. In emacs, hit ‘M-x shell’. In this shell, do your ssh -X -C stuff, then run R. Do ‘M-x ess-remote’ and everything should still work!

Next thing to do, with my local emacs, open files that are remote. this should be easy i think.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...