Creating a new variable with get (data.table) immediately after loading the data.table object

I am trying to create a new variable in a data table that I generated, saved and loaded again. Once loaded, I address data.table indirectly via get () and it doesn't work for generating a new variable unless I directly bind it to create a variable before. Could it be some kind of environmental problem?

# Generate data.table
t<-data.table(x=c(1,2,3,4))
tStr<-"t"
names(t)

# Generate Variable a -> ok
get(tStr)[, a:=1]
names(t)

# Generate Variable b -> ok
t[, b:=1]
names(t)

# Save
save(t, file="test.Robj")
load("test.Robj", .GlobalEnv)

# Generate Variable c -> fails 
get(tStr)[, c:=1] 
names(t)

# Generate Variable d -> ok
t[, d:=1]
names(t)

# Generate Variable e -> ok again !?
get(tStr)[, e:=1]
names(t)

      

thanks for the help

+3


source to share


1 answer


This is because important metadata does not survive storage actions:

> t<-data.table(x=c(1,2,3,4))
> attr(t, ".internal.selfref")
<pointer: 0x0000000000100788>
> save(t, file="test.Robj")
> load("test.Robj", .GlobalEnv)
> attr(t, ".internal.selfref")
<pointer: (nil)>
> t[, d:=1]
> attr(t, ".internal.selfref")
<pointer: 0x0000000000100788>

      

Notice how you lose the memory pointer. I'm not sure if this is such a big mistake as an inherent conflict between what is data.table

and what does save

. It seems that in order for this to work properly, we'll need a special method load

that reassigns the internal pointer when objects are loaded data.table

.

In this case, using change by reference looks like resetting the pointer.

EDIT : As a workaround in your use case, you can try:

t <- data.table(x=c(1,2,3,4))
save(t, file="test.Robj")
load("test.Robj", .GlobalEnv)
assign("t", get("t")[, c:=3])
t

      

works as expected:



   x c
1: 1 3
2: 2 3
3: 3 3
4: 4 3

      

Also note that expectation:

get("t")[, c:=3]

      

will work, slightly expecting that:

get("x") <- 5

      

will work. data.table

may add this functionality in the future, but you are in this dark area where the referential nature is data.table

really starting to contradict the semantics of R.

+4


source







All Articles