[Rcpp-devel] Problem exposing constructor taking std::vector of user defined class to R using Rcpp-attributes and Rcpp-modules

Dirk Eddelbuettel edd at debian.org
Thu Aug 22 02:00:45 CEST 2013


On 21 August 2013 at 23:21, Luke.Domanski at csiro.au wrote:
| I running into some problems using a combination of Rcpp-attributes and
| Rcpp-modules to expose a C++ class library, and would appreciate your help.

Ahh, that intersection of modules and attributes is still a little rough. 

| I am trying to expose to R the constructor of a C++ class template which
| accepts a std vector of the template parameter T as input, as shown in the
| following toy class A used to replicate the problem:
| 
| template <class T> class A {
|     public:
|         std::vector<T> vec_of_T;
|         A( std::vector<T> in_vec );
| };
| 
| The class and constructor will only be exposed to R as a specific instantiation
| of the class A with template parameter T=class B, i.e. A<B>, where B is
| implemented in both C++ and R, and the C++ implementation of B has a
| constructor B::B(SEXP):
| 
| #include <RcppCommon.h>
| 
| class B {
|     public:
|         int id;
|         B (SEXP b);
|         B (int id=-1);
| };
| 
| /*** R
| # Reference Class of B
| B_R <- setRefClass( "B_R",
|     fields = list(id="numeric")
| )
| */
| 
| 
| template <class T> class A {
|     public:
|         std::vector<T> vec_of_T;
|         A( std::vector<T> in_vec );
| };
| 
|  
| 
| #include <Rcpp.h>
| 
| B::B(SEXP b){
|     Rcpp::Reference in_b(b);
|     id=in_b.field("id");
| }
| 
|  
| B::B(int id){
|     this->id=id;
| }
| 
|  
| 
| The idea is to allow an R vector of Reference Class objects B_R to be passed to
| the C++ class A<B> constructor. To achieve this I have used the following
| Rcpp-modules directly after the preceding code:
| 
|  
| using namespace Rcpp;
| 
| RCPP_MODULE(testing) {
|     class_< A<B> >("A")
|     .constructor< std::vector<B> >()
|     ;
| }
| 
| I then use Rcpp-attributed sourceCpp() to compile the code from the R prompt,
| and generate the R bindings to the C++ class A<B>.
| 
| > sourceCpp(file="test.cpp", verbose=TRUE)
| 
| From my understand of  section 3.2 in Rcpp-introduction.pdf, and what I could
| glean about the handling of RCPP_MODULE declarations within Rcpp-attribute
| (e.g. help(sourceCpp)), the conversion of ?STL vectors of [?] arbitrary types
| that offer a constructor that takes a SEXP? should be supported by Rcpp, and
| Rcpp-attributes should automatically generate wrappers/bindings which utilize
| them.

"Should automatically use available" wrappers/bindings.  

Someone still has to provide the basic as<>() and wrap() code. For common
types we have done.  For custom types you have to do it.
| 
| However, when I run the above sourceCpp command I get the following error which
| is stretching my understanding on both C++ and Rcpp:

[...]  

| Am I missing something in my code?

Could be as simple as your needing to supply as<>() and wrap(). 

I would try something simpler.  Rcpp Modules work well with packages, so
maybe try that first.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list