[Rcpp-devel] Accessing an attribute object in C/C++

Silkworth,David J. SILKWODJ at airproducts.com
Thu Jul 12 05:41:39 CEST 2012


I have built the following example in order to demonstrate my concern.  In reality I am obtaining an object such as the example mymat with an attached vector as an attribute from Douglas Bates' 1998 C code for spline_basis in the splines package.  It seems that back in the days of stone knives and clubs (before Rcpp) this was indeed a unique way of passing a list of objects back to R.

Some may recall that I have ripped splines.c successfully into a new package project, keeping the original code intact, complete with its identity.

My desire is to build calling code without any need to go back to the R interpreter , hoping for optimal performance.  As can be seen, by grabbing the attr() function using what I call the "environment facility" of Rcpp to extract the attached vector violates my best intentions.

I am hoping for a pure C/C++ way of accessing the vector that returns as an attribute.

One possibility might be a C function that takes the SEXP object with the attribute, and returns just the attrubute as a separate SEXP object.  Alas, this is too far over my head at the moment.

src<- '
Rcpp::NumericMatrix m_rcpp(arg1);
arma::mat m_arma   = Rcpp::as<arma::mat>(arg1); 
Environment base("package:base");
Function getAttrib = base["attr"];
Rcpp::NumericVector v_rcpp(4);
v_rcpp = getAttrib(m_rcpp,"attachment");
arma::vec v_arma   = Rcpp::as<arma::vec>(v_rcpp); 

 return(Rcpp::List::create(Rcpp::Named("origMatrix")=wrap(m_arma),
               Rcpp::Named("origVector")=wrap(v_arma)));
 '

attr_sep<-cxxfunction(signature(arg1="numeric"),
    body = src, plugin = "RcppArmadillo")


## R test code:
mymat<-matrix(1:12,3,4)
myvec<-rep(5,4)
attr(mymat,"attachment")<-myvec

return_list<-attr_sep(mymat)





More information about the Rcpp-devel mailing list