[Rcpp-devel] Rcpp::List::create(...) with more than 20 elements ?

Romain Francois romain at r-enthusiasts.com
Wed Apr 17 16:02:18 CEST 2013


Le 17/04/13 15:54, Dominique Belhachemi a écrit :
> Hi everyone,
>
> I am porting some code from the old API to the new API like in the
> following example.
>
> -      SEXP ret = R_NilValue;
> -      RcppResultSet rs;
> -      rs.add("foo1", bar1);
> -      rs.add("foo2", bar2);
> -      ret = rs.getReturnList();
>
> +     SEXP ret = R_NilValue;
> +     ret = Rcpp::List::create(Rcpp::Named("foo1")  = bar1
> +                                       Rcpp::Named("foo2")  = bar2);
>
> This works well if I have less than 20 elements.
>
> But how can I add new elements to the list, including assigning names ?
> Is there an alternative way to create the list without using
> Rcpp::List::create ?
>
>
> Thanks
>
> -Dominique

We don't have List::create above 20 elements. You can have a look at how 
it is done for up to 20 and reproduce it locally.


You can do something like this "manually"

List out(25) ;
CharacterVector names(25) ;
for( int i=0; i<25; i++){
	out[i] = ... ;
	names[i] = ... ;
}
out.attr("names") = names ;


You can use a std::map<string,SomeType> if you deal with homogeneous types:

std::map<string,SomeType> map ;
map["foo"] = bar1 ;
map["foo2"] = bar2 ;
wrap( map ) ;


Or perhaps:

std::map<string,RObject> map ;
map["foo"] = wrap( bar1 ) ;
map["foo2"] = wrap( bar2 ) ;
wrap( map ) ;

Or perhaps even :

std::map<string,SEXP> map ;
map["foo"] = wrap( bar1 ) ;
map["foo2"] = wrap( bar2 ) ;
wrap( map ) ;

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/Zs97qg  : highlight 0.4.1
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list