At the moment, at least, this is an exercise in learning for me, so the actual functions or their complexity is not a problem. Suppose I write a function whose argument list contains some input variables and the name of the function passed as a string. This function then calculates some variables inside and "decides" how to pass them the name of the function in which I passed.
For non-primitive functions, I can do (for this example, suppose that not my funcname functions have any arguments other than (x,y,z) . If they did, I would have to write code to search for names(formals(get(funcname))) so as not to remove other arguments):
foo <- function (a,b,funcname) { x <- 2*a y <- a+3*b z <- -b formals(get(funcname)) <- list(x=x, y=y, z=z) bar <- get(funcname)() return(bar) }
And the best part is, even if the funcname function is executed without errors, even if it does not use x , y or z (as long as there are no other arguments, t have default values).
The problem with the "primitive" functions is that I do not know how to find or change their formats. Besides writing a wrapper like foosin <-function(x) sin(x) , is there a way to set my foo function to work with both primitive and non-primitive function names as input arguments?
source share