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?<br><br>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).<br>
<br>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).<br>
<br>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.)<br>
<br>Best, Jo<br><br><br><br><div class="gmail_quote">2010/10/28 Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org">edd@debian.org</a>></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
First, I (re-)define your helper function, making the argument const:<br>
<br>
> ## define our helper function, this will be passed as an include segment to cxxfunction()<br>
> inc <- 'void other(const double any) {<br>
+ std::cout << "YAY it works, value is " << any << std::endl;<br>
+ }<br>
+ '<br>
><br>
And use it:<br>
> num <- 42.1337<br>
> fun(num)<br>
YAY it works, value is 42.1337<br>
YAY it works, value is 42.1337<br>
[1] 0<br>
> fun(3.1415)<br>
YAY it works, value is 3.1415<br>
YAY it works, value is 3.1415<br>
[1] 0<br>
><br></blockquote></div><br>