-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
Just adding that this is a general issue with 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 |
Code without |
I would like to see what vebose says, or even better str() of output from verbose. I am currently not with laptop so cannot check myself. |
Summarized in the issue:
c.f. jsub = substitute(foo(x), list(foo=sum))
jsub
# .Primitive("sum")(x)
jsub[[1L]]
# function (..., na.rm = FALSE) .Primitive("sum")
# [1] "builtin"
as.character(jsub[[1L]])
# Error in as.character(substitute(foo(x), list(foo = sum))[[1L]]) :
# cannot coerce type 'builtin' to vector of type 'character'
jsub[[1L]] == "sum"
# Error in jsub[[1]] == "sum" :
# comparison (==) is possible only for atomic and list types
# in the PR, we use deparse(), which _does_ give us a string representation without error:
deparse(jsub[[1L]])
# [1] ".Primitive(\"sum\")" |
What's wrong about? it is what user requested. |
That's fine, but DT=data.table(a=1:2, b=3:4)
eval(substitute(DT[, foo(b), by=a], list(foo=sum)))
# Error in as.character(jsub[[1L]]) :
# cannot coerce type 'builtin' to vector of type 'character' If
Also note that the provided PR has nothing to do with how |
Inconsistency is when someone will try to actually inject function body via passing function to env argument. Don't have reasonable use case for that but I don't think we can rule out such situation. For env it is actually sufficient to provide |
I think it's a good idea, I think supplying the call programmatically will be common, using I still think #6027 is needed given how opaque the error is when user approaches the problem incorrectly. |
So the error comes from getting the string for a column name (or verbose output), do I understand correctly? |
We have a couple places in |
got it. thanks. PR looks good |
The issue is that the resulting query, instead of being
DT[, sum(b), by=a]
, becomesDT[, .Primitive("sum")(b), by=a]
.The same will happen for any closure:
This is not a general issue with
[
:The text was updated successfully, but these errors were encountered: