這是有些做(相比於如何在如C++投三分球)的一個愚蠢的方式,但你可以這樣做:
# recursively iterate over 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 over the other environments
idx = which(sapply(ls(env), function(x) is.environment(get(x, env = env))))
for (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))
# a
#1: 1
find.by.address(address(e$DT))
# b
#1: 2
find.by.address(address(e$f$DT))
# c
#1: 2
讓我們假設一個時刻,你可以 - 你會如何使用它? – eddi
@eddi我想結合data.table中的'read.only'屬性,並且能夠通過將* address/pointer *維護到只讀數據從我的會話中的任何環境重新查詢任何data.table對象。表。主要用於交互式使用。 – jangorecki