[Rcpp-devel] named and default arguments in exposed C++ classes

Dirk Eddelbuettel edd at debian.org
Wed Mar 5 16:36:22 CET 2014


On 5 March 2014 at 16:26, Andreas Prescher wrote:
| Hello,
| 
| I wrote a R package using Rcpp (0.10.4) modules
| to expose my C++ classes.
| 
| All works fine, but for convenient using from R,
| it would be great to use the class methods with
| named arguments and default arguments.
| 
| At the moment I have to use all class methods as
| declared in the corresponding C++ header files.
| 
| something like this:
| RCPP_MODULE(PsiMin) {
| 
| class_<Foo>( "Foo" )
| ..method( "bar", &Foo::bar); }
| 
| class Foo  {
| 
| int bar(int x, int y = 2) {return x + y};
| }
| 
| using from R
|  > f <- new(Foo)
|  > f$bar(x = 1) => 3
|  > f$bar(x = 1, y = 1) => 2
| 
| I only can call my methods like this:
|  > f$bar(3,4) => 7
| 
| Is there a workaround to make named/default
| arguments working when using classes?
| 
| E.g. make some "mix" with 'function'
| and 'class_'.
| 
| Any suggestions would be great.

In RcppCNPy I use free functions exposed via Rcpp Modules and that works:

  R> library(RcppCNPy)
  R> npyLoad
  internal C++ function <0x19721a0>
      docstring : read an npy file into a numeric or integer vector or matrix
      signature : Rcpp::RObject npyLoad(std::string, std::string, bool)
  R> args(npyLoad)
  function (filename, type = "numeric", dotranspose = TRUE) 
  NULL
  R> str(npyLoad(file="/home/edd/git/rcppcnpy/tests/fvec.npy"))
   num [1:5] 0 1.1 2.2 3.3 4.4
  R> 

If it doesn't work that way for autoexported member functions you may need to
a) live with it, b) but a wrapper function around it or c) patch Rcpp Modules.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list