[Rd] R: determine if `suppressMessages()` has been invoked

2023-02-19 Thread Nino Hardt
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]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] R: determine if `suppressMessages()` has been invoked

2023-02-19 Thread Nino Hardt
Awesome, this gets the job done.
 
To answer your question:
When using C or C++ via Rinside or within a package, those functions do not 
listen to suppressMessages, e.g. `Rprintf` keeps printing to the console. Since 
it's common to use wrapper functions in R anyway, they can run 
`are_messages_suppressed` and pass the information on to an explicit `verbose` 
argument of the C / C++ function.
 
- Original Mail -
Von: "Ivan Krylov" 
An: "Nino Hardt" 
CC: r-devel@r-project.org
Gesendet: Sonntag, 19. Februar 2023 12:01:37
Betreff: Re: [Rd] R: determine if `suppressMessages()` has been invoked
On Sun, 19 Feb 2023 15:37:33 +0100 (CET)
Nino Hardt  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
[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel