将包装函数内的可选参数传递给子函数

Dan*_*gan 6 r function

我有一个包装函数,我需要将可选参数传递给指定的子函数.但是有许多不同的可能子功能我无法预先指定它们.作为参考,子功能存在于环境等...考虑:

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. 
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 5

你需要传递...method.out.然后它工作正常:

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
Run Code Online (Sandbox Code Playgroud)