pryr:::address
is defined as function(x) address2(check_name(substitute(x)), parent.frame())
. If you wrap pryr:::address
in another function its parent.frame()
changes. E.g.:
myfun = function() parent.frame()
myfunwrap = function() { print(environment()); myfun() }
myfun()
#<environment: R_GlobalEnv>
myfunwrap()
#<environment: 0x063725b4>
#<environment: 0x063725b4>
myfunwrap()
#<environment: 0x06367270>
#<environment: 0x06367270>
Specifically, unless I've lost it somewhere, pryr::address
seems to work like the following:
ff = inline::cfunction(sig = c(x = "vector", env = "environment"), body = '
Rprintf("%p\\n", findVar(install("x"), env));
return(eval(findVar(install("x"), env), env)); //pryr::address does not evaluate
') #this is not a general function; works only for variables named "x"
assign("x", "x from GlobalEnv", envir = .GlobalEnv)
ff1 = function(x) ff(x, parent.frame())
ff2 = function(x) ff1(x)
pryr::address(x)
#[1] "0x6fca100"
ff(x, .GlobalEnv)
#0x06fca100
#[1] "x from GlobalEnv"
ff1(x)
#0x06fca100
#[1] "x from GlobalEnv"
ff1(x)
#0x06fca100
#[1] "x from GlobalEnv"
ff2(x)
#0x06375340
#[1] "x from GlobalEnv"
ff2(x)
#0x0637480c
#[1] "x from GlobalEnv"
I'm not sure how that can be "fixed" (sometimes it might be wanted to behave like this) other than doing something like:
add = pryr::address
add(x)
#[1] "0x6fca100"