[ESS] submit region to R process on stdin

Dan Davison davison at stats.ox.ac.uk
Mon Jul 28 17:15:19 CEST 2008


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?

Secondly, the above defun clearly has some problems, which I'd like to
ask for help on (but I admit aren't strictly related to ESS). I've
added the 'sleep 60' command to make the plot stay around. However
that means that one has to use C-g to get control back to the emacs
process. I tried adding an & to background the shell-command, but that
results in

Error in read.table(file = file, header = header, sep = sep, quote = quote,  :
  no lines available in input
Calls: plot -> read.csv -> read.table
Execution halted

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?

Thanks,

Dan




More information about the ESS-help mailing list