1

I have a question re: the issue that Rcpp modules are not easily exported as regular R objects. In particular, my code is object-oriented and uses an instance of a class exported to R. The issue has been documented here.

A possible workaround is to load a few things into the environment upon loading but I don't quite understand how to hold back my zzz.R which gets executed prematurely (build vs. load time) which results in an error because the modules called in zzz.R only become available AFTER compilation and installation.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hirek
  • 435
  • 3
  • 12
  • I have the feeling we answered this via a number of related questions. Can you do search? – Dirk Eddelbuettel Aug 13 '21 at 17:58
  • I have but only saw this suggested conceptually and no actual answer, by Roman Francois on an old thread. Can you or someone give a hint of how to properly include the zzz.R or maybe even work out how to give proper citizenship to Rcpp-exported objects? – Hirek Aug 13 '21 at 18:03
  • The short answer is that your best bet, by far, is to wrap your code into a package and have each worker node load the package ensuring proper local instantation. Rcpp Modules is R glue code around compiler code loaded in a 'random' and 'hidden' RAM location on your server and there is no mechanism to send it to nodes, just as there isn't really for persisting modules across sessions. – Dirk Eddelbuettel Aug 13 '21 at 20:18
  • @DirkEddelbuettel Thank you for that. Do you have a reference for making .onLoad work properly? I already have it wrapped in a package because it's using custom headers. I shall make this available for the gallery shortly as it is a good example of using classes and modules. – Hirek Aug 13 '21 at 21:21
  • I am not sure what your problem, and hence question is, but I can assure you that a number of (public, on CRAN) packages use Rcpp Modules just fine so I can only urge you to maybe study one or two of those to identify what you may be doing wrong. Rcpp Modules code has not changed in a while, and generally does not require `zzz.R` or `.onLoad()` so the problem may be yours alone. Sorry! – Dirk Eddelbuettel Aug 13 '21 at 21:52
  • My issue is that I need to construct an object from my C++ class, and loading the package does not allow that. When I ask my zzz.R/.onLoad to execute "MyClass myobj", devtools::document() etc. complain that that MyClass is not found, so I get a catch22. I am thinking of just wrapping construction of my object in a C-style function and returning a function pointer or something like that. – Hirek Aug 13 '21 at 22:08
  • Well all we say is that `loadModules()` be placed in _any_ file in `R/`. So maybe make it `zzz.R` or `init.R` and call it first, then instantiate. Should work. Or, if you need something special, use something else. Modules are just one way, we often also use `Rcpp::XPtr` to instantiate ourself. – Dirk Eddelbuettel Aug 13 '21 at 22:29
  • I think Rcpp::XPtr is the way to go. I shall try and instatiate at the cpp level and return an XPtr using https://gallery.rcpp.org/articles/passing-cpp-function-pointers/ if that is what you mean? – Hirek Aug 14 '21 at 14:10
  • I agree. You get more control. I keep forgetting in which package I did this (maybe it was once at work...) but I set up a basic class, and then some simple helper functions to a) instantiate and hook to a (global) pointer, b) access setter and gettres and c) and unwind function to release. Basically what Modules gives you, but more manually for finer control. – Dirk Eddelbuettel Aug 14 '21 at 14:53

0 Answers0