将函数列表作为参数传递的最佳方法是什么?

Emm*_*Lin 6 r function parameter-passing

我正在寻找关于将函数列表作为参数传递代码的最佳方法的建议.

我想做的事:

我想作为一个参数传递一个函数来将它们应用于特定的输入.并根据这些提供输出名称.

一个"可重复的"例子

input = 1:5
Run Code Online (Sandbox Code Playgroud)

传递的功能是mean,min

预期电话:

foo(input, something_i_ask_help_for)
Run Code Online (Sandbox Code Playgroud)

预期产量:

list(mean = 3, min = 1)
Run Code Online (Sandbox Code Playgroud)

如果不是很清楚,请参阅我的两个解决方案以进行说明.

解决方案1:将函数作为参数传递

foo <- function(input, funs){
  # Initialize output
  output = list()

  # Compute output
  for (fun_name in names(funs)){
    # For each function I calculate it and store it in output
    output[fun_name] = funs[[fun_name]](input)
  }

  return(output)
}

foo(1:5, list(mean=mean, min=min))
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种方法是我们不能通过这样做来调用它:foo(1:5, list(mean, min)).

解决方案2:将函数名称作为参数传递并使用get

foo2 <- function(input, funs){
  # Initialize output
  output = list()

  # Compute output
  for (fun in funs){
    # For each function I calculate it and store it in output
    output[fun] = get(fun)(input)
  }

  return(output)
}
foo2(1:5, c("mean", "min"))
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种方法的是我们并没有真正将函数对象作为参数传递.

我的问题:

两种方式都有效,但我不太确定选择哪一种方式.

你能帮帮我吗:

  • 告诉我哪一个是最好的?
  • 每种方法的优点和缺点是什么?
  • 还有另一个(更好)的方法

如果您需要更多信息,请不要犹豫.

谢谢!

G. *_*eck 5

简化有问题的解决方案

问题中的第一个解决方案要求为列表命名,第二个解决方案要求函数具有作为字符串传递的名称。可以使用以下简化来实现这两个用户界面。请注意,我们添加了一个envir参数以foo2确保函数名称查找按预期进行。其中第一个看起来更清晰,但如果这些功能是交互使用的并且需要较少的输入,那么第二个就不需要指定名称。

foo1 <- function(input, funs) Map(function(f) f(input), funs)
foo1(1:5, list(min = min, max = max)) # test

foo2 <- function(input, nms, envir = parent.frame()) {
    Map(function(nm) do.call(nm, list(input), envir = envir), setNames(nms, nms))
}
foo2(1:5, list("min", "max")) # test
Run Code Online (Sandbox Code Playgroud)

或者,我们可以建立foo2foo1

foo2a <- function(input, funs, envir = parent.frame()) {
    foo1(input, mget(unlist(funs), envir = envir, inherit = TRUE))
}
foo2a(1:5, list("min", "max")) # test
Run Code Online (Sandbox Code Playgroud)

或基于传递包含函数名称的公式的用户界面,因为公式已经包含环境的概念:

foo2b <- function(input, fo) foo2(input, all.vars(fo), envir = environment(fo))
foo2b(1:5, ~ min + max) # test
Run Code Online (Sandbox Code Playgroud)

传递函数本身时的可选名称

然而,问题表明,最好是

  • 函数本身被传递
  • 名称是可选的

为了合并这些功能,以下允许列表具有名称或不具有名称或混合名称。如果列表元素没有名称,则使用定义函数的表达式(通常是它的名称)。

我们可以从列表的名称中导出名称,或者当缺少名称时,我们可以使用函数名称本身,或者如果函数是匿名的,因此作为其定义给出,则名称可以是定义函数的表达式。

关键是使用match.call和挑选它。我们确保它funs是一个列表,以防它被指定为字符向量。match.fun将解释函数和字符串命名函数并在父框架中查找它们,因此我们使用for循环代替Maplapply为了不生成新的函数作用域。

foo3 <- function(input, funs) {
  cl <- match.call()[[3]][-1]
  nms <- names(cl)
  if (is.null(nms)) nms <- as.character(cl)
  else nms[nms == ""] <- as.character(cl)[nms == ""]
  funs <- as.list(funs)
  for(i in seq_along(funs)) funs[[i]] <- match.fun(funs[[i]])(input)
  setNames(funs, nms)
}

foo3(1:5, list(mean = mean, min = "min", sd, function(x) x^2))
Run Code Online (Sandbox Code Playgroud)

给予:

$mean
[1] 3

$min
[1] 1

$sd
[1] 1.581139

$`function(x) x^2`
[1]  1  4  9 16 25
Run Code Online (Sandbox Code Playgroud)