[Rcpp-devel] Rcpp-devel Digest, Vol 12, Issue 8

Douglas Bates bates at stat.wisc.edu
Fri Oct 8 14:43:35 CEST 2010

Not sure that I saw the original posting from Vinh but it may be
helpful to know that there is an example that uses Rcpp classes to do
much of packaging of the function, arguments, initial values, etc. in
the minqa package

On Fri, Oct 8, 2010 at 5:28 AM, Christian Gunning <xian at unm.edu> wrote:
>> Message: 2
>> Date: Thu, 7 Oct 2010 21:41:06 -0700
>> From: Vinh Nguyen <vinhdizzo at gmail.com>
>> Subject: [Rcpp-devel] pass an R list as an argument and accessing
>>        elements        of a list
>> Dear Rcpp List,
>> My goal is to write a generic C++ optimizing function (call it "optim"
>> in this email) based on the Newton-Raphson algorithm. ?I will be
>> passing into the optimizing function 4 arguments:
>> 1. ?(pointer to) a function, "f1", to be optimized,
>> 2. ?an initial value/vector,
>> 3. ?the remaining inputs (other data, say x, y, and z) that argument 1
>> needs in order to compute its value, derivative, and 2nd derivative,
>> for the optimization, and
>> 4. ?the final object to store the value, derivative, and 2nd derivative outputs.
> It might be easier for optim to return an SEXP, i.e. a wrapped Rcpp
> object, rather than a pointer/reference to place the object in.  The
> resulting SEXP can be returned directly to R (which is what you want,
> right?).  Packing a Rcpp::List with value, deriv, etc. inside optim
> might make sense here.
>> As argument 1 will be different from case to case, so too will
>> argument 3. ?In other words, the arguments to the function f1 will be
>> varying in terms of number and type (could be 2, 3, 4, ... number of
>> objects). ?To be as flexible as possible, I'm thinking that argument 3
>> should be an R list. ?Questions follow:
>> 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
>> used Rcpp::List::create but haven't done passing a list as an
>> argument.
> Yes, I've used this successfully.  I have run into trouble extracting
> elements of length>1 from lists with variable-length elements, e.g.
> _mylist = list(a=1, b=1:3), mylist["a"] works but mylist["b"] is
> squirrely.
>> 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?
>> 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"].
>> ...
>> }
> Yes.
> hth,
> Christian
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

More information about the Rcpp-devel mailing list