Address Function R

I use a function address()in a package pryrin R and wonder if the following result should be expected ...

x <- 1:10
add <- function(obj){address(obj)}
address(x)
# [1] "0x112d007b0"
add(x)
# [1] "0x11505c580"

i.e. 0x112d007b0! = 0x11505c580

I was hoping they would be the same. Is there a way to tune the function addabove to make sure it gets the same value? those. get address in parent environment?

+4
source share
1 answer

pryr:::addressdefined as function(x) address2(check_name(substitute(x)), parent.frame()). If you wrap pryr:::addressin another function, it parent.frame()will change. For example:.

myfun = function() parent.frame()
myfunwrap = function() { print(environment()); myfun() }
myfun()
#<environment: R_GlobalEnv>
myfunwrap()
#<environment: 0x063725b4>
#<environment: 0x063725b4>
myfunwrap()
#<environment: 0x06367270>
#<environment: 0x06367270>  

In particular, if I have not lost it somewhere, it pryr::addressworks as follows:

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"

, "" ( ), - :

add = pryr::address
add(x)
#[1] "0x6fca100"
+2

Source: https://habr.com/ru/post/1612811/


All Articles