使用空环境开始新的 R 会话。编写一系列带有参数的函数,该参数将用作times
调用中的参数值rep()
。
f <- function(n) {
rep("hello", times = n)
}
f(x)
有人期望这会失败,实际上有人会得到:
# Error in f(x) : object 'x' not found
稍微修改一下函数:
f2 <- function(n) {
ls.str()
rep("hello", times = n)
}
f2(x)
正如预期的那样,它仍然失败:
# Error in f2(x) : object 'x' not found
再修改一点(在控制台中查看环境):
f3 <- function(n) {
print(ls.str())
rep("hello", times = n)
}
f3(x)
我仍然期待失败,而是得到:
## n : <missing>
## [1] "hello"
就好像调用print()
使 rep 工作好像times
被设置为 1。