Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress error message in R

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!

like image 856
Inga Avatar asked Oct 01 '13 08:10

Inga


People also ask

How do I turn off error messages in R?

To suppress the warning message, we can set warn=2 in the options function, or we can use suppressWarnings instead to mute the warning message. On the other hand, we can also use the stopifnot function to check whether the argument is valid or not.

How do I not show error in R markdown?

In R Markdown, error = FALSE is the default, which means R should stop on error when running the code chunks.

How do you handle errors in R?

In R Programming, there are basically two ways in which we can implement an error handling mechanism. Either we can directly call the functions like stop() or warning(), or we can use the error options such as “warn” or “warning. expression”.

How does tryCatch work in R?

The tryCatch() function in R evaluates an expression with the possibility to catch exceptions. The class of the exception thrown by a standard stop() call is try-error. The tryCatch() function allows the users to handle errors. With it, you can do things like: if(error), then(do this).


2 Answers

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){})
like image 184
Megatron Avatar answered Sep 17 '22 16:09

Megatron


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.

like image 21
Carl Witthoft Avatar answered Sep 21 '22 16:09

Carl Witthoft