Message-ID: <19631.319.768874.645661@max.nulle.part>
Date: 2010-10-08T11:32:15Z
From: Dirk Eddelbuettel
Subject: [Rcpp-devel] pass an R list as an argument and accessing elements of a list
In-Reply-To: <AANLkTi=WAHETKks=Bo7DJ8FO2_dDinqXEqZuQZc-RWQ2@mail.gmail.com>
Hi Vinh,
On 7 October 2010 at 21:41, Vinh Nguyen wrote:
| I'll pass the R list as an SEXP object and then use Rcpp to access that list:
| extern "C" SEXP MyCppFunc(..., SEXP _list){
| ...
| Rcpp::List list(_list) ;
| ...
| }
|
| Is the above the proper way to pass a list the Rcpp way? ?I've
Yes it is. There are working examples in other packages where these
Rcpp::List are used to pass a (large) number of variables.
| used Rcpp::List::create but haven't done passing a list as an
| argument.
|
| To access the elements of the list, can I access them as list["x"],
| list["y"], and list["z"], just like the Rcpp::NumericVector case?
Absolutely.
| Don't think I saw accessing lists in the vignettes.
|
| When I pass the Rcpp::List into the optim function, is this the
| correct way to pass it:
|
| void optim(..., Rcpp::List list){
| ...
| // do stuff with list, such as list["x"].
| ...
| }
You may want to make that a reference:
void optim(..., Rcpp::List & list){
or a const reference:
void optim(..., const Rcpp::List & list){
for greater efficiency. [ The const case every now and then reveals a
accessor functions, let us know when you get errors. ]
Cheers, Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com