myfun <- function(x, warn=FALSE){
message('This function is rather chatty')
cat("It even prints in different output forms!\n")
message('And even at different....')
cat("...times!\n")
if(warn)
warning('It may even throw warnings!')
x
}
out <- myfun(1)
out
# tell the function to shhhh
out <- quiet(myfun(1))
out
# which messages are suppressed? Extract stored attribute
out <- quiet(myfun(1), keep = TRUE)
attr(out, 'quiet.messages')
# Warning messages still get through (see manageWarnings(suppress)
# for better alternative than using suppressWarnings())
out2 <- myfun(2, warn=TRUE) |> quiet() # warning gets through
out2
# suppress warning message explicitly, allowing others to be raised if present
myfun(2, warn=TRUE) |> quiet() |>
manageWarnings(suppress='It may even throw warnings!')
Run the code above in your browser using DataLab