[Rcpp-devel] Idiom for accessing scalars

Romain François romain at r-enthusiasts.com
Wed Jan 4 14:33:47 CET 2012


Le 04/01/12 14:29, Hadley Wickham a écrit :
> Hi all,
>
> I'm still just getting my feet wet in Rcpp, so please excuse the
> naivety of my question, but is this the appropriate idiom for treating
> an input as a C++ scalar?
>
> f<- cxxfunction(signature(x = "integer"), plugin = "Rcpp", '
>    Rcpp::IntegerVector x1(x);
>    int x2 = x1[0];
>
>    return(Rcpp::NumericVector(x2));
> ')
>
> i.e. is there any way I can turn x in x2 more succinctly?  (This
> function is just for illustrative purposes - I'm trying to do
> something at least a little more complicated)
>
> Thanks!
>
> Hadley
Yep,

We have "as" for this, and wrap for the other way around:

f<- cxxfunction(signature(x = "integer"), plugin = "Rcpp", '
   int x2 = as<int>(x) ;

   return wrap(x2) ;
')


Romain
-- 
Romain Francois
Professional R Enthusiast
http://romainfrancois.blog.free.fr



More information about the Rcpp-devel mailing list