Suppress error message in R

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){})

Leave a Comment