[Rcpp-devel] Named vectors

Andrew Redd amredd at gmail.com
Thu Sep 9 16:52:37 CEST 2010

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);
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.

Andrew Redd

More information about the Rcpp-devel mailing list