I have been meaning to write a suppressConditions function to suppress every condition such as error, warning and message. For that purpose I have reviewed the source code of suppressMessage and suppressWarning functions and couldn't figure out why the classes argument only contains one condition and we cannot use a general function for both purposes:
I would like to know why this code works in muffling either message or warning condition, whereas the following code doesn't:
suppressConditions <- function(expr, condition = "message") {
withCallingHandlers(
condition = function(cons) {
if(inherits(cons, condition)) {
rlang::cnd_muffle(cons)
}
},
expr
)
}
suppressConditions(message("hello!")) # It works
suppressConditions <- function(expr, conditions = c("message", "warning")) {
withCallingHandlers(
conditions = function(cons) {
if(inherits(cons, conditions)) {
rlang::cnd_muffle(cons)
}
},
expr
)
}
> suppressConditions(message("hello!")) # It doesn't work
hello!
So in the end I would like to know how I can add error condition to this function so that it suppress all three conditions.
Thank you very much in advance.