Hello,
Does anyone know a way of interrupting a loop by pressing a key (besides
ctrl-c)?
My problem is the following:
I have a machine acquiring data and saving text files into a directory.
I have an R script that read those files, process them and plots the
results.
What I would like to do is: process the data as the files appear in the
folder (I've done that do with a loop that checks the number of files)
and when the machine stops acquiring the data, break the loop and do
some other calculations with the entire dataset.
I've tried a couple of solutions:
1) Setting a timer that, if certain amount of time goes by and no new
file is created, the loop is broken and the remaining calculations
occur. However, this solution is dependent on the machine acquiring
time, which change with different experiments...
2) I've tried this structure:
f.loop<-function()
{
repeat
{
print("initial calculations")
switch(menu(c("go","stop")),next,break)
}
print("final calculations")
This also works but requires user intervention at each cycle to chose
from "go" and "stop"...
So... back to the initial question. Can I break the cycle with a signal
from the keyboard?
Something like this:
while (Idontpressthe_Y_key)
{
(1) process all files already produced
(2) wait for new files that are yet to be produced
(3) see if I am pressing key Y (go to (1) if I am not, and quit the loop
if I am)
}
Many thanks in advance,
Bruno
------------------------
Bruno Jesus
Centro de Oceanografia
Campo Grande
1740-016 Lisboa
Portugal
Bruno Jesus wrote:> So... back to the initial question. Can I break the cycle with a signal > from the keyboard?The following code, slightly modified from: http://www.sciviews.org/_rgui/tcltk/OKCancelDialog.html lets you interrupt an analysis by clicking on a dialog box, using the tcltk package. Yes, I know it's not a keypress, but maybe this will do. You can probably map a keypress to the TclTk dialog anyway, or do something really tricky with Tk events... Copy the following into a file, test.R, make sure lines aren't badly broken, and source("test.R"). If you are using Emacs and ESS then you may have to thump Ctrl-G to wake up emacs to what is happening... require(tcltk) # Load the TclTk package tt <- tktoplevel() # Create a new toplevel window tktitle(tt) <- "Simple Dialog" # Give the window a title # Create a variable to keep track of the state of the dialog window: # If the window is active, # done = 0 # If the window has been closed using the OK button, # done = 1 # If the window has been closed using the Cancel button or destroyed, # done = 2 done <- tclVar(0) # tclVar() creates a Tcl variable # Create two buttons and for each one, set the value of the done # variable # to an appropriate value OK.but <- tkbutton(tt, text = " OK ", command = function() tclvalue(done) <- 1) Cancel.but <- tkbutton(tt, text = "Cancel", command = function() tclvalue(done) <- 2) # Place the two buttons on the same row in their assigned window (tt) tkgrid(OK.but, Cancel.but) # Capture the event "Destroy" (e.g. Alt-F4 in Windows) and when this # happens, # assign 2 to done tkbind(tt, "<Destroy>", function() tclvalue(done) <- 2) tkfocus(tt) # Place the focus to our tk window for(i in 1:1000){ # now do our 1000 iterations of whatever, checking the 'done' variable # to see # if the user clicked the dialog while we were busy. cat("Iteration ",i,"\n") Sys.sleep(1) doneVal <- as.integer(tclvalue(done)) # Get Tcl variable if(doneVal != 0)break } tkdestroy(tt) # Test the result if (doneVal == 0) tkmessageBox(message = "Analysis finished okay") if (doneVal == 1) tkmessageBox(message = "You interrupted the analysis") if (doneVal == 2) tkmessageBox(message = "You either pressed Cancel or destroyed the dialog!")
On 1/15/2008 1:18 PM, Bruno Jesus wrote:> Hello, > > Does anyone know a way of interrupting a loop by pressing a key (besides > ctrl-c)? > > My problem is the following: > I have a machine acquiring data and saving text files into a directory. > I have an R script that read those files, process them and plots the > results. > What I would like to do is: process the data as the files appear in the > folder (I've done that do with a loop that checks the number of files) > and when the machine stops acquiring the data, break the loop and do > some other calculations with the entire dataset. > > I've tried a couple of solutions: > 1) Setting a timer that, if certain amount of time goes by and no new > file is created, the loop is broken and the remaining calculations > occur. However, this solution is dependent on the machine acquiring > time, which change with different experiments... > > 2) I've tried this structure: > f.loop<-function() > { > repeat > { > print("initial calculations") > switch(menu(c("go","stop")),next,break) > > } > print("final calculations") > > This also works but requires user intervention at each cycle to chose > from "go" and "stop"... > > So... back to the initial question. Can I break the cycle with a signal > from the keyboard? > > Something like this: > > while (Idontpressthe_Y_key) > { > (1) process all files already produced > (2) wait for new files that are yet to be produced > (3) see if I am pressing key Y (go to (1) if I am not, and quit the loop > if I am) > }You can do this sort of thing fairly easily with tcltk (and presumably with other GUI packages too, but I don't know how). You'll need to read the TCL/TK manual (which is distributed with at least the Windows version of R) and translate those instructions into the R syntax. For a vaguely related example, see the tkcanvas demo in the tcltk package; for another, see the spin3d or spinControl functions in the tkrgl package. Duncan Murdoch
Bruno Jesus a ?crit :> Does anyone know a way of interrupting a loop > by pressing a key (besides ctrl-c)?No hit key solution sorry, but may help : if (file.exists('zefile')) stop() allows to stop given zefile is created