[Rcpp-devel] Default value for Rcpp List

Dirk Eddelbuettel edd at debian.org
Wed Aug 14 16:25:08 CEST 2013



On 14 August 2013 at 15:57, Matteo Fasiolo wrote:
|  thank you very much for your replies. In the end a named vector is
| enough for my purpose:
| 
| cppFunction(
|   '
|   NumericVector myVett(NumericVector x = NumericVector::create(0) )
|  {
|   if(x.size() == 0) x = NumericVector::create(_["a"] = 1.0, _["b"] = 2.0);
|   return x;
|  }
|  ' 
| )

You don't even need the assignment in the function definition:

R> cppFunction("NumericVector matteo(NumericVector x) { if (x.size()==0) x = NumericVector(4); return(x); }")
R> matteo(vector())
[1] 0 0 0 0
R> matteo(1:3)
[1] 1 2 3
R> 

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list