[Rcpp-devel] Is there an extractor for dimnames from the Rcpp::<whatever>Matrix class?

Jelmer Ypma jelmerypma at gmail.com
Wed Jun 15 18:53:18 CEST 2011


I'm not sure if this is what you're looking for and I don't know if
this is the official way to extract them, but if you have an object
called obj and know the name of an attribute, you can extract the
attribute using obj.attr( "attribute_name" ). You can get a vector
with all the attributes of an object with the method
obj.attributeNames(). The output of the code below shows that a
NumericMatrix has an attribute "dimnames" (if the rownames,
columnnames or both are defined).

Hope this helps,
Jelmer

library('inline')
library('Rcpp')

src <- '
Rcpp::NumericMatrix mat(a);

std::vector< std::string > attr_names = mat.attributeNames();
Rcpp::List lst( attr_names.size() );
for (int i=0;i<attr_names.size();i++) {
    lst[ i ] = mat.attr( attr_names[ i ] );
}
lst.names() = attr_names;

return lst;
'

fun <- cxxfunction(signature(a="matrix"),
                   src, plugin="Rcpp")

mat_A <- rbind( c(1,2), c(3,2), c(5,4) )
fun( mat_A )
colnames( mat_A ) <- c("c1", "c2")
fun( mat_A )
rownames( mat_A ) <- c("r1", "r2", "r3")
fun( mat_A )
attr( mat_A, "dummy" ) <- list( "a"=1, "b"=2 )    # add dummy attribute name
fun( mat_A )

On Wed, Jun 15, 2011 at 17:45, Douglas Bates <bates at stat.wisc.edu> wrote:
> I was looking for an extractor for the dimnames (i.e.
> R_DimNamesSymbol) from the Rcpp Matrix classes but didn't find one.
> Have I overlooked something?
> _______________________________________________
> 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