I have a part of a function which later runs in a loop and I want to impose a time limit on a part of this function. It should return NAs in case the time limit is exceeded to prevent my whole program from running too long. The problem is that the optimisation, a part of the code, runs in C, so withTimeout() from R.utils doesn’t work.
The function I want to time-restrict is ugarchfit() from the rugarch package. The documentation indicated that the optimisation runs in C code for efficiency.
Code to be time restricted:
fit <- tryCatch(
{
ugarchfit(spec = spec,
data = data,
solver = 'hybrid')
},
error = function(e){
cat('nSolvers did not work for one observation (Index: ', index_name, ' Spec: ', spec_i, ', Dist: ', dist,'):', e$message, 'nn')
return(NA)
}
)
I’ve already tried to use withTimeout() from R.utils. This didn’t work because of the already mentioned problem that a part of the code runs in C and this function only works for R.
I also tried to use mcparallel() in combination with mccollect() and Sys.sleep() to circumvent the problem, but this didn’t work either.
Jan Möhle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.