do.call(what, args, quote = FALSE, envir = parent.frame())
what | the function to call. Can be specified as either a function object or a character string, which is then found in the environment specified by envir, and from which the function object is produced. |
args | a list of the arguments to the called function. The names attribute of args gives the argument names. |
quote | a logical flag. If FALSE (the default), each element of args is evaluated in envir when the function first reads the argument. If TRUE, each element of args is conceptually wrapped in a call to quote, so the value is not evaluated again when the function first reads the argument. |
envir | the environment used to evaluate the function. This environment is also used to look up the function if it is specified as a character string,and to evaluate the elements of args (if quote is FALSE). |
x <- function(x,y) x+y do.call("x",list(1,2)) do.call(x,list(1,2)) do.call(function(x,y)x+y,list(1,2)) # all above return 3A <- 11 B <- 22 do.call(paste, list(as.name('A'), as.name('B')), quote=TRUE) # returns "A B" do.call(paste, list(as.name('A'), as.name('B')), quote=FALSE) # returns "11 22"
A <- 11 f <- function(x) x+100 # define global "f" and "A" (in global env) myenv <- new.env() assign("A", 22, envir=myenv) assign("f", function(x) x+200, envir=myenv) # define local "f" and "A" (in myenv)
do.call(f,list(A),envir=myenv) # returns 111 (use global "f", "A")
do.call("f",list(A),envir=myenv) # returns 211 (evaluate "f" in myenv)
do.call(f,list(quote(A)),envir=myenv) # returns 122 (evaluate "A" in myenv)
do.call("f",list(quote(A)),envir=myenv) # returns 222 (evaluate "f", "A" in myenv)