ESS and rpy2 sharing R process in Emacs

286 views Asked by At

I am a fairly new user of R and have started using Emacs Speaks Statistics (ESS). I also use rpy2 (a python interface to R) where I can start an R-process in my ipython shell running in Emacs.

Is it possible to let ESS and rpy2 somehow share the same R process so that I could do a little bit coding in Python/rpy2 to set up some R objects, then switch to ESS and run R commands directly without using python/rpy2?

For example, I would either start R within Emacs using M-x R, and then in my ipython session let rpy2.robjects.r point to or use this running R process; or alternatively I would start R in python with rp2.robjects.r and then get ESS to connect to the python-embedded R process.

I guess the problem is that the R process in rpy2 is embedded in the python interpreter, and also that the python objects representing R objects would become "out of sync" if I could manipulate R directly without going through rpy2? I acknowledge I don't have complete insight into how rpy2 and Emacs process-handling works...

2

There are 2 answers

0
Spacedman On BEST ANSWER

What you want is some kind of R console built on rpy2 technology. This isn't too hard to build.

I imagine there's some rpy2 interface to R's 'eval' function, so then you just need some python code to read lines and feed them to rpy2.eval(line) or whatever. That would then get all wrapped up in a nice console-style interface with command-line recall and editing etc.

When done with interactive R, you'd quit the console with some code (not 'q()'!) and get back your python interpreter.

I don't think this thing has been built yet, but similar things have been. The 'manageR' plugin for Quantum GIS (mapping s/w) provides an R console that is launched from python and uses rpy.

If you are really set on leveraging ESS for this then you'd need some way of sending ESS's inputs to a listening python process to feed to an R 'eval' function. Bit beyond me.

0
lgautier On

As answered by Spacedman, building an R console using Python+rpy2 might be needed.

However, the easiest might be to alias this to "R" before running Emacs. This way "Meta-x R" in Emacs would call it rather than the "real" R console. That's only for one part; if you wish to have both your ess R-console and your python session plug to the same R you will have to implement a client-server layer of some sort.

Related to that, Nathaniel implemented a real nice feature in his rnumpy (layer on the top of rpy2, compatible only with rpy2-2.0.x I think). When used with ipython one can switch to R-like console and back to ipython Python console. It is possible to run ipython through Emacs, and may be also possible to activate the ess-R mode when switching to R.