问题
Why is this reporting error: no matching function for call to ‘as<Eigen::Matrix<double, 400, 1, 0, 400, 1> >(int)’
and cannot convert ‘3’ (type ‘int’) to type ‘SEXP {aka SEXPREC*}’
? There appears to be a similar problem here.
In a previous question, I asked about registering plugins so that I could use a c++ template library from within R
. Continuing on with that example, I still cannot get this small example to work. The plugin issue has been figured out, but now Rcpp::as
won't allow me to convert numeric vectors and matrices from R
into the appropriate c++ type.
library(Rcpp)
inc <-
'template <size_t dim>
class SillyWrapper
{
public:
Eigen::Matrix<double,dim,1> m_vec;
SillyWrapper(const Eigen::Matrix<int,dim,1>& vec) : m_vec(vec) {};
void printData() { Rcpp::Rcout << m_vec(0); };
};'
src <- '
void foo(){
const int dim = 400;
SillyWrapper<dim> myThing(Rcpp::as<Eigen::Map<Eigen::Matrix<double,dim,1>>>(3));
myThing.printData();
}'
f <- function(x){
plug <- Rcpp.plugin.maker(include.before = "#include <Eigen/Dense>")
settings <- plug()
settings$env$PKG_CPPFLAGS = "-I/usr/include/eigen3"
settings
}
Rcpp::registerPlugin(name = "Eigen3", plugin = f)
fun <- cppFunction(code = src,
plugins = "Eigen3",
includes = inc)
A few other things:
- I am registering my own plugin instead of using RcppEigen for practice (perhaps it will help later when I need to use other header only libraries). I plan on relying on plugins because I have a lot of header files I need to include, and I don't want to
paste
them all together and assign the massive string toinc
above. - I cannot use
Eigen::MatrixXd
I need to size the matrices with the compiler. - @DirkEddelbuettel mentioned something about
configure
logic. I tried searching that, but no luck.
来源:https://stackoverflow.com/questions/57791272/how-do-i-convert-r-data-types-into-an-eigenmatrixdouble-dim-1-in-this-situat