gpt4 book ai didi

c++ - R:在 C++ 中抑制来自 Rprintf 的消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:34 24 4
gpt4 key购买 nike

我正在编写一个 R 包,其中包含一些运行冗长计算的 C++ 代码。在 C++ 代码中,我使用 Rprintf() 来输出信息。我尝试使用 suppressMessages() 抑制 R 的输出,但这不起作用,消息仍然出现在 R session 中。

我发现了一些类似的问题,其中人们使用 printf 而不是 Rprintf,但我已经在使用 Rprintf。我还尝试了 R_ShowMessage(),它也会立即显示,并且不会被 suppressMessages() 抑制。

这是一些示例 C++ 代码:

#include <R.h>
extern "C" {
void R_testprint()
{
Rprintf("Try to suppress me!\n");
R_ShowMessage("Try to suppress me, too!");
}
}

以及调用这段代码的函数:

test.print <- function(string) {
res <- .C("R_testprint")
}

现在,以下 R 代码将不会抑制输出:

> suppressMessages( test.print() )
Try to suppress me!
Try to suppress me, too!

我正在使用 R 版本 3.1.0

感谢您的帮助!

最佳答案

使用 capture.output 代替 suppressMessages:

 b <- capture.output( test.print() )

然后将输出存储在字符 vector b 中,而不是打印出来。

关于c++ - R:在 C++ 中抑制来自 Rprintf 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28327013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com