data.table evaluation skips function scope when using parent.frame()
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with the [.data.table evaluation entry point and reproduce the minimal examples comparing parent.frame() with data.frame behavior. Determine whether the skipped function frame is intentional; if so, update the ?data.table documentation to describe evaluation frames, otherwise investigate the frame handling causing the discrepancy.
Written by the indexing model from the issue text.
Description
When evaluating expressions inside [.data.table, the expected scoping behavior seems to break. parent.frame() inside a data.table call does not point one frame up to the function’s environment as expected. Instead, it skips over the function frame and lands higher up the call stack. Note, that parent.frame defaults to parent.frame(n = 1).
Minimal example:
> DT <- data.table(g=1:10, reg=rep(5:8, length.out=10))
> DF <- as.data.frame(DT)
> reg <- 7
> (\() {reg <- 8; DF[DF$reg == get('reg', envir=environment()), ]})()
g reg
4 4 8
8 8 8
> (\() {reg <- 8; DT[reg == get('reg', envir=environment())][]})()
g reg
<int> <int>
1: 1 5
2: 2 6
3: 3 7
4: 4 8
5: 5 5
6: 6 6
7: 7 7
8: 8 8
9: 9 5
10: 10 6
> (\() {reg <- 8; DT[reg == get('reg', envir=parent.frame())][]})()
g reg
<int> <int>
1: 3 7
2: 7 7
Problem:
We expect parent.frame() to move one level up—the function’s environment—but inside [.data.table, it skips that frame, behaving differently than expected and differently from data.frame behavior.
Question:
Is this frame skipping intended? If yes, could this be documented more explicitly in ?data.table regarding evaluation frames?
Also see: related SO post. This arose as a follow-up problem to data.table issue #4958.
- 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 ·