简体   繁体   English

R:C ++中来自Rprintf的suppressMessages

[英]R: suppressMessages from Rprintf in C++

I am writing an R-package with some C++ code running lengthy calculations. 我正在编写一个R-package,其中包含一些运行冗长计算的C ++代码。 Inside the C++ code, I am using Rprintf() to output information. 在C ++代码中,我使用Rprintf()来输出信息。 I tried suppressing the output from R with suppressMessages(), but this doesn't work, the messages still appear inside the R session. 我尝试使用suppressMessages()来抑制R的输出,但是这不起作用,消息仍然出现在R会话中。

I found some similar questions, where people were using printf instead of Rprintf, but I am already using Rprintf. 我发现了一些类似的问题,人们使用的是printf而不是Rprintf,但我已经在使用Rprintf了。 I also tried R_ShowMessage(), which is also immediately displayed and not suppressed by suppressMessages(). 我也尝试过R_ShowMessage(),它也会立即显示而不会被suppressMessages()抑制。

Here is some example C++ code: 这是一些示例C ++代码:

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

And the function that calls this code: 以及调用此代码的函数:

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

Now, the following R code will not suppress the output: 现在,以下R代码不会抑制输出:

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

I am using R version 3.1.0 我使用的是R版本3.1.0

I appreciate any help! 我感谢任何帮助!

Use capture.output in place of suppressMessages : 使用capture.output代替suppressMessages

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

Then the output is stored in character vector b instead of being printed. 然后输出存储在字符向量b而不是打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM