[Rcpp-devel] module/S4 retval

Dirk Eddelbuettel edd at debian.org
Thu Sep 6 16:53:09 CEST 2012


On 6 September 2012 at 09:42, Richard Downe wrote:
| Absolutely, my apologies, I'd just been hitting "reply".
| 
| It does appear as though instances of "classes" as encapsulated by 
| modules show up in R as S4 objects.  I had naively thought that by using 

Actually as Reference Classes -- see help(setRefClass) about them.  John as
added to this to extend Rcpp modules with the ability to extend what we get
from the C++ classes via Rcpp modules on the R side. He called it Rcpp
classes, and there is a little bit in the current Rcpp versions.

| the Rcpp::S4 constructor I could return a valid instance, but there is 
| clearly quite a bit of magic going on (I still can't quite figure out 
| how to shoehorn my XPtr into the S4 object properly).
| 
| I've been debating trying to figure out how to construct one using the 
| raw R C api, but if there's a way to piggyback on the modules code in a 
| way that lets me instantiate an instance of my R module class from the 
| c++ side (but presumably present said value to wrap() as an S4 object) 
| that would be ideal.

"There should be" but I can't guide you on this. On the Rcpp side this is
code written by Romain who is currently taking a less active role. He may
read this message, or may not.  So you are on your own.

But starting there, methinks, gives you a better starting point than starting
at the raw C API for R.

Dirk

| -rd
| 
| On 09/06/2012 06:25 AM, Dirk Eddelbuettel wrote:
| > Good post -- should we keep this on rcpp-devel ?
| >
| > Not many people in the C++ and R and S4 and Rcpp intersection, I don't hit
| > all of these as I do little S4 :)
| >
| > Dirk
| >
| > On 5 September 2012 at 23:17, Richard Downe wrote:
| > | Yeah...after some consultation with other c++ hacks, it sounds as though
| > | using regexps with perl or python to generate the module declaration is
| > | far cleaner than anything I could do with template metaprogramming.
| > |
| > | I did find a boost library called "mirror" which, if it ever makes it
| > | into mainline boost, might be safe to use in Rcpp, at least optionally,
| > | to automagically export all public members, but as long as it's outside
| > | the official distribution, simply parsing the headers in the "configure"
| > | script and including the result with #include is far less work  (I think
| > | I'm going to use comment block markers, such as
| > | // @@BEGIN_RCPP_EXPORTS@@
| > | to bound the parts of the header I want slurped into a module delcaration).
| > |
| > | I do seem to have 1 residual sticking point.  When, in my object
| > | factory, I call the Rcpp::S4 constructor on a class name, that works,
| > | but then I can't seem to find a valid means of binding the pointer.
| > |
| > | Attached is what I'm attempting -- I'm assuming there's a better way
| > | (perhaps by somehow accessing the module's bound ctor?)?
| > |
| > |          Rcpp::S4 CreateBLMorph(long fusKey) {
| > |              using namespace std;
| > |              vector< pair<string,long> > terms;
| > |              terms.push_back( make_pair( "fusid", fusKey ) );
| > |              terms.push_back( make_pair( "ivussegid",
| > | m_regObj.getBLSegID() ) );
| > |
| > |              pqxx::result res = ExecuteSelect( string("fusion"), terms );
| > |
| > |              if (res.size()) {
| > |                  Rcpp::S4 retVal("Rcpp_morphologyIndices");
| > |                  retVal.slot( "pointer" ) = Rcpp::XPtr<
| > | morphologyIndices >( new morphologyIndices(fusKey), true );
| > |                  return retVal;
| > |              }
| > |              else {
| > |                  stringstream errorMessage;
| > |                  errorMessage << "No fusion with fusid = " << fusKey <<
| > | " or specified fusion not associated "
| > |                               << "with current registration object.";
| > |                  throw runtime_error( errorMessage.str() );
| > |              }
| > |          }
| > |
| > | -rd
| > |
| > | On 09/05/2012 08:55 PM, Dirk Eddelbuettel wrote:
| > | > Hi Richard,
| > | >
| > | > On 5 September 2012 at 16:22, Richard Downe wrote:
| > | > | I would like to be able to create object factories in my rcpp module,
| > | > | such that I can call
| > | > | classA::GetInitializedB(), and get an S4 object (module instance?) back.
| > | > |  From what I can tell, the "module" class definition macros are not
| > | > | quite flexible to permit this, but I'm wondering if there's a way to
| > | > | leverage the code in Module.h/cpp in an operator SEXP() block to
| > | > | appropriately wrap up object factory pointers.
| > | > |
| > | > | I really am loathe to have to enumerate all exported methods *again*,
| > | > | and would like to be able to use template metaprogramming to slurp in a
| > | > | header, parse it, and use that to enumerate the list of methods in the
| > | > | returned S4 object.
| > | > |
| > | > | Has anyone ever done anything like this?
| > | >
| > | > Not that I can recall. Would be nice to have though.
| > | >
| > | > I did some work recently with Rcpp modules. It is pretty straightforward, and
| > | > if you wanted to, you could probably write a header parser / module
| > | > declaration generator. So far, I've been happy to do it by hand as it has not
| > | > been reptitive but rather exploratory.
| > | >
| > | > Dirk
| > | >
| > |
| >
| 

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


More information about the Rcpp-devel mailing list