On Sun, 19 Feb 2023 15:37:33 +0100 (CET) Nino Hardt <m...@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 ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel