R check does not like std: cout (C ++)

I am trying to send a package to CRAN that contains C ++ code (I have no hint about C ++, cpp files were written by someone else).

Check R complains about 'std :: cout (C ++) Compiled code should not cause entry points that can interrupt R and write to stdout / stderr instead of the console, and C RNG

I found the following command in the code:

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, not std :: cout ... but what?

+6
source share
2 answers

Your C ++ project may well use standard input and output.

The problem, as described in the Writing R-Extensions guide, is that you end up mixing two output systems: R and C ++.

So, you are "recommended" to replace all uses, 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 result mixes correctly with R. In one of my (non-Rcpp) packages I had to make many tedious corrections for this ...

Now, as mentioned in the @vasicbre comment and @Dason's answer, if you use Rcpp, you can just do

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

If you are already using Rcpp, this is pretty simple, otherwise you need to decide whether to add Rcpp ...

+7
source

If you want to pass R-buffered output, you will want to use Rcpp :: Rcout instead of std :: cout.

For more information, you can read this article by one of the authors of Rcpp: http://dirk.eddelbuettel.com/blog/2012/02/18/

+4
source

Source: https://habr.com/ru/post/981161/


All Articles