Creating objects of R S4 classes in Rcpp?

There is an S4 R class whose object I need to create in Rcpp. Can this be done, and if so, how?

+4
source share
1 answer

Of course. Here's what comes from the S4 unit tests in the Rcpp package.

First, create a class trackin R. Then we create a minimal function that creates the object S4, providing a string to the constructor and returning it:

R> setClass("track", representation(x="numeric", y="numeric"))
R> cppFunction('SEXP trythis(std::string txt) { S4 foo(txt); return foo; }')
R> trythis("track")
An object of class "track"
Slot "x":
numeric(0)

Slot "y":
numeric(0)

R> 

You can set the values ​​of the slots, etc. pp from C ++.

+7
source

All Articles