Passing many argumentes (...) by ellipsis in Rcpp
r, rcpp
Solution
`Rcpp11` has the concept of variable number of arguments with the `Dots` and `NamedDots` class. You'd do something like this:
#include <Rcpp11>
List force_dots( const Dots& dots ){
List out(n) ;
for( int i=0; i<n; i++){
out[i] = Rcpp_eval( dots.promise(i), dots.environment(i)) ;
}
return out ;
}
// [[export]]
List dots_example(NumericVector x, Dots dots){
int n = dots.size() ;
List args = force_dots(dots) ;
return args ;
}
/*** R
dots_example(1:10, "e" )
# [[1]]
# [1] "e"
*/
When you use `attributes::sourceCpp` on this file, you get an R function that has ellipsis:
> dots_example
function(x, ...){
res <- .Call( "sourceCpp_dots_example" , x, environment())
res
}
This only partly answers the question, i.e. how to pass down to C++ a variable number of arguments from R.
You'd also need something similar to R's `do.call` for when you call the `another_function` function. For now you sort of have to do it manually until we find a way to implement a useful `do_call`
Problem
I'm trying to pass arguments in rcpp function using ... but it is not working. How to do this correctly? ``` NumericVector function(SEXP xR, ...){ NumericVector x(xR); int lenx = x.size(); NumericVector ret(lenx); for(int i=0; i < lenx; i++){ if(x[i]<0){ ret[i] = 0; }else if(x[i]>1){ ret[i] = 1; }else{ ret[i] = anotherfunction(x[i], ...); } } return ret; } ``` In current version I get this error: `expected primary-expression before '...' token`