R函数接收器未将消息或警告重定向到文件

Bsc*_*ein 4 redirect r sink

我想将stderr和stdout消息重定向到输出文件。这是我尝试过的:

sink("outputFile" ,type = c("output", "message"))
print("using print")
cat("using cat\n")
message("using message")
warning("using warning")
Run Code Online (Sandbox Code Playgroud)

运行此代码时,我在R控制台中仍然看到“正在使用消息”和“正在使用警告”,并且没有被重定向。

是否可以将stdout和stderr都重定向到文件?我使用此代码将stderr重定向到stdout,但这并不是我想要的。

sink(stdout(), type = "message") # sink messages to stdout
Run Code Online (Sandbox Code Playgroud)

Lyz*_*deR 6

您需要使用以下步骤分两步进行操作:

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明