[R] A question about call()
super
desolator88 at 163.com
Thu Jul 24 16:55:44 CEST 2014
Thanks a lot, it is much clear to me now, but i still have a question:<br/>The raw question is from:<br/>Hadley wickham's book advanced r programming, Chapter Meta programming, section expressions, in the part "Creating a call from its components"<br/>He said:<br/>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/>And He also said in the same section expressions before:<br/>There are four possible components of an expression: constants, names, calls and pairlists.<br/>constants are length one atomic vectors, like "a" or 10. ast() displays them as is<br/>So, i can't figure out are 1:10 and rnorm(10) both the expressions? I am confused, or 1:10 is a expression but it is evaled immediately so the expression actually represent it's value? <br/>I hope you can read the section expressions.
At 2014-07-24 07:15:55, "Duncan Murdoch" <murdoch.duncan at gmail.com> wrote:
>On 24/07/2014, 2:41 AM, super wrote:
>> The question is as below:
>> Exercises
>> 1.The following two calls look the same, but are actually different:
>> (a <- call("mean", 1:10))
>> #> mean(1:10)
>
>This one creates a call where the first argument is a vector containing
>10 elements.
>
>> (b <- call("mean", quote(1:10)))
>> #> mean(1:10)
>
>This one creates a call where the first argument is a call to the ":"
>function to produce a sequence.
>
>> identical(a, b)
>> #> [1] FALSE
>> What¡¯s the difference? Which one should you prefer?
>> So, how i can figure out this question?
>
>In this case they deparse the same, but in other cases they wouldn't, e.g.
>
>call("mean", rnorm(10))
>
>appears quite different from
>
>call("mean", quote(rnorm(10)))
>
>The difference is when the evaluation takes place. Which should you
>prefer? That's up to you.
>
>Duncan Murdoch
>
More information about the R-help
mailing list