[Rcpp-devel] Pass an Rcpp module object to a method belonging to another module from R?

Dirk Eddelbuettel edd at debian.org
Wed Sep 10 03:16:39 CEST 2014


On 9 September 2014 at 19:56, Grant Brown wrote:
| Hi folks, 
| 
| I'm building several Rcpp modules which specify parts of a complicated
| statistical model, and want to pass them to a main module for use. Is there an
| easy way to do this? 
| 
| Below is a simplified example, with question marks where I'm not sure what to
| do. I've tried accepting a pointer to A, playing with using XPtrs, and a bunch
| of things which made far less sense, but so far no luck. 
| 
| Any suggestions or links to sections of the docs I should revisit would be
| appreciated. 

That's neat and somewhat ambitious. Not sure it can be done; Romain may have
pointers for you.

I prefer to keep things simple; at least initially.  I sometimes create a
larger class (possibly as a singleton) and then have accessors from R which
initialize, supply parameters, alter parameters, request computation, ... You
can then play with factories, and components, and inheritance, etc pp.

By keeping the interface simple I can set it up in a way which lets me test
the functionality also in a simple main away from R.  But that may just be me.

Dirk

 
| Thanks,
| -Grant
| 
| 
| ## file: A.cpp
| 
| class A
| {
| public:
| A();
| ~A();  
| };
| 
| class B
| {
| public:
| B();
| void DoSomethingWithInstanceOfA(???);
| ~B();
| };
| 
| A::A()
| {
| // Do stuff
| }
| 
| B::B()
| {
| // Do stuff
| }
| 
| B::DoSomethingWithInstanceOfA(???)
| {
| // Do stuff
| }
| 
| RCPP_MODULE(mod_A)
| {
| using namespace Rcpp;
| class_<A>( "A" )
| .constructor();
| }
| 
| RCPP_MODULE(mod_B)
| {
| using namespace Rcpp;
| class_<B>( "B" )
| .constructor()
| .method("DoSomethingWithInstanceOfA", &B::DoSomethingWithInstanceOfA);
| }
| 
| 
| ### End pseduo c++ code
| 
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list