[ESS] submit region to R process on stdin

Stephen Eglen S.J.Eglen at damtp.cam.ac.uk
Mon Jul 28 23:26:31 CEST 2008


Dan Davison <davison at stats.ox.ac.uk> wrote:

> Dear ESS list,
> 
> I'm trying to write an elisp function which will transform the current
> region into a data frame in R, and call a function with that data
> frame as an argument. The motivation was that on the emacs org-mode
> (highly recommended!) mailing list, there was a request to create
> plots from org-mode's ASCII tables. So far I've come up with the
> following, which assumes that region contains csv-format tabular data
> and passes it to Rscript on stdin.
> 
> (defun r-function-on-region (&optional R-function)
> "Convert region to data frame in R and apply a function to this data frame"
>   (interactive)
>   (unless R-function (set 'R-function "plot"))
>   (shell-command-on-region
>    (point-min) (point-max)
>    (concat "Rscript -e 'X11() ; " R-function "(read.csv(\"stdin\")) ; system(\"sleep 60\")'")))
> 
> 
> My first question is, should I be using ESS for this rather than
> Rscript? How hard would it be to write an equivalent function, which
> passes region to a running R process, which would convert it to a data
> frame and pass that data frame to an arbitrary function (defaulting to
> 'plot'). ess-r-call-fun-with-region-as-input or something. Could
> anybody give some pointers on how that would be done?

Rscript might be the way to go, esp if the user is not familiar with
ESS, as then it could all happen without the user installing ESS.  

However, if I were coding it to take advantage of ESS, I'd suggest that
your top level elisp function do something like:

- save current region to a tmp file, say /tmp/org.csv
- get ESS to exec the command orgplot('/tmp/org.csv') using 
  (ess-execute 'orgplot(...)') or similar.

where orgplot is an R function that readss in data from the file and
then plots it.  

> Does anyone have some suggestions for how this should be done? Is
> there some way to get Rscript to pop up an (X11) window and then go
> away, leaving the window?

Not sure about that, couldn't see anything obvious in the ?Rscript page;
what about making a pdf, and then using a separate command to launch a
viewer (xpdf) in the background?  Might be more useful also if you
wanted to then print the plot.

Stephen




More information about the ESS-help mailing list