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

不羁岁月 提交于 2020-01-13 11:14:08

问题


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

回答1:


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()



来源:https://stackoverflow.com/questions/48173020/r-function-sink-isnt-redirecting-messages-or-warnings-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!