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.