nevrome
nevrome

Reputation: 1550

Expose user defined list class from C++ to R via Rcpp modules

I have written a class class_A in C++ and use the Rcpp modules framework to expose it to R. Works like a charm.

#include "Rcpp.h"
using namespace Rcpp;

class class_A {
  public:
    class_A(double num){this->num = num;};
    double get_num(){return this->num;};
  private:
    double num;
};

RCPP_MODULE(class_A_module) {
  class_<class_A>("class_A")
  .constructor<double>()
  .property("num", &class_A::get_num)
  ;
}

RCPP_EXPOSED_CLASS(class_A);

Now I would like to have a class class_B that can store multiple instances of class_A (and some other attributes). My idea was to use a std::vector for this purpose:

class class_B {
  public:
    class_B(std::vector<class_A> num_list){this->num_list = num_list;};
    std::vector<class_A> get_num_list(){return this->num_list;};
  private:
    std::vector<class_A> num_list;
};

RCPP_MODULE(class_B_module) {
  class_<class_B>("class_B")
  .constructor<std::vector<class_A>>()
  .property("num_list", &class_B::get_num_list)
  ;
}

RCPP_EXPOSED_CLASS(class_B);

That doesn't compile:

no matching function for call to 'export_range__dispatch(SEXPREC*&,
__gnu_cxx::__normal_iterator<class_A*, std::vector<class_A>
>&, Rcpp::traits::r_type_traits<class_A>::r_category)'

This is rather not surprising: How should Rcpp expose a std::vector to R?

From this highly relevant rcpp-devel Mailing list thread I understood that it's nevertheless somehow possible to create this simple storage class class_B. I guess I have to write my own wrap and as implementation.

1. Is this correct so far?

2. How do I exactly do this in the case of my simple code example? The examples I found all are far more complicated than I expect the necessary minimum implementation to be.


Edit: I modified my example code and tried to pinpoint the problem.

Header and implementation are now separated:

ex.h

class class_A {
  public:
    class_A(double num);
    double get_num();
  private:
    double num;
};

class class_B {
  public:
    class_B(std::vector<class_A> num_list);
    std::vector<class_A> get_num_list();
  private:
    std::vector<class_A> num_list;
};

ex.cpp

#include "Rcpp.h"
#include "ex.h"

using namespace Rcpp;


class_A::class_A(double num){this->num = num;}
double class_A::get_num(){return this->num;}

RCPP_MODULE(class_A_module) {
  class_<class_A>("class_A")
  .constructor<double>()
  .property("num", &class_A::get_num)
  ;
}

RCPP_EXPOSED_CLASS(class_A);


class_B::class_B(std::vector<class_A> num_list){this->num_list = num_list;}
std::vector<class_A> class_B::get_num_list(){return this->num_list;}

RCPP_MODULE(class_B_module) {
  class_<class_B>("class_B")
  .constructor<std::vector<class_A>>()
  .property("num_list", &class_B::get_num_list)
  ;
}

RCPP_EXPOSED_CLASS(class_B);

Here's my attempt to implement 'wrap' and 'as'.

fix.cpp

#include <RcppCommon.h>
#include "ex.h"

// non-intrusive extension via template specialisation
namespace Rcpp {

  template <> class_A as(SEXP aa);
  template <> SEXP wrap(const class_A &a);

  template <> class_B as(SEXP bb);
  template <> SEXP wrap(const class_B &b);

}

#include <Rcpp.h>

// define template specialisations for as and wrap
namespace Rcpp {
  template <> class_A as(SEXP aasexp) {
    double aa = as<double>(aasexp);
    return class_A(aa);
  }

  template <> SEXP wrap(const class_A &a) {
    return Rcpp::wrap(a);
  }

  template <> class_B as(SEXP bbsexp) {
    // ?
  }

  template <> SEXP wrap(const class_B &b) {
    // ?
  }
}

I guess the interesting parts are the ones I can't get my head around?

Upvotes: 0

Views: 376

Answers (1)

Romain Francois
Romain Francois

Reputation: 17642

One way would be to have a constructor taking an Rcpp::List, or a factory if you don't want to mess with the semantics of class_B:

class_B* class_B_ctor( List data){
  std::vector<class_A> v ;
  for( int i=0; i<data.size(); i++){
    v.push_back( as<class_A>(data[i]) );
  }
  return new class_B(v) ;
}

That you can use with the .factory method in your module:

RCPP_MODULE(class_A_module) {
  class_<class_A>("class_A")
    .constructor<double>()
    .property("num", &class_A::get_num)
  ;

  class_<class_B>("class_B")
    .factory< List >( &class_B_ctor )
    .property("num_list", &class_B::get_num_list)
  ;

}

so that:

> a1 <- new( class_A )
> a2 <- new( class_B )
> a1
C++ object <0x103008b78> of class 'class_A' <0x10e4bcd20>
> a2
C++ object <0x103008b78> of class 'class_B' <0x10e4bce20>
> new( class_B, list(a1,a2) )
C++ object <0x10e4de420> of class 'class_B' <0x10e4bce20>

Upvotes: 2

Related Questions