[Rcpp-devel] Method with default params using RCPP_MODULE

Dirk Eddelbuettel edd at debian.org
Wed Oct 26 17:53:38 CEST 2016


On 26 October 2016 at 09:48, Sergio Bra wrote:
| I am exposing a c++ class using Rcpp. It works fantastic, but my problem is how
| to implement a method which has default params. I have the class:
| 
| class myClass {
| 
|     public:
|     
|         int myMethod(int param1, int param2 = 0);
| }
| 
| and I config the module:
| 
| RCPP_MODULE(mymodule) {
|     class_<myClass>("myClass")
|         .constructor()
|         .method("myMethod", &myClass::myMethod)
| ;
| }
| 
| When I try to execute the function in R passing only the mandatory param I get
| an error because R does not recognize the function, but if I execute it with
| both params, it works great. So, how can I define the method in order to be
| able to execute it without informing the optional params?

Hm, I have the feeling I am doing that somewhere too.  Did you check existing
packages with modules?

I just glanced at RcppCNPy and it does it, albeit with free functions.

You can definitely have multiple constructors, maybe have one with one int
(defaulting value two) and one with two ints?   There are subtle constraints
where because of the _C_ interface so you have to 'waste' two function names
on this.

In sum, the easiest may be to 'hide' the module and deal with this from the R
side exposing both variants from there and then just calling the one you have now.

Makes sense?

Dirk

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


More information about the Rcpp-devel mailing list