[Rcpp-devel] Create and access several instances of a C++ class from R

soeren.vogel at uzh.ch soeren.vogel at uzh.ch
Fri May 13 20:07:21 CEST 2011


On 13.05.2011, at 18:55, Dirk Eddelbuettel wrote:

> On 13 May 2011 at 16:11, soeren.vogel at uzh.ch wrote:
> 
> | Following all recommendations, we have rewritten our class to use STL. However, I have been trying around with various changes, but I can't get it working with RCPP_MODULES. Also, the code snippets posted by Romain Francois did not help since we cannot use inline, rather we need raw C++ code that can be compiled using R CMD BUILD etc. Therefore, I kindly ask the experts here for an example adaption to the source below. I guess you experts instantly see what needs to be done next to make the code working. Could you please give tips for the next step?
> 
> Could you detail what you mean by "can't get it working"?  Does it not
> compile? If so, can you show how you tried to compile and the error you are
> getting?
> 
> I'd like to help, but there is not enough information in this post.

Hello Dirk

Compilation with R CMD CHECK FOO fails with the following error in 00install.out:

Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so':
  dlopen(/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so, 6): Symbol not found: __ZN3FOOC1Ev
  Referenced from: /Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so
  Expected in: flat namespace

The (original) class and its functions compile fine with R CMD SHLIB. So we guess that this error has something to do with the Rcpp modules implementation.

Currently, FOO_mod.cpp is embedded in a package "FOO" created with Rcpp.pack...("FOO", module=TRUE), DESCRIPTION and NAMESPACE were adjusted accordingly, all other C sources were removed. Recapture that the package compiled and installed with our previous FOO version, however, since we wanted to use pointers, rewriting was necessary. After that, even exposing a blank class (FOO without anything, except the constructor, but that again without parameters) fails with the error above. I guess that we still do not manage to expose the variables and the one method used in the class to R -- but we have no idea how to accomplish. If I know how to do it with one, say, int, one vector and the one method, perhaps I could understand how to do for the (many more) other variables in the complete class (indeed, FOO is only a reduced example).

What we want to prevent is to completely write the C-code for R use only. The best solution would be to change only some lines of code or even write a simple wrapper for the header file containing the class (there in C++). However, we thought it would be helpful to get it working in the first place, and then reengineer the code back to a more modular source.

Thank you for your help
Sören


> | /* FOO_mod.cpp */
> | #include <Rcpp.h>
> | #include <vector>
> | #include <valarray>
> | using namespace std;
> | 
> | // from FOO.h
> | class FOO
> | {
> | 		double dtau;
> | 		vector<double> D, ee, ff, S;
> | 
> | 	public:
> | 		int M;
> | 		vector<double> C, t, s, par;
> | 		vector<int> y;
> | 
> | 		FOO();
> | 		~FOO();
> | 
> | 		double do_bar(vector<double> z);
> | };
> | 
> | // from FOO.cpp
> | double FOO::do_bar(vector<double> z)
> | {
> | 	// whatever it does
> | }
> | 
> | RCPP_MODULE(mod_foo){
> | 	using namespace Rcpp ;
> | 	class_<FOO>( "FOO" )
> | 		.constructor()
> | 		.field( "M" , &FOO::M )
> | 		.field( "C" , &FOO::C )
> | 		.method( "do_bar", &FOO::do_bar )
> | 	;
> | }
> | 
> 
> -- 
> Gauss once played himself in a zero-sum game and won $50.
>                      -- #11 at http://www.gaussfacts.com
> 


More information about the Rcpp-devel mailing list