for example:
method1 <- function(alpha,beta,ff)
Does R allow this ???
What I am try to achieve is that: I have a general method, method1. And I have other methods: f2,f3,f4. I want it to be like method1 <- function(a,b, ff), where a & b are constants and most importantly, ff can be either f2 or f3 or f4, depending on how i call the function on the console. Ideally f2, f3, f4 computes a matrix.
Using the example in your answer below, I was wondering why cant I have this instead??
f1 <- function(a,b,ff(a,b))
{
solve(ff(a,b))
}
f2 <- function (x,y){
Diag(x*y)
}
This is a really bad example. But I would like to know why cant I include the ff(a,b) in the argument ??? What is the logic of writing ?
f1 <- function(a,b,ff){
ff(a,b)
}
Depends what you mean. You can pass a function as an argument to another function, and you can make the default argument a function:
You can pass the results of a function call:
But you can’t do exactly what you specified: arguments must be named according to the standard variable-naming conventions (see
?make.names).You can do it by using backticks, or by assigning
names(formals(f))[3] <- "f2(1,3)", but that’s dangerous and probably not what you wanted.If you explain a little more of the context of what you’re trying to do you might get a more meaningful answer …
edit: taking a guess based on your description, you want the first thing I described above.