Hello everyone, I'm writing an R function that may be running for "long" periods of time (think tens of minutes), and I would like to be able to tell it: "please stop what you're doing and return the not-yet converged results as they are for inspection". The behaviour I'm striving for is 1) User presses interrupt 2) Function handles the interrupt and returns as if the convergence test passed 3) By some black magic, the interrupt condition is raised on the previous function call level (to avoid the situation where my function is called in a loop by some other function and the user wants to interrupt the whole process, not just my function). Is this a good idea? Is (3) even possible? (I guess I could check the length of sys.parents() and avoid recovering from the interrupt if called from some other function, but that feels dirty.) Could something similar be achieved with options(error = recover) and R restarts? Are there other ways of, well, interrupting the execution of R functions without changing the semantics of interrupts in R? I've been working with MATLAB lately (when in Rome, do as Romans do...), and their idiom for my desired behaviour is "create a plot window and return when that window is closed", but that doesn't translate well to R. -- Best regards, Ivan
iuke-tier@ey m@iii@g oii uiow@@edu
2021-Sep-10 15:02 UTC
[R] [External] Handling interrupts in long-running R functions
Some variation of this might do it: tryCatch(for (i in seq_len(1000000)) Sys.sleep(1), interrupt = function(e) i) If you want the option to inspect and continue you would need to use withCallingHandlers and invoke a 'resume' restart. Best, luke On Fri, 10 Sep 2021, Ivan Krylov wrote:> Hello everyone, > > I'm writing an R function that may be running for "long" periods of > time (think tens of minutes), and I would like to be able to tell it: > "please stop what you're doing and return the not-yet converged results > as they are for inspection". > > The behaviour I'm striving for is > > 1) User presses interrupt > 2) Function handles the interrupt and returns as if the convergence > test passed > 3) By some black magic, the interrupt condition is raised on the > previous function call level (to avoid the situation where my > function is called in a loop by some other function and the user > wants to interrupt the whole process, not just my function). > > Is this a good idea? Is (3) even possible? (I guess I could check the > length of sys.parents() and avoid recovering from the interrupt if > called from some other function, but that feels dirty.) Could something > similar be achieved with options(error = recover) and R restarts? Are > there other ways of, well, interrupting the execution of R functions > without changing the semantics of interrupts in R? > > I've been working with MATLAB lately (when in Rome, do as Romans > do...), and their idiom for my desired behaviour is "create a plot > window and return when that window is closed", but that doesn't > translate well to R. > >-- Luke Tierney Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Dear Ivan Krylov, Re:> On 10 Sep 2021, at 16:43, Ivan Krylov <krylov.r00t at gmail.com> wrote: > > Hello everyone, > > I'm writing an R function that may be running for "long" periods of > time (think tens of minutes), and I would like to be able to tell it: > "please stop what you're doing and return the not-yet converged results > as they are for inspection". > > The behaviour I'm striving for is > > 1) User presses interrupt > 2) Function handles the interrupt and returns as if the convergence > test passed > 3) By some black magic, the interrupt condition is raised on the > previous function call level (to avoid the situation where my > function is called in a loop by some other function and the user > wants to interrupt the whole process, not just my function). > > Is this a good idea? Is (3) even possible? (I guess I could check the > length of sys.parents() and avoid recovering from the interrupt if > called from some other function, but that feels dirty.) Could something > similar be achieved with options(error = recover) and R restarts? Are > there other ways of, well, interrupting the execution of R functions > without changing the semantics of interrupts in R? > > I've been working with MATLAB lately (when in Rome, do as Romans > do...), and their idiom for my desired behaviour is "create a plot > window and return when that window is closed", but that doesn't > translate well to R. > > -- > Best regards, > IvanJust a crazy idea: the sound input of a Mac/PC might serve as an interrupt. Record a short bit and assess the amplitude: moderate = signal, loud = break. See my source code below. This works on my MacBook Air with Mojave. When I snap my fingers, the amplitude is moderate (3 through 10), when tapping or scratching the microphone, amplitude is over 20. Hoping this might help, With best regards, Franklin ------ R source code: # sound as interrupt.R # with package audio # 15-09-2021 # ===================# NB First switch audio input to the wanted device # (microphone, sound input or 3rd party device) # flick your fingers = signal (m about 3 through 10) # tap the microphone -> stop (m > 20) library(audio) samp.rate = 44100 rectime = 1 # second nsamples = samp.rate * rectime repeat { sample <- record(2*nsamples, rate=44100, channels=2) wait(sample) # wait for the recording to finish sound <- sample$data # get the result m = max(sound) print(m) if (m > 0.5) alarm() # or the desired routine if (m > 20) {save.wave(sound, "sound from audio input.wav"); break} # stop program } # end Franklin Bretschneider Dept of Biology Utrecht University f.bretschneider at uu.nl