I am trying to use R within a script that will act as a simple command line plot tool. I.e. user pipes in a csv file and they get a plot. I can get to R fine and get the plot to display through various temp file machinations, but I have hit a roadblock. I cannot figure out how to get R to keep running unt开发者_如何学Cil the users closes the window.
If I plot and exit, the plot disappears immediately. If I plot and use some kind of infinite loop, the user cannot close the plot; he must exit by using an interrupt which I don't like. I see there is a getGraphicsEvent function, but it claims that the device is not supported (X11). Anyway, it doesn't appear to actually support an onClose event, only onMouseDown.
Any ideas on how to solve this?
edit: Thanks to Dirk for the advice to check out the tk interface. Here is my test code that works:
require(tcltk)
library(tkrplot)
## function to display plot, called by tkrplot and embedded in a window
plotIt<-function(){ plot(x=1:10, y=1:10) }
## create top level window
tt<-tktoplevel()
## variable to wait on like a condition variable, to be set by event handler
done <- tclVar(0)
## bind to the window destroy event, set done variable when destroyed
tkbind(tt,"<Destroy>",function() tclvalue(done) <- 1)
## Have tkrplot embed the plot window, then realize it with tkgrid
tkgrid(tkrplot(tt,plotIt))
## wait until done is true
tkwait.variable(done)
You need something with a distinct event loop --- and the best portable solution is to rely on the (already included) tcltk
package. Start with its demos.
The simplest case may be
> library(tcltk)
> tk_messageBox(message="Press a key")
which pops a box you need to acknowledge to proceed.
精彩评论