R output not appearing after calling sink()

r, rstudio

Solution

When you've initially indicated that you want to sink only messages, running `sink()` does not turn that behavior off. Instead, use `sink(type="message")`, which does what you're wanting.

> config <- list()
> config$debug_mode <- FALSE
> if (!config$debug_mode){
+   messages <- file("messages.Rout", open = "wt")
+   sink(messages, type = "message")
+ }
> message("trial")
> sink(type="message")
> message("trial")
trial

This is likely what is being (obliquely) referenced in the "Warning" section of the `?sink` help file, which includes this note:

Do not sink the messages stream unless you understand the source code implementing it and hence the pitfalls.

Problem

I have a collection of complicated R scripts, and decided to have all my debug-related messages called via `message()`. I was trying to find a way to suppress all messages, and stumbled upon this SO post, which recommended I try using `sink()`. So I inserted the following lines of code into my script, and set my `config$debug_mode <- FALSE`: ``` if (!config$debug_mode){ messages <- file("messages.Rout", open = "wt") sink(messages, type = "message") } ``` The other SO post and R documentation says to simply call `sink()` or `sink(file=NULL)` to stop the previous diversion, but this is not working for me. Even after calling `sink()`, I don't see the R Studio console output from my `message()` calls. Also, `sink.number()` returns `0`, which seems to suggest that there are no diversions in place. Why, then, am I no longer seeing output in my R Studio console?

Original source

Related problems