r - How is the evaluation environment retained after a promise has been turned into a call? -
in following minimal working example, a_promise turned a_call using substitute(). can see a_promise object store both expression , environment expression should evaluated.
however, far can tell, once it's turned a_call, thing a_call stores expression (condition) no reference environment in expression can evaluated , turned a >= 4.
however, error @ end indicates eval(a_call, df, parent.frame() try object a, meaning a_call indeed translated a >= 4. how?
library(pryr) f <- function(df, a_promise) { print(promise_info(a_promise)) #> shows both expression , eval envir a_call <- substitute(a_promise) print(a_call) #> condition eval(a_call, df, parent.frame()) } g <- function(df, condition) { f(df, condition) } df <- data.frame(a=1:5, b=5:1) g(df, >= 4) #> error in eval(expr, envir, enclos) : object 'a' not found
when do
a_call <- substitute(a_promise) you getting expression passed a_promise condition. class of a_call symbol (not call or expression). symbol pointer promise in parent frame hasn't been resolved yet. time you're @ f, you've missed chance turn a >= 4 expression in usual substitute way. now, when ask value of condition evaluating previous promise , getting non-sensical result. comes down difference between
eval( >= 4, df) # error in eval(a >= 4, df) : object 'a' not found and
eval( expression(a >= 4), df) # [1] false false false true true. when condition resolving it's promise, it's not doing inside eval().
Comments
Post a Comment