Nino Hardt
2023-Feb-19 14:37 UTC
[Rd] R: determine if `suppressMessages()` has been invoked
Hi all, I would like to create a function that detects if suppressMessages has been invoked upon running that same function. I was looking through {https://cran.r-project.org/doc/manuals/r-release/R-ints.html}, but I haven't found an answer. I do not understand **how** suppressMessages works. I have not received an answer on SO (https://stackoverflow.com/questions/75036750/r-determine-if-suppressmessages-has-been-invoked), but it has been suggested I ask here. Nino [[alternative HTML version deleted]]
Ivan Krylov
2023-Feb-19 17:01 UTC
[Rd] R: determine if `suppressMessages()` has been invoked
On Sun, 19 Feb 2023 15:37:33 +0100 (CET) Nino Hardt <me at ninohardt.com> wrote:> I would like to create a function that detects if suppressMessages > has been invoked upon running that same function.Would you mind letting us know why? Just curious. Normally, I would just use message() for everything and let the users decide whether they want to see it.> I was looking through [R Internals], but I haven't found an answer. I > do not understand **how** suppressMessages works.It works by cooperating with message(). message() itself works by trying to raise a "message" condition and providing a "muffleMessage" restart that does nothing. If the condition wasn't handled (the "muffleMessage" restart wasn't called by the handler), the text of the message is printed. In turn, suppressMessages() sets up a handler for conditions of class "message" that invokes the "muffleMessage" restart provided by message() itself above. We can use the fact that the availability of the "muffleMessage" restart is a documented detail and check whether signalling a "message" condition will call this restart: are_messages_suppressed <- function() withRestarts( { signalCondition(simpleMessage('')) # we stay here if restart is not invoked FALSE }, muffleMessage = function() # we jump here if restart is invoked TRUE ) are_messages_suppressed() # [1] FALSE suppressMessages(are_messages_suppressed()) # [1] TRUE I don't think I understand handlers and restarts enough to explain them well, but the following link seems to be one of the defining documents for R's condition handling system: https://homepage.stat.uiowa.edu/~luke/R/exceptions/simpcond.html Hadley Wickham's Advanced R (first edition only) contains a good explanation of R's condition system: http://adv-r.had.co.nz/Exceptions-Debugging.html http://adv-r.had.co.nz/beyond-exception-handling.html (In my opinion, this could be a better question for R-help, since we ought to be using documented R APIs here.) -- Best regards, Ivan
Possibly Parallel Threads
- R: determine if `suppressMessages()` has been invoked
- Using IDs to suppress specific messages and warnings
- Using IDs to suppress specific messages and warnings
- RcppArmadillo compilation error: R CMD SHLIB returns status 1
- Print All Warnings that Occurr in All Parallel Nodes