I have an R package called multicool which handles permutations of multisets. Currently, internally, there exists a C++ class, and a call to initMC creates a new object of class Multicool which then can do all the things I need it to do. However, there is no simple way to release the memory allocated to this object. It doesn't matter for simple uses, but I have an application which might call this hundreds of thousands of times.
The solution, I think, is to expose the class to R using an Rcpp Module. However, I have tried following the instructions and I get the error:
Error: object 'mcModule' not found
Initially - I would just like to expose the object and its constructor. This is my class definition
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
class Multicool{
struct list_el {
int v;
struct list_el * n;
};
typedef struct list_el item;
item *h;
item *t;
item *i;
int *m_pnInitialState;
int *m_pnCurrState;
int m_nLength;
bool m_bFirst;
public:
// constructor
Multicool(IntegerVector x){
int nx = (int)x.size();
}
};
and then I export the class and the constructor with
RCPP_MODULE(mcModule){
using namespace Rcpp;
class_<Multicool>("Multicool")
.constructor<IntegerVector>()
;
}
I have added the line
import(Rcpp)
to my NAMESPACE file
I have added
RcppModules: mcModule
to my DESCRIPTION file
and I have added a call to loadRcppModules in the .onLoad function
.onLoad <- function(libname, pkgname) {
loadRcppModules()
}
All of this compiles and the package builds without complaint. But when I got to create a new Multicool object I get the aforementioned error
> library(multicool)
> Multicool = mcModule$Multicool
Error: object 'mcModule' not found
Any help or advice would be appreciated
You generally need a new
call first. See in my RcppRedis package:
RCPP_MODULE(Redis) {
Rcpp::class_<Redis>("Redis")
.constructor("default constructor")
[...stuff omitted for brevity...]
;
}
where the R code (eg in the demo/
directory) does
suppressMessages(library(RcppRedis))
redis <- new(Redis)
after which you can access functions redis$foo()
etc that are part of the module.
So try adding
mcModule <- new(mcModule)
before accessing mcModule$Multicool
.
Edit: Looks like you were missing the loadModule("mcModule", TRUE)
call.
Edit 2: To be more explicit, I added your file to the (working) testRcppModule
from the package and made the one change you made (to DESCRIPTION
) and one more to an R file to load the module:
edd@max:/tmp/rcpp/module$ diff -ru ~/git/rcpp/inst/unitTests/testRcppModule/ testRcppModule/
diff -ru /home/edd/git/rcpp/inst/unitTests/testRcppModule/DESCRIPTION testRcppModule/DESCRIPTION
--- /home/edd/git/rcpp/inst/unitTests/testRcppModule/DESCRIPTION 2015-08-26 15:53:03.891830292 -0500
+++ testRcppModule/DESCRIPTION 2015-10-22 21:34:23.716959638 -0500
@@ -10,6 +10,6 @@
LazyLoad: yes
Depends: methods, Rcpp (>= 0.8.5)
LinkingTo: Rcpp
-RcppModules: RcppModuleWorld, stdVector, NumEx
+RcppModules: RcppModuleWorld, stdVector, NumEx, mcModule
Packaged: 2010-09-09 18:42:28 UTC; jmc
diff -ru /home/edd/git/rcpp/inst/unitTests/testRcppModule/R/zzz.R testRcppModule/R/zzz.R
--- /home/edd/git/rcpp/inst/unitTests/testRcppModule/R/zzz.R 2015-08-26 15:53:03.891830292 -0500
+++ testRcppModule/R/zzz.R 2015-10-22 21:41:41.468532838 -0500
@@ -8,4 +8,5 @@
loadModule("RcppModuleNumEx", TRUE)
loadModule("RcppModuleWorld", TRUE)
loadModule("stdVector", TRUE)
+loadModule("mcModule", TRUE)
Only in testRcppModule/src: multicool.cpp
edd@max:/tmp/rcpp/module$
With that, all is good:
$ r --package testRcppModule --eval 'm <- new(mcModule); print(m)'
C++ object <0x757d18> of class 'mcModule' <0x1adeab0>
$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With