<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Dear Dirk,<div><br></div><div>Sorry for my poor explanation. I have read `inst/include/Rcpp/iostream/Rstreambuf.h` and think I understand the role of Rcpp::Rcout, but failed to explain my point. By "users [...] can just stick to std::cout", I did not mean allowing users to write to stdout via std::cout. It is about changing the destination of std::cout from stdout to R's i/o. The following code hopefully explains better:</div><div><br></div><div>```</div><div>// [[Rcpp::export]]<br></div><div><div>void example_function() {</div><div>    // Normal state</div><div>    std::cout   << "to stdout; BAD\n";</div><div>    Rcpp::Rcout << "to R's i/o\n";</div><div><br></div><div>    // Ideal state</div><div>    std::streambuf* stdoutbuf = std::cout.rdbuf(Rcpp::Rcout.rdbuf());</div><div>    std::cout   << "to R's i/o via Rcpp::Rcout.buf; GOOD\n"<br></div><div>    Rcpp::Rcout << "to R's i/o\n";</div><div><br></div><div>    // Restore original state</div><div>    std::cout.rdbuf(stdoutbuf);</div><div>    std::cout   << "to stdout; BAD\n";</div><div>    Rcpp::Rcout << "to R's i/o\n";</div><div>}</div></div><div>```</div><div><br></div><div>In other words, we don't have to replace std::cout with Rcpp::Rcout. We only have to change its buffer.</div><div><br></div><div>Thanks,</div><div>Watal</div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, Oct 27, 2018 at 12:09 AM Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi Watal,<br>
<br>
On 26 October 2018 at 23:36, Watal M. Iwasaki wrote:<br>
| Is it possible for Rcpp to do some pre-execution hook before user code? For<br>
| example, if Rcpp system can hijack the std::cout buffer by executing<br>
| `std::cout.rdbuf(Rcpp::Rcout.rdbuf())` automatically, then Rcpp users (and<br>
| external libraries) no longer have to care about Rcout, and can just stick<br>
| to std::cout.<br>
<br>
Sorry, but we need the opposite (which is what Rcpp::Rcout does): we need to<br>
get what _would otherwise go to std::cout_ and feed it to Rcpp::Rcout which<br>
then hands it to R's buffered i/o.  That is the whole underlying reason --<br>
see the as always very detailed "Writing R Extensions" manual for a bit more.<br>
<br>
Automating that (ie replacing std::cout by Rcpp::Rcout) is what is needed.<br>
<br>
Dirk<br>
<br>
<br>
-- <br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Watal M. Iwasaki / 岩嵜 航<br>SOKENDAI, The Graduate University for Advanced Studies,<br>Hayama, Kanagawa 240-0193, Japan<br>+81-46-858-1576<br><a href="https://heavywatal.github.io/" target="_blank">https://heavywatal.github.io/</a></div></div>