[Rcpp-devel] NumericVector wrapping an Armadillo vec is returned as a matrix

Romain Francois romain at r-enthusiasts.com
Wed Apr 28 09:20:28 CEST 2010


Le 27/04/10 23:49, Davor Cubranic a écrit :
>
> In my code, I have an Armadillo column vector v (i.e., an 'arma::vec') that I return as a member of a list:
>
>     arma::vec v(...);
>     List result = List::create(_["v"] = v);

 From armadillo's documentation: http://arma.sourceforge.net/docs.html#Col

Col<type>
colvec
vec
Classes for column vectors (matrices with one column).

So arma::vec is a matrix

> In R, result$v becomes a matrix, so I need to call 'as.vector(v)' to turn it into a vector. Even explicitly returning a NumericVector doesn't help, as in:
>
>     arma::vec v(...);
>     List result = List::create(_["v"] = NumericVector(wrap(v)));
>
> Is this by design, a bug, or am I misusing the API?
>
> Davor

NumericVector does not enforce this. You can remove the dim attribute in 
c++ if you like.

NumericVector rvec = wrap(v) ;
rvec.attr( "dim" ) = R_NilValue ;


Or you can use the range wrap, like this:

Rcpp::wrap( vec.memptr() , vec.memptr() + vec.n_elem )

which is what wrap( Col ) does, i.e it eventually calls this :

template <typename T>
	SEXP arma_wrap( const T& object, const ::Rcpp::Dimension& dim){
	    ::Rcpp::RObject x = ::Rcpp::wrap( object.memptr() , object.memptr() 
+ object.n_elem ) ;
	    x.attr( "dim" ) = dim ;
	    return x;
	}


Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/9aKDM9 : embed images in Rd documents
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7



More information about the Rcpp-devel mailing list