Scoping / Documentation / Context issues -- How to be specific?
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 55/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- r
- Domain
- documentation
Research direction
Start with the Introduction to data.table vignette and compare its scoping guidance with the data.table programming vignette's env interface examples. Clarify how callers pass column names and outer values in i, including the character-value cases described here; done when the introductory documentation gives a correct, usable note and links to the fuller programming guidance.
Written by the indexing model from the issue text.
Description
Hello,
I have been trying to find a good documentation on how to avoid potential scoping issues.
For instance, consider
library("data.table")
dt = data.table(a = 1:3, b = 4:6)
dt[a %in% 1:3]
So far so good, a is interpreted within the dt. But what if we set a = 4:6?
Documentation often talks about the . and .. calls, but those are specific to j and not i part of data.table.
a = 4:6
dt[a %in% a]
obviously wouldn't have desired effect.
But the Introduction to data.table doesn't really offer a solution.
https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html
Most stackoverflow or AI answers are incorrect, often they will say to use the get() solution, but it doesn't work either.
The solution is introduced in https://cran.r-project.org/web/packages/data.table/vignettes/datatable-programming.html, use the env = list(...), such as:
dt[col %in% value, env = list(col = "a", value = a)
Yet, in some cases, wrapping in I is also required if the value is of character, because it would be interpreted as a column.
dt[] = lapply(dt, as.character)
a = "1"
dt[col %in% value, env = list(col = "a", value = a))] # fails because "1" is not a column
dt[col %in% value, env = list(col = "a", value = I(a))] # works
dt[col %in% value, env = list(col = as.name("a"), value = I(a))] # safest?
All these things are imo required when he user wants to be specific, like this variable names a column in a data.table, while this_one names a variable comming from outer environment. This way wires won't be crossed.
This should IMO be in the "introduction to data.table" as a simple case or much more complex "Programming on data.table`
Apparently, env is a new interface and get(), mget() etc. were at one point interfaces of data.table but were discontinued (likely because they were buggy).
https://stackoverflow.com/a/54800108
tl,dr: Add a note in "Introduction to data.table" about the env interface and how to pass column names/values as variables.
- 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 ·