Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a function in env= fails #6026

Open
MichaelChirico opened this issue Mar 28, 2024 · 2 comments · May be fixed by #6027
Open

Using a function in env= fails #6026

MichaelChirico opened this issue Mar 28, 2024 · 2 comments · May be fixed by #6027
Assignees
Labels
bug programming parameterizing queries: get, mget, eval, env

Comments

@MichaelChirico
Copy link
Member

DT=data.table(a=1:2, b=3:4)
DT[, f(b), by=a, env=list(f=sum)]
# Error in as.character(jsub[[1L]]) : 
#   cannot coerce type 'builtin' to vector of type 'character'

The issue is that the resulting query, instead of being DT[, sum(b), by=a], becomes DT[, .Primitive("sum")(b), by=a].

The same will happen for any closure:

DT[, f(b), by=a, env=list(f=\(x) sum(x))]
# Error in as.character(jsub[[1L]]) : 
#   cannot coerce type 'closure' to vector of type 'character'

This is not a general issue with [:

DT[, .Primitive("sum")(b), by=a]
#        a    V1
#    <int> <int>
# 1:     1     3
# 2:     2     4
@MichaelChirico MichaelChirico added bug programming parameterizing queries: get, mget, eval, env labels Mar 28, 2024
@MichaelChirico
Copy link
Member Author

Just adding that this is a general issue with [, but it's a bit hard to construct examples AFAICT, which is why it hasn't come up. E.g.

DT = data.table(a = 1:3)
eval(substitute(DT[, foo(a)], list(foo = sum)))
# Error in as.character(jsub[[1L]]) : 
#   cannot coerce type 'builtin' to vector of type 'character'

Directly writing .Primitive("sum")(b) as above is not quite the same -- AIUI it's confusing because .Primitive("sum") is just the deparse() output / print string representation of the LHS.

@jangorecki
Copy link
Member

Code without eval call would give better picture what's happening, like verbose via env var

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug programming parameterizing queries: get, mget, eval, env
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants