Quoting from the good manual:
4.3.3 Argument evaluation
One of the most important things to know about the evaluation of
arguments to a function is that supplied arguments and default
arguments are treated differently. The supplied arguments to a
function are evaluated in the evaluation frame of the calling
function. The default arguments to a function are evaluated in the
evaluation frame of the function.
要看看这在实践中意味着什么,创建一个函数,其中一个参数的默认值是另一个参数的值的函数:
f
y
}
当用y的默认值调用时,R看起来在函数调用的评估框架中评估y,即在整个函数的整体被评估的同一个环境中 – 一个x已经好得多的地方确实存在:
f()
# [1] 16
当用提供的值y调用时,R在调用函数的评估框(在这里是全局环境)中查找,找不到x,并让它在其错误信息中知道:
f(y=x^2)
# Error in f(y = x^2) : object 'x' not found