Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating objects of R S4 classes in Rcpp?

Tags:

r

rcpp

There is an S4 R class whose object I need to create in Rcpp. Is it possible to do this, and if yes, how?

like image 323
ysb.4 Avatar asked Feb 10 '23 16:02

ysb.4


1 Answers

Sure. Here is something taken from the S4 unit tests in the Rcpp package.

We first create a track class in R. We then create a minimal function creating an S4 object by supplying a string to the constructor and return 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 slot values etc pp from C++.

like image 119
Dirk Eddelbuettel Avatar answered Feb 13 '23 05:02

Dirk Eddelbuettel