I\'m running a large number of iterations in parallel. Certain iterates take much (say 100x) longer than others. I want to time these out, but I\'d rather not have to dig in
I like R.utils::withTimeout(), but I also aspire to avoid package dependencies if I can. Here is a solution in base R. Please note the on.exit() call. It makes sure to remove the time limit even if your expression throws an error.
with_timeout <- function(expr, cpu, elapsed){
expr <- substitute(expr)
envir <- parent.frame()
setTimeLimit(cpu = cpu, elapsed = elapsed, transient = TRUE)
on.exit(setTimeLimit(cpu = Inf, elapsed = Inf, transient = FALSE))
eval(expr, envir = envir)
}