beginneR
beginneR

Reputation: 3291

convert Rcpp::NumericVector to Eigen::VectorXd

A similar question has been asked before, here:

Converting between NumericVector/Matrix and VectorXd/MatrixXd in Rcpp(Eigen) to perform Cholesky solve

The thing is, the code I found in fastLm.cpp (at the end) does not work for me.

Rcpp::NumericVector X( (SEXP) R.parseEval("x <- 1:10"));

Eigen::Map<Eigen::VectorXd> XS(Rcpp::as<Eigen::Map<Eigen::VectorXd>>(X)); //!!not working

It gives the following error:

error: no matching constructor for initialization of 'Eigen::Map<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::Stride<0, 0> >'
                    Exporter( SEXP x ) : t(x){}

and (besides others):

note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'SEXP' (aka 'SEXPREC *') to 'const Eigen::Map<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::Stride<0, 0> >'
template<typename PlainObjectType, int MapOptions, typename StrideType> class Map

It might have something to do with the SEXP object?

Upvotes: 0

Views: 1601

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368509

It works for me if I add one more space between the >>:

R> cppFunction("bool conv(NumericVector X) { \
      Eigen::Map<Eigen::VectorXd> \
      XS(Rcpp::as<Eigen::Map<Eigen::VectorXd> >(X)); return true; } ", 
           depends="RcppEigen")
R> conv(1:4)
[1] TRUE
R>

Also, I don't think you can just say

X((SEXP) R.parseEval("x <- 1:10"))

unless you use RInside, which has its own example for Eigen -- and for that just do

cd examples/eigen
make
./rinside_sample0
./rinside_sample1

Both still run as they should in my installation.

Upvotes: 2

Related Questions