21

I am running a simulation study in R. Occassionally, my simulation study produces an error message. As I implemented my simulation study in a function, the simulation stops when this error message occurs. I know that it is bad practice to suppress errors, but at this moment to me there is no other option than to suppress the error and then go on with the next simulation until the total number of simulations I like to run. To do this, I have to suppress the error message R produces.

To do this, I tried different things:

library(base64)
suppressWarnings
suppressMessages
options(error = expression(NULL))

In the first two options, only warnings and message are suprressed, so that's no help. If I understand it correctly, in the last case, all error messages should be avoided. However, that does not help, the function still stops with an error message.

Has someone any idea why this does not work the way I expect it to work? I searched the internet for solutions, but could only find the above mentioned ways. In the function I am running my simulation, a part of the code is analysed by the external program JAGS (Gibbs sampler) and the error message is produced by this analysis. Might this be where it goes wrong?

Note that I do not have to supress a certain/specific error message, as there are no other error messages produced, it is 'good enough' to have an option that supresses just all error messages.

Thanks for your time and help!

Inga
  • 303
  • 1
  • 4
  • 12
  • Thanks for your quick answer. I used try(simulation(x,y,z)) (with in brackets my own function), but that would not work either. I also used options(show.error.messages = FALSE) with no succes. – Inga Oct 01 '13 at 12:36
  • If you don't show us your code or the exact errors, how can we help? – Carl Witthoft Oct 01 '13 at 13:45

2 Answers2

16

As suggested by the previous solution, you can use try or tryCatch functions, which will encapsulate the error (more info in Advanced R). However, they will not suppress the error reporting message to stderr by default.

This can be achieved by setting their parameters. For try, set silent=TRUE. For tryCatch set error=function(e){}.

Examples:

o <- try(1 + "a")
>  Error in 1 + "a" : non-numeric argument to binary operator
o <- try(1 + "a", silent=TRUE)  # no error printed

o <- tryCatch(1 + "a")
> Error in 1 + "a" : non-numeric argument to binary operator
o <- tryCatch(1 + "a", error=function(e){})
Megatron
  • 15,909
  • 12
  • 89
  • 97
7

There is a big difference between suppressing a message and suppressing the response to an error. If a function cannot complete its task, it will of necessity return an error (although some functions have a command-line argument to take some other action in case of error). What you need, as Zoonekynd suggested, is to use try or trycatch to "encapsulate" the error so that your main program flow can continue even when the function fails.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • 1
    Thanks for your responses. I did not provide my code as it consists of a function that calls another function from a source in a for loop, therefore the code is quite long. Furthermore, as becomes clear in the answer from Carl Witthoft, my problem was a missconception: I thought that the options and functions I tried (see above) were suppossed to suppress the **response** to an error. In the end, what worked for me was calling the function where the error occurs with try() and then using the next option in the for loop from where I call the function. – Inga Oct 02 '13 at 10:58
  • @Inga: I have the same or similar problem but I can't catch your solution. It would be nice if you you provide a minimal example for your solution. – giordano Jul 27 '14 at 07:23
  • 1
    Of course. I called a function in a for loop -N- times. The function did an analysis (JAGS), but sometimes an error occured. To prevent this, I adjusted the for loop like this: for (i in 1:N){ results[[i]] <- try(Jags.Analysis(Npairs = Npairs...) if(class(results[[i]]) == "try-error") next – Inga Aug 04 '14 at 08:15
  • I did not realize that you can edit your response only for 5 minutes. What the code does is not to prevent the error but to make R not jump out of the for loop but start with the next iteration when the error occurs. So when in a for loop of N = 5 times the error occurs3 times, only the results of 2 analyses will be saved. If the syntax is not clear due to the lack of formatting (or something else), just let me know! – Inga Aug 04 '14 at 08:24
  • 2
    @Carl Witthoft: In my case, I get errors printed to screen but everything runs fine (yes, indeed quite a strange situation). Can I suppress these errors somehow, similar to how I can suppress warnings with `options(warn = -1)`? I wish there was like a `options(err = -1)` command or something like that! This would be helpful in suppressing the *message* but not necessarily the response. **NOTE:** Neither `options(error = expression(NULL))` nor `suppressMessages` worked for me. – warship Apr 29 '16 at 03:07
  • @warship without seeing your code, no way to tell what's going on. You might want to post your specific problem as a new question, with a bit of reproducible code & data. – Carl Witthoft Apr 29 '16 at 11:12