Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic methods with Rcpp-modules

Tags:

module

rcpp

What is the appropriate place to define generic methods on classes exposed using Rcpp modules? More concretely, suppose the following source file is part of an Rcpp package. I'd like to use the + operator with the toy class 'Double':

#include <Rcpp.h>

class Double {
public:
  Double(double d);
  double get() const;
private:
  ...
};

RCPP_MODULE(my_module) {
  using namespace Rcpp;
  class_<Double>("my.double")
    .constructor<double>()
    .property("value", &Double::get);
}

The following R code appears to give the desired result:

.onLoad <- function(libname, pkgname) {
  loadRcppModules()
}

.onAttach <- function(libname, pkgname) {
  setMethod("+", signature(e1=my.double, e2=my.double), function(e1, e2) {
    new(my.double, e1$value + e2$value)
  }, where=.GlobalEnv)
}

I am, however, interested in learning the right way to solve this problem.

like image 553
AlexK Avatar asked Sep 14 '26 01:09

AlexK


1 Answers

I am doing exactly that in the RcppBDT package. I also find it tedious -- for many types, and many ops this gets repetitive quick -- but do not know of a better way.

like image 167
Dirk Eddelbuettel Avatar answered Sep 21 '26 02:09

Dirk Eddelbuettel