Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R output not appearing after calling sink()

Tags:

r

rstudio

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?

like image 993
Yu Chen Avatar asked Oct 27 '25 09:10

Yu Chen


1 Answers

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.

like image 175
Josh O'Brien Avatar answered Oct 29 '25 00:10

Josh O'Brien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!