[Rcpp-devel] What is the "correct" way to print text to R with Rcpp?
Jelmer Ypma
jelmerypma at gmail.com
Tue Oct 18 11:30:02 CEST 2011
Hi all,
another way is to define a new ostream as below. This is modified from
an answer on stackoverflow, where someone ran into a similar problem
with Matlab ( http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
). Using this code you can use something like in the regular way
Rcout << "test" << std::endl;
Hope this helps,
Jelmer
library('inline')
library('Rcpp')
include_code <- '
#include <streambuf>
#include <ostream>
#include <Rcpp.h>
// modified from
// http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
class Rstreambuf : public std::streambuf
{
public:
protected:
virtual std::streamsize xsputn(
const char *s,
std::streamsize n );
virtual int overflow(
int c = EOF );
};
std::streamsize
Rstreambuf::xsputn(
const char *s,
std::streamsize num )
{
Rprintf( "%.*s", num, s );
return num;
}
int
Rstreambuf::overflow(
int c )
{
if (c != EOF) {
Rprintf( "%.1s", &c );
}
return c;
}
class Rostream : public std::ostream
{
protected:
Rstreambuf buf;
public:
Rostream() : std::ostream( &buf ) {}
};
// declare global variable Rcout
Rostream Rcout;
'
example_class <- '
// Example class with two data members
// and overloaded operator<<
class Example
{
friend std::ostream& operator<<(
std::ostream& out,
const Example& ex );
private:
double d_a;
double d_b;
public:
Example(
double _a,
double _b )
:
d_a( _a ),
d_b( _b )
{};
};
std::ostream& operator<<(
std::ostream& out,
const Example& ex )
{
out << "Example( " << ex.d_a << ", " << ex.d_b <<" )";
return out;
}
'
src_code <- '
Rcout << 1 << ", " << 2 << ", " << 3 << std::endl << "(" << 5 << ",
6)" << std::endl;
double a = 1.234;
int b = 3;
std::string s( "test" );
Rcout << a << ", " << b << ", " << s << std::endl;
Rcout << Example( 3.0, 4.0 ) << std::endl;
return Rcpp::wrap<int>( 0 );
'
fun <- cxxfunction( signature(),
body = src_code,
includes = paste( include_code, example_class, sep='\n' ),
plugin="Rcpp",
verbose=TRUE )
fun()
On Tue, Oct 18, 2011 at 06:54, Tim Jurka <timjurka at gmail.com> wrote:
> Hi Slava,
>
> One way to do it is to use Rprintf() the same way you'd use the standard
> printf() command.
> Best,
> Tim
> On Oct 17, 2011, at 10:52 PM, Slava Razbash wrote:
>
> Hello,
>
> What is the "correct" way to print text to R with Rcpp? I find that
> "std::cout<<" will only work if i running R from a console. I am using
> winXP.
>
> Best Regards,
>
> Slava
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
More information about the Rcpp-devel
mailing list