Skip to content

groupingsets() wrong scoping logic #5560

@sindribaldur

Description

@sindribaldur
library(data.table)
irisDT <- data.table(iris)

foo = function(w) {
  bv = "Species"
  groupingsets(
    irisDT, 
    j  = lapply(.SD, \(y) sum(y > w)),
    by = bv,
    sets = as.list(bv),
    .SDcols = c("Sepal.Length", "Petal.Length")
  ) |> print()
  irisDT[, lapply(.SD, \(y) sum(y > w)), .SDcols = c("Sepal.Length", "Petal.Length"), by=bv]
}
foo(5)
# Error in ..FUN1(Sepal.Length) : object 'w' not found

w = 4
foo(5)
#       Species Sepal.Length Petal.Length
#        <fctr>        <int>        <int>
# 1:     setosa           50            0
# 2: versicolor           50           34
# 3:  virginica           50           50
#       Species Sepal.Length Petal.Length
#        <fctr>        <int>        <int>
# 1:     setosa           22            0
# 2: versicolor           47            1
# 3:  virginica           49           41

I'm using data.table ‘1.14.7’ (just updated with data.table::update_dev_pkg()) with R 4.2.1 on Windows 10.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions