[Rd] how to call a function from C

From: Romain Francois <romain.francois_at_dbmail.com>
Date: Thu, 14 Jan 2010 11:42:42 +0100


Hi,

In Rcpp, we now have a "Function" class to encapsulate functions (they cover all three kinds, but this may change).

To call the function, what we do is generate a call with the function as the first node and then evaluate the call.

SEXP stats = PROTECT( R_FindNamespace( mkString( "stats") ) ); SEXP rnorm = PROTECT( findVarInFrame( stats, install( "rnorm") ) ) ; SEXP call = PROTECT( LCONS( rnorm, CONS( ScalarInteger(10), CONS(ScalarReal(0), R_NilValue ) ) ) );
SEXP res = PROTECT( eval( call , R_GlobalEnv ) ); UNPROTECT(4) ;
return res ;

It works, but I was wondering if there was another way. I've seen applyClosure, but I'm not sure I should attempt to use it or if using a call like above is good enough.

Romain

PS: using Rcpp's C++ classes you would express the code above as :

Environment stats("package:stats") ;
Function rnorm = stats.get( "rnorm" )
return rnorm( 10, 0.0 ) ;

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr

|- http://tr.im/KfKn : Rcpp 0.7.2
|- http://tr.im/JOlc : External pointers with Rcpp
`- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009 ______________________________________________ R-devel_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 14 Jan 2010 - 10:49:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 19 Jan 2010 - 01:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive