[Rcpp-devel] Redirecting cout/cerr streams to Rprintf

Romain Francois romain at r-enthusiasts.com
Wed Oct 23 13:51:07 CEST 2013

Le 21/10/2013 12:24, Renaud Gaujoux a écrit :
> Hi,
> this is somehow a general C++ question, but it could be of interest to
> all Rcpp users (I think).
> Still for the purpose of RcppOctave, I would like to redirect cout and
> cerr streams that are used by Octave to Rprintf REprintf respectively.
> Currently I redirect these streams to 2 temporary stringstreams (via the
> std::cout.rdbuf get/set methods ) which buffer the output/errors
> messages and print them at the end of the Octave call. The standard
> streams are restored on exiting the function (in a class destructor).
> However, I would like to do this without buffering, i.e. that messages
> are printed as they come through the stream. This should be possible
> through the definition of a std::stream child class with some method
> overload that output the formatted messages as they are pushed into the
> stream.
> Anybody knows which class and method(s) need to be overloaded, or have
> hints on how to achieve this in an -- aesthetic -- way?
> Thank you.
> Bests,
> Renaud


Rcpp11 does this by default now instead of the behavior that is in Rcpp, 
which depends on the Rcpp::Rcout and Rcpp::cerr. See e.g. 

In Rcpp, you would just have to grab the rdbuf of cout, remember it, set 
it to something different and then later reset it. You can take 
advantage of C++ constructor/destructor anddo something like this:

class Sinker {
     Sinker(): remembered_buffer(std::cout.rdbuf() ){
         std::cout.rdbuf( Rcout.rdbuf() ) ;

         std::cout.rdbuf( remembered_buffer  ) ;

     std::streambuf* remembered_buffer ;
}  ;

Then, whenever you want this redirection, you just have to create a 
Sinker object:

void foo(){
     Sinker bar ;
     // call whatever that uses cout


Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

More information about the Rcpp-devel mailing list