Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: suppressMessages from Rprintf in C++

I am writing an R-package with some C++ code running lengthy calculations. Inside the C++ code, I am using Rprintf() to output information. I tried suppressing the output from R with suppressMessages(), but this doesn't work, the messages still appear inside the R session.

I found some similar questions, where people were using printf instead of Rprintf, but I am already using Rprintf. I also tried R_ShowMessage(), which is also immediately displayed and not suppressed by suppressMessages().

Here is some example C++ code:

#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:

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

I am using R version 3.1.0

I appreciate any help!

like image 238
chakalakka Avatar asked Feb 04 '15 16:02

chakalakka


1 Answers

Use capture.output in place of suppressMessages:

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

Then the output is stored in character vector b instead of being printed.

like image 55
Adrian Baddeley Avatar answered Nov 01 '22 00:11

Adrian Baddeley