如何在 R 中创建 returns 具有固定参数的函数?
How to create a function that returns a function with fixed parameters in R?
我有一些带有很多参数的 R 函数 target
:
target = function(b1,b2,l1,l2,l3,o1,o2) return((b1+b2+l1+l2+l3+o1+o2)^2)
target
的一些参数应该保持固定(存储在命名向量fixed
中),一些参数应该是可变的(它们的名字存储在向量variable
中):
fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5)
variable = c("o2","b2")
现在,我想用输入 fixed
和 variable
编写一个函数 f
returns 我是(未执行的)函数 target
fixed
中的参数固定为 fixed
.
中的值
我目前的成绩:
f = function(fixed, variable){
### create the new "target" function with fixed parameters
target_new = function() {}
### create the arguments
formals(target_new) = setNames(rep(list(bquote()), length(variable)), variable)
### create the body
body(target_new) = call("target",fixed,variable)
### return the new "target" function with fixed parameters
return(target_new)
}
我创建正文失败。它应该与 do.call
、call
、substitute
或 deparse
的组合一起使用 - 有人知道怎么做吗?
f(fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5), variable = c("o2","b2"))
的期望输出是:
function (o2, b2)
target(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5, o2 = o2, b2 = b2)
编辑
通过
定义正文
### create the body
arguments = c(paste(variable,"=",variable), paste(names(fixed),"=",fixed))
body(f) = call("target",arguments)
产量
function (o2, b2)
target(c("b1 = 1", "l1 = 2", "l2 = 3", "l3 = 4", "o1 = 5", "o2 = o2", "b2 = b2"))
这几乎是所需的输出(引号和 c()
除外)。
您可以使用 do.call
并将 as.symbol
分配给 变量。
target <- function(b1,b2,l1,l2,l3,o1,o2) return((b1+b2+l1+l2+l3+o1+o2)^2)
fixed <- c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5)
variable <- c("o2","b2")
f <- function(fixed, variable) {
target_new <- function() {}
formals(target_new) <- setNames(rep(list(bquote()), length(variable)), variable)
for(i in variable) assign(i, as.symbol(i))
body(target_new) <- do.call("call", unlist(list("target", as.list(fixed), mget(variable))))
target_new
}
f(fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5), variable = c("o2","b2"))
#function (o2, b2)
#target(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5, o2 = o2, b2 = b2)
#<environment: 0x564b81f2ded8>
f(fixed, variable)(3, 4)
#[1] 484
我有一些带有很多参数的 R 函数 target
:
target = function(b1,b2,l1,l2,l3,o1,o2) return((b1+b2+l1+l2+l3+o1+o2)^2)
target
的一些参数应该保持固定(存储在命名向量fixed
中),一些参数应该是可变的(它们的名字存储在向量variable
中):
fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5)
variable = c("o2","b2")
现在,我想用输入 fixed
和 variable
编写一个函数 f
returns 我是(未执行的)函数 target
fixed
中的参数固定为 fixed
.
我目前的成绩:
f = function(fixed, variable){
### create the new "target" function with fixed parameters
target_new = function() {}
### create the arguments
formals(target_new) = setNames(rep(list(bquote()), length(variable)), variable)
### create the body
body(target_new) = call("target",fixed,variable)
### return the new "target" function with fixed parameters
return(target_new)
}
我创建正文失败。它应该与 do.call
、call
、substitute
或 deparse
的组合一起使用 - 有人知道怎么做吗?
f(fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5), variable = c("o2","b2"))
的期望输出是:
function (o2, b2)
target(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5, o2 = o2, b2 = b2)
编辑
通过
定义正文### create the body
arguments = c(paste(variable,"=",variable), paste(names(fixed),"=",fixed))
body(f) = call("target",arguments)
产量
function (o2, b2)
target(c("b1 = 1", "l1 = 2", "l2 = 3", "l3 = 4", "o1 = 5", "o2 = o2", "b2 = b2"))
这几乎是所需的输出(引号和 c()
除外)。
您可以使用 do.call
并将 as.symbol
分配给 变量。
target <- function(b1,b2,l1,l2,l3,o1,o2) return((b1+b2+l1+l2+l3+o1+o2)^2)
fixed <- c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5)
variable <- c("o2","b2")
f <- function(fixed, variable) {
target_new <- function() {}
formals(target_new) <- setNames(rep(list(bquote()), length(variable)), variable)
for(i in variable) assign(i, as.symbol(i))
body(target_new) <- do.call("call", unlist(list("target", as.list(fixed), mget(variable))))
target_new
}
f(fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5), variable = c("o2","b2"))
#function (o2, b2)
#target(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5, o2 = o2, b2 = b2)
#<environment: 0x564b81f2ded8>
f(fixed, variable)(3, 4)
#[1] 484