【问题标题】:Message function in R not writing to text file / print message to stdoutR中的消息函数不写入文本文件/将消息打印到标准输出
【发布时间】:2019-10-29 15:35:06
【问题描述】:

我正在使用print(), cat() and message() 函数向 R 控制台写入一些文本。我希望将控制台输出收集到一个文本文件中,所以我将代码夹在一对sink() 函数之间。最终代码如下:

sink("output_filename.txt") 
print("some text with print function")
cat("some text with cat function")
message("some text with message function")
sink()

当我运行此代码时,打印和 cat 按预期工作,而 message's output writes to console 不输出文本文件。为什么会发生这种情况,我该如何解决这个问题?我不喜欢用替代品替换消息功能。

【问题讨论】:

标签: r console sink


【解决方案1】:

就像评论中已经提到的jogomessage() 发送到stdeer,而不是stdout。您可以使用 sink(stdout(), type = "message") 更改此设置

sink("output_filename.txt") 
sink(stdout(), type = "message")
print("some text with print function")
cat("some text with cat function\n")
message("some text with message function")
sink()

来自documentarysink()

正常 R 输出(到连接标准输出)被默认类型转移 =“输出”。只有提示和(大多数)消息继续出现在控制台上。发送到 stderr() 的消息(包括来自 message 的消息, 警告和停止)可以由 sink(type = "message") 转移(参见 下面)。

您也可以在完成后将其改回,因为警告和停止也通过这种方式更改为stdout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2015-08-31
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多