Passing optional arguments inside a wrapper function to a sub-function

function, r

Solution

You need to pass the `...` to `method.out`. Then it works fine:

funInFun<- function (x, method, ...) {    

  method.out <- function(this.x, FUN, ...) {
    FUN <- match.fun(FUN)
    c <- FUN(this.x, ...)
    return(c)
  }

  d <- method.out(x, method, ...)  # <<--- PASS `...` HERE
  return(d)
}

data<-seq(1,10)
funInFun(data, mean) #  Works
# [1] 5.5    

data<-c(NA,seq(1,10))
funInFun(data, mean, na.rm=TRUE) # Should remove the NA
# [1] 5.5

funInFun(c(seq(1,10)), quantile, probs=c(.3, .6)) 
# 30% 60% 
# 3.7 6.4

Problem

I have a wrapper function, where I need to pass optional arguments to the sub-function specified. But there are so many different possible sub-functions that I can't pre-specify them. For reference, the sub-functions exist in the environment etc... Consider: ``` funInFun<- function (x, method, ...) { method.out <- function(this.x, FUN, ...) { FUN <- match.fun(FUN) c <- FUN(this.x, ...) return(c) } d <- method.out(x, method) return(d) } data<-seq(1,10) funInFun(data, mean) # Works data<-c(NA,seq(1,10)) funInFun(data, mean, na.rm=TRUE) # Should remove the NA funInFun(c(seq(1,10)), quantile, probs=c(.3, .6)) # Shoudl respect the probs option. ```

Original source