[Rcpp-devel] Rcpp modules

Romain Francois romain at r-enthusiasts.com
Wed May 19 15:07:33 CEST 2010


One thing perhaps I'd like opinions about is the use if the name 
"function" in :

RCPP_MODULE(yada){
	using namespace Rcpp ;
	function( "hello" , &hello ) ;
}

Boost.Python uses "def" but this is too pythonic for me. OTOH, having 
both Rcpp::Function (the class that deals with R functions) and 
Rcpp::function (for the module code) might be confusing.

Any suggestion ? Maybe Rcpp::fun ? Or should I use some other namespace 
for this functionality ?

Romain

Le 19/05/10 12:09, Romain Francois a écrit :
>
> Hello,
>
> I've been looking at Boost.Python lately and thought we might be able to
> port some of the ideas to Rcpp too.
>
>
> I commited (rev 1281) some code to Rcpp that introduces the notion of
> Rcpp modules (wording might change later). A module is a collection of
> C++ functions that :
> - take 0 or more parameters of arbitrary type, not really arbitrary but
> anything that Rcpp::as can deal with
> - returns void or any type that Rcpp::wrap can handle
>
>
> Here is an example of C++ code using the concept of Rcpp module :
>
> std::string hello(){
> return "hello" ;
> }
>
> int bar( int x){
> return x*2 ;
> }
>
> double foo( int x, double y){
> return x * y ;
> }
>
> void bla( ){
> Rprintf( "hello\\n" ) ;
> }
>
> void bla1( int x){
> Rprintf( "hello (x = %d)\\n", x ) ;
> }
>
> void bla2( int x, double y){
> Rprintf( "hello (x = %d, y = %5.2f)\\n", x, y ) ;
> }
>
>
> RCPP_MODULE(yada){
> using namespace Rcpp ;
>
> function( "hello" , &hello ) ;
> function( "bar" , &bar ) ;
> function( "foo" , &foo ) ;
> function( "bla" , &bla ) ;
> function( "bla1" , &bla1 ) ;
> function( "bla2" , &bla2 ) ;
>
> }
>
> All the magic happens because the compiler knows how to distinguish the
> type of each function pointer and we manage to intercept this
> information through C++ templates.
>
> The only price to pay is the declarative call to the RCPP_MODULE macro.
>
>
> On the R side, we just pick it up using the Module function in Rcpp :
>
>  > # inc is the code above
>  > fx <- cppfunction( signature(), "", include = inc )
>  >
>  > # look for the Module called "yada" in the DLL we just compiled
>  > yada <- Module( "yada", getDLL( fx ) )
>  > print( yada )
> Rcpp module 'yada'
> 6 functions:
> bar : 1 arguments
> bla : 0 arguments
> bla1 : 1 arguments
> bla2 : 2 arguments
> foo : 2 arguments
> hello : 0 arguments
>  >
>  > print( yada$hello() )
> [1] "hello"
>  > print( yada$bar( 2L ) )
> [1] 4
>  > print( yada$foo( 2L, 4.0 ) )
> [1] 8
>  > yada$bla()
> hello
>  > yada$bla1( 2L )
> hello (x = 2)
>  > yada$bla2( 2L, 5 )
> hello (x = 2, y = 5.00)
>
>
>
> Right now, this is limited to a maximum of 2 parameters because I wrote
> this manually, but once I make some R code to generate the template
> code, we should be able to get up to 65 parameters (same as .Call)

Done !

> functions are only the tip of boost.python iceberg, obviously we also
> want to expose C++ classes the way boost.python does, but this might be
> slightly more involved.
>
>
> Romain
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bklUXt : RcppArmadillo 0.2.1
|- http://bit.ly/936ck2 : Rcpp 0.8.0
`- http://bit.ly/9aKDM9 : embed images in Rd documents



More information about the Rcpp-devel mailing list