Using `names(obj) <- names(dt)` could be problematic
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- r
- Domain
- documentation
Research direction
Start by reproducing the supplied R example, then read the data.table Do's and Don'ts wiki and the Reference Semantics vignette, which are the documentation locations mentioned. Done means documenting the names-vector aliasing behavior and guidance for avoiding the shown unexpected errors in an appropriate location.
Written by the indexing model from the issue text.
Description
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)
dt
p <- list(1, 2)
names(p) <- names(dt)
p
dt[, 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?
- Dominant language
- R
- Stars
- 3.9k
- Forks
- 1.1k
- Avg merge
- 14h 4m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·