Skip to content
Prev 21248 / 63424 Next

foo2Args()

On 6/30/2006 4:19 PM, Paul Gilbert wrote:
I don't think it really works, it's just a coincidence that the answer 
matches your expectations:

 > foo(3)
[[1]]
[1] 1

[[2]]
[1] 2

Or maybe I am completely misunderstanding your expectations...
This is a somewhat subtle thing about the way args are evaluated.  What 
is done makes lots of sense once you understand it:

  - When you specify an argument in the call, the expression you give is 
evaluated in the current context.  In the context where you made this 
call, foo2Args is not defined, hence the error.

  - When you specify a default for an argument, it is evaluated in the 
local context of the function evaluation.  So as a default, foo2Args is 
recognized, because it's a local variable within foo.

You also need to remember "lazy evaluation":  neither of the above 
actually take place until args is used.

R is pretty flexible, so it's probably possible to do whatever you 
intended here; can you describe exactly what you want to happen?

Duncan Murdoch