You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
asynchronous code in j using grouping variable #3563
In an expression using bydata.table provides the grouping variable in j. When iterating over the groups this value is changed by reference. This leads to problems when performing asynchronous operations using the grouping variable in j:
Below is a very simple instance of this problem. (it doesn't even need to be asynchronous)
library(data.table)
x <- list()
data.table(i=1:3,letter=letters[1:3])[, {x[[i]] <<- letter; NULL}, by=letter]
#> Empty data.table (0 rows and 1 cols): letter
x
#> [[1]]
#> [1] "c"
#>
#> [[2]]
#> [1] "c"
#>
#> [[3]]
#> [1] "c"
I understand that this might be hard to fix efficiently until reference counting becomes default for R, but I favor correctness over efficiency. 😇 (.GRP, .N, .SD and .BY are equally affected btw)
I wonder whether it would be worth at least warning the user somewhere in the documentation that erroneous results may be produced in computations that depend on a variable that is implicitly modified by reference between grouping operations. It isn't obvious that computations such as these can interact between levels of the grouping by column. This can be remedied by using the copy() trick, but the documentation (reference semantics vignette and copy() help page) really only warns about unexpected modifications due to :=, set*, and changing names(DT). The phenomena here and in the other examples in #3563 (comment) seem conceptually different from those mentioned in the documentation, as these appear to result from implementation-level modifications by reference that may not be known to the user.
Activity
jangorecki commentedon May 14, 2019
This is probably a same problem as #3120
Atrebas commentedon May 14, 2019
See also https://stackoverflow.com/questions/51057600/rmarkdown-data-table-plots-dont-match-after-compilation.
Using the
copy()trick works on the example above.jan-glx commentedon May 17, 2019
Oh yes, apparently related issues are hard to find but very abundant:
On SO:
https://stackoverflow.com/questions/51057600/rmarkdown-data-table-plots-dont-match-after-compilation
https://stackoverflow.com/questions/42449257/saving-plots-in-a-data-table-list-column
https://stackoverflow.com/questions/43536583/data-table-not-returning-correct-plots
https://stackoverflow.com/questions/28400446/plotting-by-group-in-data-table
https://stackoverflow.com/questions/36413514/strange-error-plotting-by-group
https://stackoverflow.com/questions/27505794/values-of-the-wrong-group-are-used-when-using-plot-within-a-data-table-in-rs
and here:
#3120
#2050
#1524
Below is a very simple instance of this problem. (it doesn't even need to be asynchronous)
I understand that this might be hard to fix efficiently until reference counting becomes default for R, but I favor correctness over efficiency. 😇 (.GRP, .N, .SD and .BY are equally affected btw)
ToeKneeFan commentedon Jun 7, 2019
I wonder whether it would be worth at least warning the user somewhere in the documentation that erroneous results may be produced in computations that depend on a variable that is implicitly modified by reference between grouping operations. It isn't obvious that computations such as these can interact between levels of the grouping
bycolumn. This can be remedied by using thecopy()trick, but the documentation (reference semantics vignette andcopy()help page) really only warns about unexpected modifications due to:=,set*, and changingnames(DT). The phenomena here and in the other examples in #3563 (comment) seem conceptually different from those mentioned in the documentation, as these appear to result from implementation-level modifications by reference that may not be known to the user.jangorecki commentedon Apr 6, 2020
closing as duplicate of #507