Invoke interrupt from R code

I can propose a partial solution, which relies on the tools package.

invokeInterrupt <- function() {
  require(tools)
  processId <- Sys.getpid() 
  pskill(processId, SIGINT)
}

However, be aware that throwing the interrupt signal (SIGINT) with pskill doesn't appear to be very robust. I ran a few tests by sending the exception and catching it with your function, like so:

will_interrupt <- function() {
    Sys.sleep(3)
    invokeInterrupt()
    Sys.sleep(3)
}

r = tryCatch2(will_interrupt())

On linux, this worked well when executed from the R commandline. On windows, the R commandline and R Gui did close when executing this code. There is worse: on both linux and windows, this code crashed Rstudio instantly...

So, if your code is to be executed from the R commandline on Linux, this solution should be OK. Otherwise you might be out of luck...


Late answer but I have found that rlang::interrupt can throw "user interrupts":

interrupt() allows R code to simulate a user interrupt of the kind that is signalled with Ctrl-C. It is currently not possible to create custom interrupt condition objects.

Source: ?rlang::interrupt

Internally it calls the R API function Rf_onintr which is an alias for the function onintr.

Basically an interrupt is "just" a special condition with these classes: interrupt and condition (see the R source code).

If you just want to simulate an interrupt to test tryCatching (without the need to interrupt a running R statement) it suffice to throw a condition with these classes via signalCondition:

interrupt_condition <- function() {
  structure(list(), class = c("interrupt", "condition"))
}

tryCatch(signalCondition(interrupt_condition()),
                         interrupt = function(x) print("interrupt detected"))
# [1] "interrupt detected"

Tags:

Try Catch

R