[Rcpp-devel] The proper way of wrapping an R object

Alessandro Mammana mammana at molgen.mpg.de
Tue Feb 10 13:09:10 CET 2015


Dear all,
the following is not about "how to make it work", because it already
works, it is only about "what's the best, cleanest way of doing it".

I have an s4 object with three slots, and many Rcpp functions dealing with it.
My goal is to have a C struct (or class) equivalent to the s4 object,
i.e. where each field is an s4 slot.

Right now I defined the struct in this way:

struct CountSignals {
    Rcpp::IntegerVector counts;
    Rcpp::IntegerVector breaks;
    bool ss;

    CountSignals(Rcpp::RObject csig) {
        if (not csig.inherits("CountSignals")) Rcpp::stop("expecting a
CountSignals object");
        counts = Rcpp::as<Rcpp::IntegerVector>(csig.slot("counts"));
        breaks = Rcpp::as<Rcpp::IntegerVector>(csig.slot("breaks"));
        ss = Rcpp::as<bool>(csig.slot("ss"));
    }
};


And then each method starts like this, for example:

// [[Rcpp::export]]
Rcpp::List asList(Rcpp::RObject csig){
    CountSignals x(csig);
....

As said before, this is working fine, but I am skeptical about the
following things:
1. in the assignment operators in the CountSignals constructor, am I
copying the whole vector or am I copying a pointer to it? I need to
make sure that nothing is copied (as these vectors can be very long).
2. Even cleaner would be to start a method in this way:

// [[Rcpp::export]]
Rcpp::List asList(CountSignals x){
....

do you know if/how I can achieve that? (All of this code is inside an
R package).

Thanks a lot!

-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany


More information about the Rcpp-devel mailing list