The most common place I've seen messages used in R is in the starting of a package. Suppressing one function's worth of messages is easily accomplished with suppressMessages
as discussed here: Disable Messages Upon Loading Package in R. It is also possible to suppress multiple lines of message generating function calls by embedding {}
inside of the supressMesssages
function call. However, if you have a full script with messages happening here and there, is there anyway to disable them completely? I'm looking for something like option(warn=-1) but for messages. Please note that sink
doesn't quite do what I want because it redirects all output... I would like to keep output from print
but not hold onto output from message
.
The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() . Ignore messages with suppressMessages() .
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 .
Use the type parameter in sink
# Open a file to send messages to
zz <- file("messages.Rout", open = "wt")
# Divert messages to that file
sink(zz, type = "message")
message("not gonna show up in console")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With