r - Access object by address / pointer -
can access data.table object created in current r session memory address or pointer?
library(data.table) dt <- data.table(a = 1:10, b = letters[1:10]) address(dt) # [1] "0x6bf9b90" attr(dt,".internal.selfref",true) # <pointer: 0x2655cc8>
this of silly way of doing (as compared how can cast pointers in e.g. c++), do:
# recursively iterate on environments find.by.address = function(addr, env = .globalenv) { idx = which(sapply(ls(env), function(x) address(get(x, env = env))) == addr) if (length(idx) != 0) return (get(ls(env)[idx], env = env)) # didn't find it, let's iterate on other environments idx = which(sapply(ls(env), function(x) is.environment(get(x, env = env)))) (i in idx) { res = find.by.address(addr, get(ls(env)[i], env = env)) if (res != "couldn't find it") return (res) } return ("couldn't find it") } dt = data.table(a = 1) e = new.env() e$dt = data.table(b = 2) e$f = new.env() e$f$dt = data.table(c = 2) find.by.address(address(dt)) # #1: 1 find.by.address(address(e$dt)) # b #1: 2 find.by.address(address(e$f$dt)) # c #1: 2
Comments
Post a Comment