user3294195 user3294195 - 1 year ago 62
R Question

Rcpp - Define a C++ function that takes an R function and an ellipsis argument

I have an R function

that takes another R function
, defined as follows:

foo <- function(x,y) x + y
bar <- function(foo, z, ...) z + foo(...)

A call to
would be of the form:

bar(foo, 1,2,3)

Now with
defined as above, I want to create a C++ version of
. Here's what I've tried:

double bar(Function foo, double z, ...) {
return z + foo(...);

This clearly doesn't work. What would be the right way to define this function in C++?


Answer Source

It might be easier to turn your ellipsis into a list before feeding it to Rcpp

bar <- function(foo, z, ...) {
   args <- list(...)
   bar_internal(foo, z, args)

And then your Rcpp function can simply take a Rcpp::List instead of ellipsis.

double bar_internal(Function foo, double z, List args){

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download