Pregunta

I have a c++ class myClass which has a method foo(int x=0) and it has a parameter x with default value = 0. The c++ class could be exported to R by

RCPP_MODULE(my_module) {
    class_< myClass >( "myClass" )
    .constructor()
    .method( "foo", &myClass::foo )
    ;
}

However, in R, I am not able to call myClass$foo without specifying the value of x. I have to specify the value of x regardless the default value.

So my question is how to export Rcpp class method with default arguments. I tried to search it over the internet. The closest thing that I found was

using namespace Rcpp;
double norm( double x, double y ) { return sqrt( x*x + y*y );
}
RCPP_MODULE(mod_formals2) {
    function("norm", &norm,
}

But it doesn't work in my case.

¿Fue útil?

Solución

I had the same problem recently. After looking at the source file of rcpp handling the classes (~/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include/Rcpp/module/class.h in my setup) I don't think that it is currently possible.

The best workaround I came up with was to create a wrapper in R to handle the default arguments.

Here is a full example demonstrating how to do it. I defined a simple function that accepts 3 arguments and outputs their sum. The second and third arguments are optional and set by default to 10 and 100.

mwe.cpp

#include <Rcpp.h>

class  MWE {
public:
  int sum_them(int mandatory_arg,
               int optional_arg1,
               int optional_arg2)
  {
    return (mandatory_arg+optional_arg1+optional_arg2);
  }
};

RCPP_MODULE(mod_mwe) {
  Rcpp::class_<MWE>( "MWE" )
  .constructor()
  .method("sum_them", &MWE::sum_them)
  ;
}

mwe.R

require('Rcpp')

# source the C++ code
sourceCpp('mwe.cpp')

# create an instance of the class:
my_mwe = new(MWE)

# assign a wrapper with default arguments to the instance:
assign('sum_them_wrapper',
       function(mandatory_arg,
                optional_arg1=10,
                optional_arg2=100) {
         return(my_mwe$sum_them(mandatory_arg, optional_arg1, optional_arg2))
       },
       envir = my_mwe
       )

This outputs the expected result:

> my_mwe$sum_them_wrapper(3, optional_arg2=500)
[1] 513
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top