[Rcpp-devel] A beginners guide to Rcpp
Dirk Eddelbuettel
edd at debian.org
Fri Nov 30 14:15:47 CET 2012
On 30 November 2012 at 07:57, Gabor Grothendieck wrote:
| to wrestle with SEXPs. If the alternative is to work with SEXPs
| directly then the alternative certainly is painful but if the
| alternative is to use .C (and that may not be a feasible alternative
| in some or many cases due to its restricted applicability) its not
| painful at all.
That is true.
If one wanted to, say, just sum up a single vector of doubles a simple .C may
be easier to use that wrangling with SEXP via .Call. But consider this:
a) Simon's point I just quoted about this being dangerous as you are bound
to run over the limits (frequent source of error on messages on list)
b) the fact that thanks to JJ's work it is now even easier to just pass a
vector down an get a result back: we just write
double mysum(NumericVector x) {
return std::accumulate(x.begin(), x.end(), 0.0);
}
without pointers, memory, indices, ... trapping us. And creating that
function is now a single call:
R> cppFunction("double mysum(NumericVector x) { return std::accumulate(x.begin(), x.end(), 0.0); }")
R> mysum(1:100)
[1] 5050
R>
And again, this uses SEXP as only SEXP gives us the handles Rcpp exploits to
allow for these nice interface.
Simple things, easily done, with nicer interfaces.
Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list