0

I'm running a complex but relatively quick simulation in R (takes about 5-10 minutes per simulation) and I'm beginning to run it in parallel with various input values in order to test the robustness of some of my algorithms.

There seems to be one problem: some arrangements of inputs cause a fatal error within the simulation and the whole code comes crashing down, causing the simulations to end. Is there an easy way to catch the error (which may come from a variety of locations) and have it just ignore those input values and move on to the next?

It's frustrating when I set an array of inputs to check that should take 5-6 hours to run through all the simulations and I come back to find that it crashed in the first 45 minutes.

While I work on trying to fix the bug / identify inputs that push me to that error, any ideas on how to ignore / catch the errors as they come?

Thanks

jameselmore
  • 442
  • 9
  • 20
  • Wrap in a `try` or `tryCatch` – James Jul 24 '13 at 15:10
  • Take a look at `tryCatch`, for example [here](http://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r) or [hadley's tutorial](https://github.com/hadley/devtools/wiki/Exceptions-Debugging#using-trycatch). – Thomas Jul 24 '13 at 15:11
  • @Thomas -- link to hadley's tutorial is a dead end. – swihart Oct 01 '14 at 17:57
  • 1
    @swihart http://adv-r.had.co.nz/Exceptions-Debugging.html – Thomas Oct 01 '14 at 18:10
  • @Thomas I edited the link at Hadley's github ("hadley's tutorial" in your comment) changing "debugging" into "Debugging", so now it's good everywhere. Thanks. – swihart Oct 02 '14 at 16:16

1 Answers1

1

I don't know how did your organize your simulations, but I guess uu have a loop where you check use new arguments at each step.

You can use tryCatch . Here I am throwing an error if I have bad input.

step.simul <- function (x) {
  stopifnot(x%%2 == 1, x>0)
  (x - 1)/2
}

Then using tryCatch, I flag the bad inputs with a code that tells you about the bad input:

sapply(1:5, function(i)tryCatch(step.simul(i), error=function(e)-1000-i))
[1]     0 -1002     1 -1004     2

As you see my simulations runs over all the loop index.

agstudy
  • 119,832
  • 17
  • 199
  • 261