Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R function Sink isn't redirecting messages or warnings to a file

Tags:

redirect

r

sink

I am looking to redirect stderr and stdout messages to an output file. Here's what I tried:

sink("outputFile" ,type = c("output", "message"))
print("using print")
cat("using cat\n")
message("using message")
warning("using warning")

When I run this code, I still see "using message" and "using warning" in my R console, and it's not being redirected.

Is there a way to redirect both stdout and stderr to a file? I used this code to redirect my stderr to stdout, but that's not exactly what I'm looking for.

sink(stdout(), type = "message") # sink messages to stdout
like image 477
Bschrein Avatar asked Jan 09 '18 16:01

Bschrein


1 Answers

You need to do it in two steps, using something like this:

zz <- file("test.txt", open = "wt")
sink(zz ,type = "output")
sink(zz, type = "message")
print("using print")
cat("using cat\n")
message("using message")
warning("using warning")
#and to close connections
sink()
sink()

enter image description here

like image 164
LyzandeR Avatar answered Oct 20 '22 10:10

LyzandeR