How to export Rcpp Class method with default arguments

£可爱£侵袭症+ 提交于 2019-12-12 16:22:02

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/22241687/how-to-export-rcpp-class-method-with-default-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!