0

We have have written an C++ class in a R-package which we exported via RCPP_MODULE. This works beatifuly and we can use the classes functions within R. Now we would like to write in another package a function in C++ which takes an instance of the previously written class as an argument. We would like to export this function to R. The workflow in R which we intend is the following:

objA <- PackageA::ClassA$new()
result <- PackageB::doSomething(objA)

The implementation in package B is the following:

// [[Rcpp::depends(PackageA)]]
#include "ClassA.h"
RCPP_EXPOSED_CLASS(ClassA);

// [[Rcpp::export]]
void doSomething(ClassA* a){
  Rcpp::Rcout<<a->hello()<<std::endl;
}

But the code does not compile:

error: cannot convert ‘SEXP {aka SEXPREC*}’ to ‘ClassA*’ in initialization
          Exporter( SEXP x ) : t(x){}

The solutions shown here (using Rcpp::XPtr) https://stackoverflow.com/a/55225006/12417596 are working but do not fit our needs. Therefore, we were wondering whether there is a way to teach Rcpp how to convert objects of our class A.

  • You created some C++ level code in package A that your package B depends upon. It needs to know about classA in order to convert from/to it (which is part of what Rcpp does for you). For that you need to tell package B about package A's headers -- how else would it know about `RCPP_EXPOSED_CLASS(ClassA)` ?. There is an export mechanism, and we explained in a few places. It may work with Rcpp Modules; I am not sure if anybody has tried. – Dirk Eddelbuettel Feb 19 '20 at 13:18
  • We forgot to mention that we have a header only implementation of Class A and we already use ```// [[Rcpp::depends(PackageA)]]``` to include these headers in package B. It works fine for other C++ functions of package B that we do not want to export. – Markus Zinser Feb 19 '20 at 13:36
  • 1
    Sure. So package B gets to compile, but still nobody told Rcpp how to convert to classA, just as the error says. Modules did that locally package A; nobody told package B. I don't think we have automation for that. Maybe come to rcpp-devel with more details. – Dirk Eddelbuettel Feb 19 '20 at 14:01

0 Answers0