Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R check doesn't like std:cout (C++)

Tags:

c++

r

I'm trying to submit a package to CRAN which contains C++ code (I have no clue about C++, the cpp files were written by somebody else).

The R check complains about ‘std::cout’ (C++) Compiled code should not call entry points which might terminate R nor write to stdout/stderr instead of to the console, nor the C RNG

I found in the code the following command:

 integrate_const(stepper_type( default_error_checker< double >( abs_error , rel_error ) ),
                mDifEqn,
                x,
                0.0,
                (precipitationLength * timeStep), 
                timeStep,
                streaming_observer(std::cout) ); 

I guess R (CRAN) expects something else rather than std::cout... but what?

like image 460
Claudia Avatar asked Jan 17 '15 21:01

Claudia


1 Answers

Your C++ project may well be using standard input and output.

The issue, as discussed in the Writing R Extensions manual, is that you then end up mixing two output systems: R's, and the C++ one.

So you are "encouraged" to replace all uses of, say,

 std::cout << "The value of foo is " << foo << std::endl;

with something like

 Rprintf("The value of foo is %f\n", foo);

so that your output gets blended properly with R's. In one of my (non-Rcpp) packages I had to do a lot of tedious patching for that...

Now, as mentioned in a comment by @vasicbre and an answer by @Dason, if you use Rcpp you can simply do

 Rcpp::Rcout << "The value of foo is " << foo << std::endl;

If you already use Rcpp this is pretty easy, otherwise you need to decide if that makes it worth adding Rcpp...

edit: fixed typo in Rcpp::Rcout.

like image 130
Dirk Eddelbuettel Avatar answered Oct 24 '22 04:10

Dirk Eddelbuettel