-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Right now, r-devel is implementing a native pipe that is incompatible with chaining in data.table. Using the proposed d => syntax I get an error using this R version
library(data.table)
data <- CJ(group1 = letters[1:5], group2 = letters[10:14])
data[, x := rnorm(.N)] |>
d => d[, mean(x), by = group1]
#> Error: function '[' not supported in RHS call of a pipeIf "[" won't be supported as the RHS of a pipe, then no syntax transformation can make data.table compatible with the |> pipe, if I understand correctly.
I see two ways of addressing this.
On the one side, we could talk with r-core to present this issue and see if they can change their implementation to make it work.
Another option would be to create a functional alias to data.table:::"[.data.table", lets say dt(). This, for example, seems to work.
dt <- data.table:::"[.data.table"
data[, x := rnorm(.N)] |>
dt(, mean(x), by = group1)
#> group1 V1
#> 1: a 0.58876302
#> 2: b -0.24705765
#> 3: c 0.07676786
#> 4: d -0.33047608
#> 5: e 0.54832829(in fact, it also works with dt <- base::"[")
Personally, I don't mind that notation at all. It is true that if such a simple fix resolves the issue, then each user could do it in their own scripts, but I think it might be preferable if data.table provided a standardised alias so that other people's code stays readable. For the record, I don't like dt very much, it's just the first thing that came in mind.