Reputation: 1042
I'm writing some text to R console using print(), cat() and message()
functions. I wish to collect console outputs into a text file, so I sandwiched the code between a pair of sink()
functions. Final code looks like:
sink("output_filename.txt")
print("some text with print function")
cat("some text with cat function")
message("some text with message function")
sink()
When I run this code, print and cat work as expected while message's output writes to console
and not output text file. Why does this happen and how do I work around this? I prefer not to replace message function with alternatives.
Upvotes: 2
Views: 2026
Reputation: 2956
like jogo already mentioned in the commentary, message()
sends to stdeer, instead of stdout
. You can change this by using sink(stdout(), type = "message")
sink("output_filename.txt")
sink(stdout(), type = "message")
print("some text with print function")
cat("some text with cat function\n")
message("some text with message function")
sink()
from documentary of sink()
:
Normal R output (to connection stdout) is diverted by the default type = "output". Only prompts and (most) messages continue to appear on the console. Messages sent to stderr() (including those from message, warning and stop) can be diverted by sink(type = "message") (see below).
also you might change it back after you are done, since warnings and stops are also changed to stdout
this way
Upvotes: 2