简体   繁体   English

R检查不喜欢std:cout(C ++)

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

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). 我正在尝试向包含C ++代码的CRAN提交一个程序包(我对C ++不了解,cpp文件是由其他人编写的)。

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 R检查抱怨'std :: cout'(C ++)编译后的代码不应调用可能终止R的入口点,也不应写入stdout / stderr而不是写入控制台或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? 我想R(CRAN)期望的不是std :: cout ...,而是什么?

Your C++ project may well be using standard input and output. 您的C ++项目很可能正在使用标准输入和输出。

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. 正如编写R扩展手册中所讨论的那样,问题是,您最终将两种输出系统混在一起:R和C ++。

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. 这样您的输出就会与R正确混合。 In one of my (non-Rcpp) packages I had to do a lot of tedious patching for that... 在我的一个(非Rcpp)软件包中,我不得不为此做很多乏味的修补工作……

Now, as mentioned in a comment by @vasicbre and an answer by @Dason, if you use Rcpp you can simply do 现在,正如@vasicbre的评论和@Dason的答复中所提到的,如果您使用Rcpp,则只需

 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... 如果您已经使用Rcpp,这非常简单,否则您需要确定是否值得添加Rcpp ...

edit : fixed typo in Rcpp::Rcout . 编辑 :修复了Rcpp::Rcout错字。

If you want to stream to R's buffered output you'll want to use Rcpp::Rcout instead of std::cout. 如果要流式传输到R的缓冲输出,则需要使用Rcpp :: Rcout而不是std :: cout。

For more details you can read this article by one of Rcpp's authors: http://dirk.eddelbuettel.com/blog/2012/02/18/ 有关更多详细信息,您可以阅读Rcpp的一位作者的文章: http ://dirk.eddelbuettel.com/blog/2012/02/18/

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

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