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

Aperta
#4,878 6 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
r
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
R
Stelle
3.9k
Fork
1.1k
Merge medio
14h 4m
PR unite (30g)
4

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Rdatatable/data.table

Tutte le issue di Rdatatable/data.table

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.