I have some R-function target with a lot of parameters:
target = function(b1,b2,l1,l2,l3,o1,o2) return((b1+b2+l1+l2+l3+o1+o2)^2)
Some parameters of target should be kept fixed (stored in the named vector fixed) and some parameters should be variable (their names are stored in the vector variable):
fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5)
variable = c("o2","b2")
Now, I want to write a function f with the inputs fixed and variable that returns me the (not executed) function target with the parameters in fixed fixed at the values in fixed.
My results so far:
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)
}
I fail to create the body. It should work with a combination of do.call, call, substitute or deparse - does anyone know how?
The desired output of f(fixed = c(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5), variable = c("o2","b2")) is:
function (o2, b2)
target(b1 = 1, l1 = 2, l2 = 3, l3 = 4, o1 = 5, o2 = o2, b2 = b2)
EDIT
Defining the body via
### create the body
arguments = c(paste(variable,"=",variable), paste(names(fixed),"=",fixed))
body(f) = call("target",arguments)
yields
function (o2, b2)
target(c("b1 = 1", "l1 = 2", "l2 = 3", "l3 = 4", "o1 = 5", "o2 = o2", "b2 = b2"))
which is almost the desired output (except for the quotation marks and the c()).