33

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!

Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56

5 Answers5

33

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
  • 106,305
  • 20
  • 172
  • 230
  • 4
    This is not good, since OP wanted the script to terminate after the plot is closed, not after a key is pressed. – mbq Jul 21 '10 at 21:00
  • 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. – Stephen Diehl Jul 22 '10 at 04:05
  • 3
    This doesn't work for me. A graph never appears... (on windows) – Matthew Lueder Sep 12 '16 at 23:58
  • This doesn't refresh the window, so you can't resize either. – csl May 22 '17 at 20:35
  • The above works perfectly for a regular plot() call. Tried this method to open the graphics window with a leaflet map and the graphics window opens, but the map doesn't render. Any thoughts? – msoderstrom Feb 11 '19 at 11:18
18

You could add a loop that checks for the graphical device every n seconds:

while (!is.null(dev.list())) Sys.sleep(1)

This will sleep until you close the plot window.

Alex Deckmyn
  • 1,017
  • 6
  • 11
  • This is much better, end even lets the user resize the window and so on. – csl May 22 '17 at 20:37
  • This works fine, but I am confused. Should the x11() device not close first and then the loop should run. Where do I misunderstand ? – user2338823 Apr 24 '19 at 10:04
  • This loop runs while the device is running. Every second it checks whether the decice is still open, and if so, it sleeps again for 1 second. The result is that the script will stay in the loop until you manually close the graphical device (at that point, dev.list() becomes NULL and the loop exits). – Alex Deckmyn Apr 24 '19 at 10:07
3

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
  • 18,510
  • 6
  • 49
  • 72
3

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

pdf(file="myplot.pdf")

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

dev.off()
chrisamiller
  • 2,712
  • 2
  • 20
  • 25
  • similar solution has been offered [here](https://stackoverflow.com/a/42545005/4999991) outputting to pictures. It is a nice workaround. – Foad S. Farimani May 10 '18 at 00:44
-2

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
  • 35,415
  • 18
  • 79
  • 112