R 函数接收器不会将消息或警告重定向到文件 [英] R function Sink isn't redirecting messages or warnings to a file

查看:41
本文介绍了R 函数接收器不会将消息或警告重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

有没有办法将 stdout 和 stderr 都重定向到一个文件?我使用此代码将我的 stderr 重定向到 stdout,但这并不是我正在寻找的.

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

推荐答案

你需要分两步完成,使用如下:

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

这篇关于R 函数接收器不会将消息或警告重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆