Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R address function

Tags:

r

pryr

I am using the address() function in the pryr package in R, and was wondering if this following result is to 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 that they were going to be the same value...is there a way to get adjust the function add above to ensure it does get the same value? i.e. get the address in the parent environment?

like image 243
h.l.m Avatar asked Oct 19 '22 23:10

h.l.m


1 Answers

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"
like image 85
alexis_laz Avatar answered Nov 03 '22 06:11

alexis_laz