mardi 3 septembre 2019

correctly registering a plugin to use Eigen via Rcpp

I'm trying to be able to use a c++ class template in R. This was my first try at a small reproducible example.

library(inline)
library(Rcpp)
inc <- 
"#include <Eigen/Dense>

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);
};"
src <- 
'SillyWrapper mything(Rcpp::as<Eigen::Map<Eigen::Matrix<double,dim,1>>>(x));'

library(inline)
fun <- cxxfunction(signature(x="numeric"),
                   body=src, 
                   includes=inc, 
                   plugin="Rcpp")
fun(rnorm(3))

How to I get access to the Eigen headers, though?

On my machine they are at /usr/include/eigen3/. I think I need to "register a plugin." I'm not sure where this file path goes in. I've tried a few of the named arguments, but no luck. Here's one example of something that I've tried that doesn't work:

library(inline)
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);
};'
src <- 
    'SillyWrapper mything(Rcpp::as<Eigen::Map<Eigen::Matrix<double,dim,1>>>(x));'
plug <- Rcpp.plugin.maker(include.before = "#include <Eigen/Dense>", 
                          LinkingTo = "-I/usr/include/eigen3/") # correct arg name?
inline::registerPlugin("eigenDemo", plug)
fun <- cxxfunction(signature(x="numeric"),
                   body=src, 
                   includes=inc, 
                   plugin="eigenDemo")

I know there is an RcppEigen library that already exists, that would help with this example. If it's possible, I would like to see an answer that doesn't make use of this, though, because the information will be more applicable to other situations where this isn't available. Also, where do you put the c++11 flags? Apparently you can only use cxxfunction with one plugin at a time.

Aucun commentaire:

Enregistrer un commentaire