1

I am using Python which calls a plot from R. Python issues the Rscript command. Everything works, except that the plot instantly disappears.

I tried several things on the R side:

  • par(ask=TRUE)
  • Sys.sleep(5)

par does not work this way; it will just ignore it.

With sleep the problem is that R will hang the python script for 5 seconds while sleeping, but also that sometimes I want to be able to close the plot immediately: when I do python just keeps waiting until the 5 seconds are over.

Could it be Python related, or is there a solution in R?

Minimum Working Example:

R:

foo.R

plot(1:10)
Sys.sleep(5)

Python:

foo.py

import os
os.system("Rscript foo.R")
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • It's because once `Rscript` completes execution, all the things it opened (like the plot window) close with `Rscript`. Unsure as to a workaround, but perhaps someone has one. – mathematical.coffee May 23 '13 at 00:04
  • 2
    Actually, check out [this question](http://stackoverflow.com/questions/3301694/running-r-scripts-with-plots) which will pause the script until a key is pressed, or [this question](http://stackoverflow.com/questions/3063165/r-building-a-simple-command-line-plotting-tool-capturing-window-close-events) which will pause the script until the plot window is destroyed (using `tcltk`). – mathematical.coffee May 23 '13 at 00:07
  • First one did not work unfortunately, `message` and `invisble` do not help. – PascalVKooten May 23 '13 at 00:10
  • Is it necessary to have the plot stay open while the script is running? Can you just rewrite your plotting code in the R script so it gets saved as an image? – Marius May 23 '13 at 00:10
  • @Dualinity, the point of the first question I linked was `readLines`, not `message` or `invisible`. `readLines` will wait for user input (on the keyboard) before continuing. – mathematical.coffee May 23 '13 at 02:08

1 Answers1

0

Thanks to mathematical-coffee as from the comments and thanks to Dirk for a similar answer in another thread:

> library(tcltk)
> tk_messageBox(message="Press a key")
Community
  • 1
  • 1
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • There isn't really any point in replicating answers as new posts -- I suggest you delete this, and maybe even the question which wasn't novel either. – Dirk Eddelbuettel May 23 '13 at 00:56
  • @DirkEddelbuettel The question comes from another perspective (python), but I guess that then does not matter? – PascalVKooten May 23 '13 at 06:22