[Rcpp-devel] Rcpp modules

Dirk Eddelbuettel edd at debian.org
Thu May 20 10:11:59 CEST 2010


On 20 May 2010 at 09:49, Romain Francois wrote:
| Le 20/05/10 09:33, Dirk Eddelbuettel a écrit :
| >
| > On 19 May 2010 at 15:07, Romain Francois wrote:
| > | 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 ?
| >
| > Tough one. I also think that maybe 'function' is iffy. That said, it is not a
| > keyword at the C++ language level (!!) so maybe even mingw will be kind to us
| > (inside joke alert here).
| 
| tested. not as bad as stderr
| 
| > Maybe 'Function' just to show it is different?
| 
| We already have Rcpp::Function to handle R functions in c++ code: 
| Rcpp::Function( "rnorm" ), etc ...
| 
| 
| Given function really sets up a c++ function (as opposed to an R 
| function), maybe we can emphacize this (cppfunction, cppfun, or something).
| 
| Something else I had in mind was this sort of notation:
| 
| RCPP_MODULE(yada){
|    using namespace Rcpp ;
|    functions[ "hello" ] = &hello ;
| }
| 
| 
| For exposing C++ classes, I think I'll follow boost.python and use "class_"

Maybe 'addFunction' and 'addClass' ?

  RCPP_MODULE(yada){
     using namespace Rcpp ;
     addFunction[ "hello" ] = &hello ;
     addClass[ "world" ] = ....
  }

which stresses the fact that we are building an interface on-the-fly?

Could also drop the leading 'add' and make it lowercase. The hashed
assignment is nice as it is.

Dirk
 
| > But that is odd too.  Maybe   f(...)   in tip-of-the hat to math?
| >
| > Maybe start with function. The option of later wrapping it into a
| > sub-namespace or its own namespace is good too.
| 
| Any idea for a good name in the marketing department ?
| 
| > Dirk
| >
| > | 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
| 

-- 
  Regards, Dirk


More information about the Rcpp-devel mailing list