views:

83

answers:

5

Hello all,

I have a small shell script (bash) which runs a R script which produces a plot as output. Everything works fine but immedietly after the plot is rendered R quits. Is there a way to keep the R session alive until the plot window is closed.

The shell script.

#!/bin/bash
R --slave --vanilla < myscript.r

And the R script.

daq = read.table(file('mydata.dat'))
X11()
pairs(daq)
//R Completes this and then exits immediately.

Thanks in advance for any help!

+1  A: 

This is not a perfect solution, but you may call locator() just after the plot command.
Or just save the plot to pdf and then invoke pdf viewer on it using system.

mbq
+3  A: 

If you use the Rscript command (which is better suited for this purpose), you run it like this:

#!/usr/bin/Rscript

daq = read.table(file('mydata.dat'))
X11()
pairs(daq)

message("Press Return To Continue")
invisible(readLines("stdin", n=1))

Make sure to set the execute permission on myscript.r, then run like:

/path/to/myscript.r

or without the shebang:

Rscript /path/to/myscript.r
Mark
This is not good, since OP wanted the script to terminate after the plot is closed, not after a key is pressed.
mbq
This works well enough, I just needed the display to stay open long enough so that the plot could be viewed after the shell script completes.
freyrs
A: 

One solution would be to write the plot out to pdf instead:

pdf(file="myplot.pdf")

##your plot command here
plot( . . . )

dev.off()
chrisamiller
A: 

More important question is why do you want R to run after graph creation? Use it either in interactive mode or in batch mode... I don't understand what do you want to accomplish. Besides, try littler, it's located in Ubuntu repos (universe repos, if I'm correct), or Rscript, so rewrite your script and name it myscript.r, and be sure to put correct path in the first line. Try whereis Rscript (usually /usr/bin/Rscript). Forget about bash script. You can pass --vanilla and --slave arguments to Rscript, but I don't see the purpose... O_o

aL3xa