[Rcpp-devel] Accessing T of RcppVector<T>

Johannes Egner johannes.egner at gmail.com
Sat Oct 30 13:45:38 CEST 2010

Dirk's code relates to a question I meant to ask the experts for a while:
what's a safe and generic way to print C/C++ objects in an R session?

The background: forwarding the cout-stream in Dirk's example to R as below
doesn't work in the R-GUI on Windows (or in an IDE such as Eclipse); but it
does work if one runs an R session from the command line (that is, within
the console -- no surprise that the stream is correctly forwarded).

Additionally, there's a (confusing) variety of other print-functions that
successfully bridge the gap between C functions and the R-GUI, mostly made
available through prtutil.h. For instance, Rf_PrintValue takes an SEXP and
prints it to R using the "right format" when used in any C/C++-function,
whereas functions such as Rf_printIntegerVector  take C arguments (and
somehow smuggle them into the R session). And then there's the workhorse
Rprintf (which is possibly called by Rf_printIntegerVector).

Apologies for being so vague -- I would say I am eventually looking for a
way to make the below example run in the R-GUI, both to avoid using the
abovementioned functions, and to use existing C++-code that contains
summary/debugging overloads of cout. (This may belong to Rd and not to
Rcpp-devel, but I'm not sure.)

Best, Jo

2010/10/28 Dirk Eddelbuettel <edd at debian.org>

> First, I (re-)define your helper function, making the argument const:
> > ## define our helper function, this will be passed as an include segment
> to cxxfunction()
> > inc <- 'void other(const double any) {
> +            std::cout << "YAY it works, value is " << any << std::endl;
> +         }
> +        '
> >
> And use it:
> > num <- 42.1337
> > fun(num)
> YAY it works, value is 42.1337
> YAY it works, value is 42.1337
> [1] 0
> > fun(3.1415)
> YAY it works, value is 3.1415
> YAY it works, value is 3.1415
> [1] 0
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20101030/c26ab56d/attachment.htm>

More information about the Rcpp-devel mailing list