r - 传递所有参数
问题描述
考虑以下:
foo <- function(a = 1, b = 2, c = 3, d = 1, e = 2, f = 3, g = 4, h = 1) {
print(h)
bar(a = a, b = b, c = c, d = d, e = e, f = f, g = g)
foobar(c = c, e = e, g = g)
}
bar <- function(a, b, c, d, e, f, g) {
a + b + c + d + e + f * g
}
foobar <- function(c, e, g) {
if (c) {
print(g + e)
}
}
foo(1, 2, 3, 4,5, 6, 7, 8)
我有与上面类似的东西,其中我有子函数调用,这些调用需要从它上面的级别传递下来的大量变量。是否有更简单的方法来执行此代码的第 3 行和第 4 行,而不是每次调用它时手动列出每个变量?
解决方案
好吧,我们可以创建一个辅助函数来使这更容易
call_match_args <- function(fun, data) {
fun <- match.fun(fun)
seeking <- names(formals(fun))
stopifnot(all(seeking %in% names(data)))
do.call(fun, data[seeking], envir=parent.frame())
}
这将接受一个函数和一个列表,并将列表中与函数参数名称匹配的所有值作为参数传递。该do.call
函数负责将列表转换为参数。
然后我们可以改变你的foo
功能看起来像这样
foo <- function(a = 1, b = 2, c = 3, d = 1, e = 2, f = 3, g = 4, h = 1) {
vars <- mget(ls())
print(vars$h)
call_match_args(bar, vars)
call_match_args(foobar, vars)
}
foo(1, 2, 3, 4,5, 6, 7, 8)
# [1] 8
# [1] 12
whilebar
和foobar
可以保持不变。该函数的第一步是获取所有参数值并将它们放在一个列表中。然后您从该列表访问它们并将该列表传递给call_match_args
辅助函数。
推荐阅读
- azure-data-factory - 通过 ADF 从 AAS 复制数据
- python - 有没有办法通过 django Web 应用程序(使用 python)连接到蓝牙设备?
- laravel - Laravel 自定义规则传递数组属性
- ios - 转义闭包捕获非转义参数“函数” Xcode 说
- java - 在 Spring Webflux 的请求正文中拒绝具有特定键值对的请求
- flask - 为用户创建角色时的问题
- python - 在 numpy 数组中查找数据类型
- google-compute-engine - Compute Engine 部署容器
- swiftui - SwiftUI iOS 15 键盘工具栏不显示文本字段
- tensorflow - 在 tf2.3 上训练的模型在 tf2.6 上表现不佳