Unusual assessment, confusion in advanced R book

So the Hadley advanced R book has an example of a problem using substitution, here is an excerpt from the code:

subset2 <- function(x, condition) {
condition_call <- substitute(condition)
r <- eval(condition_call, x, parent.frame())
x[r, ]
}

scramble <- function(x) x[sample(nrow(x)), ]

subscramble <- function(x, condition) {
 scramble(subset2(x, condition))
}


subscramble(sample_df, a >= 4)
# Error in eval(expr, envir, enclos) : object 'a' not found
traceback()
#> 5: eval(expr, envir, enclos)
#> 4: eval(condition_call, x, parent.frame()) at #3
#> 3: subset2(x, condition) at #1
#> 2: scramble(subset2(x, condition)) at #2
#> 1: subscramble(sample_df, a >= 4)

      

Do you see what the problem is? condition_call contains the condition of the expression. So when we evaluate condition_call, it also evaluates a condition that has the value a> = 4. However, this cannot be evaluated because there is no object called a in the parent environment. But, if they were installed in the global environment, even more confusing things could happen:

There are a few things that confuse me about the above paragraph from the book.

  • The "condition_call contains an expression condition" clause. The condition symbol is used as a formal argument in a subset of functions2 and is also used in a real argument in scrambling (subset2 (x, condition)). I think he was referring to this real / invoking "condition", right?

  • As a promise, a condition in a submatrix definition is lazy evaluated? Why is this not being evaluated when calling: scramble (subset2 (x, condition))

In other words, how do you know if a promise is being evaluated or not by looking at the code? For example, if I understand it correctly, if I changed the code to the following:

scramble(subset2(x,(condition))) 

      

the condition is now forcibly evaluated. What are the rules here?

  1. When Hadley says "when we evaluate condition_call, it also evaluates the condition", what is "it"? Did he mean that "eval" has caused some kind of internal or secondary evaluation that tries to resolve the promise "condition"? Where is this happening? That is, the environment that R is trying to use to find out what the meaning of "condition" is?

  2. So the "object not found" error was not caused by "x" or "parent.frame ()" in the call below, but rather somewhere else? I am completely confused.

    r <- eval (condition_call, x, parent.frame ())

+3


source to share


1 answer


I can't comment, so I'll post this as an answer. Everything will just be paraphrased:

Non-standard estimate from another function in R



Essentially what happens in the sample_df call environment, the function will look for "condition" instead of "a> = 4". Since it cannot find it, it moves up and then finds the condition in the calling environment; subframe environment (this is because subset2 (x, condition) is a promise created in that environment) where it finds> 4.

Now it needs to find a, but we have already left the sample_df data environment to look for it in the global environment, which leads to strange results if a is defined in the global environment.

+3


source







All Articles