[Rcpp-devel] Named vectors

Andrew Redd amredd at gmail.com
Thu Sep 9 17:43:55 CEST 2010


That is good to know, and if it were up to me I would be using lists
or environments. Unfortunately, I'm working with writing compiled code
for use with the pomp package that has the constraint that model
parameters are only passed on as a named numeric vector.  So a way to
convert the named vector into an equivalent structure would be
helpful.

-Andrew

On Thu, Sep 9, 2010 at 9:30 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> Hi Andrew,
>
> On 9 September 2010 at 08:52, Andrew Redd wrote:
> | What is the appropriate way to use/convert named vectors in C++ with
> | Rcpp.  Basically I have a named vector of parameters that pass into
> | the function.  I cannot be certain of their order, so would like to
> | extract them by name.  For those initialized in C++ a
> | std:map<std::string, double> should work, but how do declare one from
> | a SEXP pointer that is passed in?  For example:
> |
> | draw <- cxxfunction(signature(A="numeric",t="numeric", params="numeric"),body="
> |     SEXP draw( SEXP A, SEXP t, SEXP params){
> |       Rcpp::NumericVector Admissions(A);
> |                       Rcpp::NumericVector T(t);
> |                       std::map<std::string, double> Params(params);
> |                       std::vector<double> draws;
> |                       for(int i=0;i<params[\"n\"];i++){
> |                               draws.pushback(rbinom(Admissions[T[i]+1,2], 1/(1+exp(-params[\"nu\"])))));
> |                       }
> |                       return wrap(draws);
> |     }
> |       ",plugin='Rcpp')
> | This of course does not compile because of std::map<std::string,
> | double> Params(params); is not valid.  But is shows more of less what
> | I'm trying to do.
>
> There is no explicit 'unrolling' of named vectors into maps.  OTOH as<> works
> over atomistic types that can be iterated, and we do talk about the opposite
> way (using Rcpp::wrap) in the intro vignette.  There may be something close
> in the 700+ unit tests.  Maybe we already can -- I just haven't tried.
>
> I tend to pass named lists down from R and pick paraeters one-by-one out of
> the Rcpp::List once in C++.  Here is a working example from something Shane
> and I working on:
>
> R side:
>
>    val <- .Call("SVMregression",
>                 X=x, y=y,
>                 list(C=C,
>                      gamma=gamma,
>                      epsilon=epsilon,
>                      sigma=sigma,
>                      type=type,
>                      kernel=kernel
>                      ),
>                 PACKAGE="rshark")
>
> C++ side:
>
> RcppExport SEXP SVMregression(SEXP Xs, SEXP Ys, SEXP svmParameters) {
>
>    try {
>        Rcpp::NumericMatrix xR = Rcpp::NumericMatrix(Xs);
>        Rcpp::NumericVector yR = Rcpp::NumericVector(Ys);
>        Rcpp::List rparam(svmParameters);
>        double C = Rcpp::as<double>(rparam["C"]);
>        double epsilon = Rcpp::as<double>(rparam["epsilon"]);
>        double gamma = Rcpp::as<double>(rparam["gamma"]);
>        double sigma = Rcpp::as<double>(rparam["sigma"]);
>        string type = Rcpp::as<string>(rparam["type"]);
>        string kernel = Rcpp::as<string>(rparam["kernel"]);
>
> I am happy with this as the lists typically contain different types (double,
> string, bool, ...) anyway.
>
> Does that help?
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>


More information about the Rcpp-devel mailing list