Skip to content
Prev 40166 / 63424 Next

Create and access several instances of a C++ class from R

Hello

Thanks again for help!

We have attached version info, code, and contents of 00install.out at the end of the message. The package can be found here:

http://sovo.md-hh.com/files/Foo.tar.gz

We had followed Dirks explanation in the Rcpp modules vignette and the presentation of Rcpp modules. The steps there and the ones Dirk listed here are basically the same, and all worked fine for us, so far. Even more, we have adapted some of the example code to a class Foo with two methods of interest, do_foo() and do_bar(). We have played around with various combinations of which method to expose in the module. In the end, exposing do_foo() works, R CMD CHECK does not complain. (Except for some namespacing error, but the same happens when I compile the example "simple", see below.)

Anyway, if we want the method do_bar() to expose, R CMD CHECK Foo stops with an error that says: "cannot convert ?SEXPREC*? to ?double*? in initialization". We *guess* that it is pointers as arguments in methods exposed that cause errors, because there is no wrappers "as()" and "wrap()" for pointers in Rcpp. Indee, we know that our crystal ball may fool us.

Summarising so far, principally, Rcpp modules works for us and coding with this module is very easy, especially with the help files provided in the package. However, Rcpp modules does not work if you want to expose methods with pointers as exposed methods' arguments (we assume). Yet, we have to use pointers, and rewriting the complete class is no option.

Recently, Dirk gave us the advice to have a look at STL containers. We have googled and found some information, and "looking at it" works fine, however, working with it is somewhat "beyond" our C++ skills. If the "STL things" *are* our *only* solution, could some reader here provide us with a specific solution to exposing do_bar()? Or what else can we do to (1) use the class as is (because it works with dyn.load as well as in other software outside R) and (2) incorporate things into Rcpp modules (or other code) anyway?

Thanks for all help!

S?ren and Carlo
On 07.05.2011, at 02:39, Dirk Eddelbuettel wrote:

            
R: sessionInfo()

R version 2.13.0 (2011-04-13)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Rcpp_0.9.4

loaded via a namespace (and not attached):
[1] tools_2.13.0



/* ### Foo_mod.cpp ### */
#include <Rcpp.h>

class Foo
{
	int N, M, n, m;
	double dtau;
	double * C;
	double * t;
	double * s;
	double * par;
	double * D;
	double * ee;
	double * ff;
	double * S;
	int * y;
	public:
		Foo(int N_, int M_, int n_) : N(N_), M(M_), n(n_)
		{
			D = new double[M];
			ee= new double[N];
			ff= new double[N];
			S = new double[n+1];
		}
		~Foo()
		{
			delete[] D;
			delete[] ee;
			delete[] ff;
			delete[] S;
		}
		// important methods
//		void set_experiment(int m, double *C, double *s); // original code
		void do_foo(int m);
		void do_bar(double *C);
		// other stuff
		void set_data(int n, int *y, double *t);
		void set_accuracy(int N, int M);
		void set_par(double *par);
		double loglikelihood(double *z);
};

//void Foo::set_experiment(int mm, double *CC, double *ss)
void Foo::do_foo(int mm)
{
	m=mm;
}
void Foo::do_bar(double *CC)
{
	C=CC;
}

RCPP_MODULE(mod_foo)
{
	using namespace Rcpp;
	class_<Foo>( "Foo" )
		.constructor<int,int,int>()
		.method("do_foo", &Foo::do_foo) // works
		.method("do_bar", &Foo::do_bar) // does not work!
	;
}


### compile warning with the working version:
** checking whether the name space can be loaded with stated dependencies ... WARNING
Error: .onLoad failed in loadNamespace() for 'Foo', details:
  call: value[[3L]](cond)
  error: failed to load module mod_foo from package Foo
Execution halted
A namespace must be able to be loaded with just the base namespace
loaded: otherwise if the namespace gets loaded by a saved object, the
session will be unable to start.
Probably some imports need to be declared in the NAMESPACE file.


### 00install.out
* installing *source* package ?Foo? ...
** libs
*** arch - i386
g++-4.2 -arch i386 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/i386  -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include"   -fPIC  -g -O2 -c Foo_mod.cpp -o Foo_mod.o
/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include/Rcpp/traits/Exporter.h: In constructor ?Rcpp::traits::Exporter<T>::Exporter(SEXPREC*) [with T = double*]?:
/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include/Rcpp/as.h:51:   instantiated from ?T Rcpp::internal::as(SEXPREC*, Rcpp::traits::r_type_generic_tag) [with T = double*]?
/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include/Rcpp/as.h:75:   instantiated from ?T Rcpp::as(SEXPREC*) [with T = double*]?
/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:122:   instantiated from ?SEXPREC* Rcpp::CppMethod1<Class, void, U0>::operator()(Class*, SEXPREC**) [with Class = Foo, U0 = double*]?
Foo_mod.cpp:59:   instantiated from here
/Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include/Rcpp/traits/Exporter.h:31: error: cannot convert ?SEXPREC*? to ?double*? in initialization
make: *** [Foo_mod.o] Error 1
ERROR: compilation failed for package ?Foo?