Changes inside a function sometimes leak outside, sometimes not
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with the reproducible R example in the issue and compare the behavior of f1 and f2 when setDT() is called inside each function. Review linked pull request #4978 for the existing work. Done should establish and implement a consistent, clearly documented rule for whether these by-reference changes affect the external data.frame.
Written by the indexing model from the issue text.
Description
Hello
After much work I was able to locate my problem to a small example:
library(data.table)
dat <- data.frame(col1=c(1,2), col2=c(3,4))
f1 <- function(d) { setDT(d); d[TRUE, col2:=c(5,6)] }
f2 <- function(d) { setDT(d); d[col1<3, col2:=c(5,6)] }
f1(dat)
dat # not changed:
# col1 col2
# 1: 1 3
# 2: 2 4
f2(dat)
dat # changed:
# col1 col2
# 1: 1 5
# 2: 2 6
I read that data.table operations are by reference, but not sure what that means when a data.frame becomes a data.table inside a function. Are changes to this new born data.table supposed to apply to the external copy or not?
Either way I hope one of those can be done in all cases, otherwise it is very confusing.
Thank you very much for your good package!
# Output of sessionInfo()
sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.14.2
loaded via a namespace (and not attached):
[1] compiler_4.1.2 tools_4.1.2
- 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 ·