Hi everyone, I have two questions regarding passing a function name as an
argument.

(1). Suppose that I have a function foo() shown below. The function takes a
NumericVector and a function pointer that points to a function that takes a
NumericVector and returns a double. Note that the function pointer will
only point to functions not exposed to R.

double foo(NumericVector x, double (*f)(NumericVector x) ){
double output;
 output=(*f)(x);
return output;
}

When I try to compile it as is using sourceCpp(), it's fine, but when I add
"// [[Rcpp::export]]" above the function definition, I get an error message:

Error in sourceCpp("foo.cpp") :
  Error 1 occurred building shared library.
foo.cpp:229: error: expected initializer before ‘SEXP’

So I wonder how I can fix this mistake.


(2). Imagine a more complex scenario: suppose there are two functions
available to be passed to foo(). But the two functions differ in the number
of arguments each has (see fun1() and fun2() below). I wonder if there is
any way to deal with this.

double fun1(NumericVector x){
double total=0;
 for(int i=0;i<x.size();i++)
total+=x(i);
 return total/10;
}


double fun2(NumericVector x, int n){
double total=0;
 for(int i=0;i<x.size();i++)
total+=x(i)+add;
 return total/n;
}
 Thank you in advance!


Best,
-Xiao
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to