Skip to content
Prev 21251 / 63424 Next

foo2Args()

On 6/30/2006 5:12 PM, Paul Gilbert wrote:
I think this would work:

  foo <- function(x, args=foo2Args()) {
    foo2Args <- function(a=1, b=2){list(a,b)}
    # above would actual do more testing of args
    #now I would call foo2 with args, but to test just
    args <- eval(substitute(args))
    args
    }

The explanation is this:

substitute(args) gets the expression attached to args without evaluating 
it. eval( substitute(args) ) then evaluates it; since I didn't say which 
environment to work in, it defaults to the current evaluation environment.

HOWEVER, this function is a bad idea.  Users don't expect evaluation 
like this.  For example, this would mess it up:

args <- 2
foo(1, args=foo2Args(a=args, b=10))

because it would try to evaluate "args" in the local environment, rather 
than where the user put it.  You'd get weird behaviour if any of the 
values in foo2Args used variables rather than constants, because they'd 
be evaluated in the function, not in the user's workspace.

Duncan Murdoch