调用 sink() 后没有出现 R 输出 [英] R output not appearing after calling sink()

查看:47
本文介绍了调用 sink() 后没有出现 R 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组复杂的 R 脚本,并决定通过 message() 调用所有与调试相关的消息.我试图找到一种方法来抑制所有消息,并偶然发现了这个 SO post,建议我尝试使用 sink().因此,我将以下代码行插入到我的脚本中,并设置了我的 config$debug_mode <- FALSE:

I have a collection of complicated R scripts, and decided to have all my debug-related messages called via message(). I was trying to find a way to suppress all messages, and stumbled upon this SO post, which recommended I try using sink(). So I inserted the following lines of code into my script, and set my config$debug_mode <- FALSE:

if (!config$debug_mode){
  messages <- file("messages.Rout", open = "wt")
  sink(messages, type = "message")
}

其他 SO 帖子和 R 文档说只需调用 sink()sink(file=NULL) 即可停止之前的转移,但这不适用于我.即使在调用 sink() 之后,我也看不到 message() 调用的 R Studio 控制台输出.此外,sink.number() 返回 0,这似乎表明没有转移.那么,为什么我不再在 R Studio 控制台中看到输出?

The other SO post and R documentation says to simply call sink() or sink(file=NULL) to stop the previous diversion, but this is not working for me. Even after calling sink(), I don't see the R Studio console output from my message() calls. Also, sink.number() returns 0, which seems to suggest that there are no diversions in place. Why, then, am I no longer seeing output in my R Studio console?

推荐答案

当您最初表示只想接收消息时,运行 sink() 并不会关闭该行为.相反,使用 sink(type="message"),它可以满足您的需求.

When you've initially indicated that you want to sink only messages, running sink() does not turn that behavior off. Instead, use sink(type="message"), which does what you're wanting.

> config <- list()
> config$debug_mode <- FALSE
> if (!config$debug_mode){
+   messages <- file("messages.Rout", open = "wt")
+   sink(messages, type = "message")
+ }
> message("trial")
> sink(type="message")
> message("trial")
trial

这很可能是 ?sink 帮助文件的警告"部分中(斜)引用的内容,其中包括此注释:

This is likely what is being (obliquely) referenced in the "Warning" section of the ?sink help file, which includes this note:

除非您了解源代码,否则不要沉没消息流实施它,因此存在陷阱.

Do not sink the messages stream unless you understand the source code implementing it and hence the pitfalls.

这篇关于调用 sink() 后没有出现 R 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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