[Rcpp-devel] Good idioms for creating a named list?

Romain Francois romain.francois at dbmail.com
Mon Mar 15 21:26:32 CET 2010


Le 15/03/10 21:13, Douglas Bates a écrit :
>
> R functions often return a named list, sometimes with an S3 class
> attached.  Over the weekend I exchanged some email with Dirk and
> Romain about good ways to create such a return object.  The way that I
> am currently doing so is through creating an Rcpp::Pairlist of
> Rcpp::Named objects then converting the Pairlist to a List.
>
> Another possibility would be to create a
>
> std::map<std::string, Rcpp::Robject>
>
> add components to the map and wrap the result.  I can check for
> myself, of course, but can someone tell me off the bat what the form
> of the R object resulting from Rcpp::wrap applied to such an object
> would be?

If all works as expected, you should get exactly what you want : a named 
generic vector.

What we need is new constructors in Rcpp::List, but this is not as easy 
as it sounds (Rcpp::List is actually cooked from a template, etc ...). 
Maybe we can do some templated factory functions. Something like:

template <typename T1, typename T2>
Rcpp::make_list( const T1& t1, const T2& t2)

...

with the appropriate handling of the Named class, so that we could do 
things like :


List res = make_list(
	Named( "x" ) = 1,
	Named( "y  ) = "foo"
  ) ;

or (this does not pass the Dirk test though):

List res = make_list(
	_["x"] = 1,
	_["y"] = "foo" ) ;

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5




More information about the Rcpp-devel mailing list