[R] Handling interrupts in long-running R functions

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Fri Sep 10 16:43:18 CEST 2021

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,

More information about the R-help mailing list