Assigning numeric vector in RcppEigen

272 Views Asked by At

I have a cpp file which contains the code as below:

#include <RcppEigen.h>
//[[Rcpp::depends(RcppEigen)]]

using namespace Eigen;


//[[Rcpp::export]]
Eigen::VectorXd memory(const double u, const double v, const double w){
  Eigen::VectorXd A(3);
   A << u,v,w;
return(A); 
}

But it shows me this error in the command (Please see the attached screenshot). Error in code

The whole R session ABORTS because of this. Why this is happening? Can anyone help? How can I write this numeric vector properly?

1

There are 1 best solutions below

4
On

Rcpp and RcppEigen are glue packages and may have a side effect on existing components. So the << idiom used to assign a matrix may be in the Eigen docs but may get shadowed by the Rcpp input/output ops.

In another note, your title is a little off. This does not 'write', you were trying to assign. Which ... you can do more easily from R directly as in this example:

#include <RcppEigen.h>
//[[Rcpp::depends(RcppEigen)]]

// [[Rcpp::export]]
void showVec(Eigen::VectorXd A) {
    Rcpp::Rcout << A << std::endl;
}

/*** R
v <- c(1.1, 2.2, 3.3)
showVec(v)
*/

which we can call from R and have the bottom portioned executed if we just call Rcpp::sourceCpp() on it:

> Rcpp::sourceCpp("~/git/stackoverflow/68453452/answer.cpp")    

# lots of compiler noise omitted here


> v <- c(1.1, 2.2, 3.3)

> showVec(v)
1.1
2.2
3.3
>