Cannot access columns of DT within a function called in j

Open
#2,276 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the dt[, f("x")] example with the supplied f function, then compare it with the grouped dt[, .(y = f("x")), by = z] case. Determine whether functions called from j can access available columns through dynamic scoping; done means both examples can resolve x as requested.

Written by the indexing model from the issue text.

Description

programming

Suppose the following function is defined:

f <- function(x) {
  get(x, parent.frame())
}
library(data.table)
dt <- data.table(x = 1:10, y = 10:1, z = rep(1:2, 5))
dt[, f("x")]

Although f does its job to work under dynamic scoping, dt[, f("x")] still results in an error because x is not used directly so that it is not exported to j's environment.

I'm wondering if there's a way to make function like f work so that it can access available variables in j as if those variables were explicitly written, especially for case like dt[, .(y = f("x")), by = z]?

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.