When column name is name of variable get(variable) generates an error and eval(variable) returnes the incorrect values
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the supplied data.table examples using get() and eval(), then trace how column names and external variables are resolved in those entry points. Add regression coverage for the shown cases and verify that variable_name's stored value is used consistently without breaking ordinary column evaluation.
Written by the indexing model from the issue text.
Description
Not sure if this is an intended consequence of how data.table was designed or not. The issue is that if I have a variable "variable_name" set to a value and I want data.table to reference that value I would typically use get(variable_name) or eval(variable_name) to operate on the value stored in the variable. However, if the variable "variable_name" is a name of a column in the data.table, then data.table throws an error.
I think it would be ideal to have data.table use the stored value in the variable when called by get() or eval().
xx <- data.table::data.table(A = seq(1,100,1), B = seq(101,200,1))
# error
A <- "B"
xx[, sum(get(A))]
# Error in get(A) : invalid first argument
xx[, get(A)]
# Error in get(A) : invalid first argument
xx[, get(eval(A))]
# Error in get(eval(A)) : invalid first argument
xx[, eval(A)]
# [1] 1 2 3 4 5 6 7 8 9 10 11 ... (values from column name A instead of column name B)
packageVersion("data.table")
[1] ‘1.13.2’
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.3 tools_4.0.3
- 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 ·