Skip to content
Prev 40167 / 63424 Next

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

Hello,

Try having do_bar with this signature:

void do_bar(NumericVector data);

And my advice would probably also to have your C field either as a 
NumericVector. NumericVector is just a thin wrapper around the internal 
R data structure.

Having data members as direct pointers is dangerous and not a very C++ 
way of doing things.

If you want to learn about the STL, there are many books that cover it 
and plenty of online resources. I use this : 
http://cplusplus.com/reference/stl/. Using the STL is easier than 
dealing with pointers and stuff.

If your game is to pass down a double* to some api you use, you can use 
the begin method of NumericVector, this will give you the array pointer 
(as would REAL do on the underlying SEXP):

Here's an example:

require( Rcpp )
require( inline )

inc <- '

     // some third party api function
     double foo( double* data, int n){
         double sum = 0.0 ;
         for( int i=0; i<n; i++){
             sum += data[i] ;
         }
         return sum ;
     }
'

fx <- cxxfunction( signature( x_ = "numeric" ), '

     NumericVector x(x_) ;

     double sum = foo( x.begin(), x.size() ) ;

     return wrap( sum ) ;

', includes = inc, plugin = "Rcpp" )


I hope this helps, please send other Rcpp questions to the Rcpp-devel 
mailing list, where you have more chances to have replies. As an 
example, I don't follow R-devel as much as I would like to these days.

Romain

Le 07/05/11 10:41, soeren.vogel at uzh.ch a ?crit :