Invoke interrupt from R code

后端 未结 2 1379
攒了一身酷
攒了一身酷 2021-02-15 13:49

I have a generic function to catch all exceptions included in my package logR::tryCatch2 defined as:

tryCatch2 <- function(expr){
    V=E=W=M=I=         


        
2条回答
  •  忘了有多久
    2021-02-15 14:24

    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"
    

提交回复
热议问题