Skip to content
Prev 2285 / 10988 Next

[Rcpp-devel] Rcpp equivalent of sample()?

On 14 May 2011 at 17:13, Chris DuBois wrote:
| Thanks Dirk. ?Quick newbie followup question: Can we put functions within the
| inline C++ code? ?The following doesn't compile, for example:
| src <- '
| int addition (int a, int b)
| {
| ??int r;
| ??r=a+b;
| ??return (r);
| }
| NumericVector xx(x);
| return(xx);
| '
| testfun = cxxfunction(signature(x="numeric"),body=src,plugin="Rcpp")

Use the 'verbose=TRUE' to cxxfunction() and it will become clear
why -- a function is written around the 'body=...' argument,
hence no chance that that part may contain another C++ function.

The remedy is simple: use the "include=..." argument as in 

   inc <- '
   int addition (int a, int b) {
     int r = a + b;
     return (r);
   }
   '
   
   src = '
   NumericVector xx(x);
   return(xx);
   '

   testfun = cxxfunction(signature(x="numeric"), body=src, include=inc, plugin="Rcpp") 


That trick is used a couple of time in the documentation and examples.


| On a related note, would you mind showing a quick working example using
| std::accumulate? ?The one below from the quick reference doesn't compile for
| me.


Try Google for something like that, eg a query that is restricted to my site as in

    site:dirk.eddelbuettel.com  "std::accumulate"

show five immediate hits.  You can similarly constrict the search
to the rcpp-devel list.

Can you send a small example for inline that shows how/where it quickref
example fails for you?

Dirk