submit region to R process on stdin

View: New views
2 Messages — Rating Filter:   Alert me  

submit region to R process on stdin

by Dan Davison :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

______________________________________________
ESS-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/ess-help

Re: submit region to R process on stdin

by Stephen Eglen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dan Davison <davison@...> 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

______________________________________________
ESS-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/ess-help
LightInTheBox - Buy quality products at wholesale price!