I'm trying to run a simulation in R where I make a whole bunch of phylogenetic trees. The tree simulation is a bit of a problem because its run time is highly variable, sometimes 0.005 seconds and sometimes minutes. I want to avoid the slow trees so I'm trying to use evalWithTimeout to skip them. So far I'm having problems because I can't make it kill slow tasks without also killing the loop. My problem is similar to this question but the solution to that question hasn't helped me.
library(TreeSim)
library(R.utils)
for (i in 1:100){
tryCatch(
expr = {
evalWithTimeout(sim.rateshift.taxa(10,1,c(0.5,2),c(0.5,0),
c(1,1),c(0,0.5),complete=F),
timeout=0.005)
},
TimeoutException = function(ex) cat("Timeout. Skipping.\n")
)
print(i)
}
This is what I have so far. I would like to it continue printing "i" regardless of whether the simulation goes over the time limit, but currently it gives me "reached CPU time limit" errors and stops.