When column name is name of variable get(variable) generates an error and eval(variable) returnes the incorrect values

Open
#4,878 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

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

programming

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.