回复 第3楼 的 yanlinlin82:原问题:
Exercises
1.The following two calls look the same, but are actually different:
<br />
(a <- call("mean", 1:10))<br />
#> mean(1:10)<br />
(b <- call("mean", quote(1:10)))<br />
#> mean(1:10)<br />
identical(a, b)<br />
#> [1] FALSE
What’s the difference? Which one should you prefer?
这部分的书上的介绍:
Creating a call from its components
To create a new call from its components, you can use call() or as.call(). The first argument to call() is a string which gives a function name. The other arguments are expressions that represent the arguments of the call.
<br />
call(":", 1, 10)<br />
#> 1:10<br />
call("mean", quote(1:10), na.rm = TRUE)<br />
#> mean(1:10, na.rm = TRUE)
as.call() is a minor variant of call() that takes a single list as input. The first element is a name or call. The subsequent elements are the arguments.
<br />
as.call(list(quote(mean), quote(1:10)))<br />
#> mean(1:10)<br />
as.call(list(quote(adder(10)), 20))<br />
#> adder(10)(20)
</p>
所以为了解答这个练习,我想只能从The other arguments are expressions that represent the arguments of the call.这句话入手了吧,但是我又奇怪为什么第一个call是可以的。。。 1:10不是一个expression啊
PS:There are four possible components of an expression: constants, names, calls and pairlists.
constants are length one atomic vectors, like "a" or 10. ast() displays them as is.