You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using names(obj) <- names(dt) could be problematic #5079
Since set* and := set by reference, it seems to alter the names() character vector by reference too. Then the assignment like names(obj) <- names(dt) might lead to unexpected error.
Consider the following example:
library(data.table)
dt<- data.table(x=1:10, y=10:1)
dtp<-list(1, 2)
names(p) <- names(dt)
pdt[, z:=1:10]
p
names(p)
p<-list(1, 2, 3)
names(p) <- names(dt)
p
setcolorder(dt, c("z", "y", "x"))
p
> library(data.table)
> dt <- data.table(x = 1:10, y = 10:1)
> dt
x y
<int> <int>
1: 1 10
2: 2 9
3: 3 8
4: 4 7
5: 5 6
6: 6 5
7: 7 4
8: 8 3
9: 9 2
10: 10 1
> p <- list(1, 2)
> names(p) <- names(dt)
> p
$x
[1] 1
$y
[1] 2
> dt[, z := 1:10]
'names' attribute [3] must be the same length as the vector [2]
> p
$x
[1] 1
$y
[1] 2
'names' attribute [3] must be the same length as the vector [2]
> names(p)
[1] "x" "y" "z"
'names' attribute [3] must be the same length as the vector [2]
> p <- list(1, 2, 3)
> names(p) <- names(dt)
> p
$x
[1] 1
$y
[1] 2
$z
[1] 3
> setcolorder(dt, c("z", "y", "x"))
> p
$z
[1] 1
$y
[1] 2
$x
[1] 3
I don't see a "don't do this" in wiki and the only thing about this is in the vignette Reference Semantics.
Should we somehow emphasize such problems more somewhere?
I have the same issue.
It's not convenient that this object stays reference further even in the tibble/data.frame stack.
This is a small sample with a crash.
Would it actually be terrible to have names and names<- always copy? I've only ever seen the current behaviour to lead to confusion without any real upside. There'd be some unnecessary allocations in existing code but the impact should be negligible?
Glad I'm not nuts or doing something dumb. Just noticed this yesterday, and I'm sure I've done it before. Has this always been the case? Going to have to go through all my code now.
It's somewhat disturbing that the reference semantics are completely opaque. I.e. if I do str(names(DT)) the thing I get back appears to be a plain R character vector, with no indication that it has special reference semantics. Unlike say a classed object of data.table which is known to have this properties. Perhaps the names should have a special class?
Activity
iMissile commentedon Aug 19, 2021
I have the same issue.
It's not convenient that this object stays reference further even in the
tibble/data.framestack.This is a small sample with a crash.
Created on 2021-08-19 by the reprex package (v2.0.1)
tlapak commentedon Sep 2, 2021
Would it actually be terrible to have
namesandnames<-always copy? I've only ever seen the current behaviour to lead to confusion without any real upside. There'd be some unnecessary allocations in existing code but the impact should be negligible?wesnm commentedon Sep 21, 2021
Glad I'm not nuts or doing something dumb. Just noticed this yesterday, and I'm sure I've done it before. Has this always been the case? Going to have to go through all my code now.
MilesMcBain commentedon Aug 30, 2022
Encountered this today, highly surprising.
It's somewhat disturbing that the reference semantics are completely opaque. I.e. if I do
str(names(DT))the thing I get back appears to be a plain R character vector, with no indication that it has special reference semantics. Unlike say a classed object ofdata.tablewhich is known to have this properties. Perhaps the names should have a special class?