1

What I want to do is similar to this question. That is, passing a function as a parameter. But I want to implement it in Armadillo in R.

I found this answer to be relevant, as it is an implementation in C++. However, after I pasted the code to Armadillo in R and ran it, the following error showed up

"no viable conversion from returned value of type 'std::__1::__nat' to function return type 'double'".

Here is my code

#include <Rcpp.h>

#include <functional>

// [[Rcpp::export]]
double Combiner(double a, double b, std::function<double (double,double)> func){
  return func(a,b);
}

double Add(double a, double b){
  return a+b;
}

double Mult(double a, double b){
  return a*b;
}

/*** R
Combiner(12,13,Add)
Combiner(12,13,Mult)

*/

What is wrong with my code? or is there any other method to "passing function as a parameter in Armadillo in R"? Thanks!

Ding Li
  • 673
  • 1
  • 7
  • 19
  • I think [this is related](https://stackoverflow.com/q/56456176/5977215) - in order for a function to be `exported` back to R, there has to be an equivalent R representation of the function arguments. – SymbolixAU Jul 16 '19 at 04:23

0 Answers0