You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
dcast.data.table eval(fun.aggragate) -- cannot pass as an argument #713
If you have a function that calls dcast.data.table, you can't seem to pass fun.aggregate into that call from the function argument.
Because of an eval(fun.aggregate) within dcast.data.table.
Example
I have a table like this:
library(data.table)
t <- data.table(id=rep(1:2, c(3,4)), k=c(rep(letters[1:3], 2), 'c'), v=1:7)
t
id k v
1: 1 a 1
2: 1 b 2
3: 1 c 3
4: 2 a 4
5: 2 b 5
6: 2 c 6
7: 2 c 7 # note the duplicate (2, c)
I reshape to long format, retaining the last occurence of duplicates
dcast.data.table(t, id ~ k, value.var='v', fun.aggregate=last) # last is in data.table
id a b c
1: 1 1 2 3
2: 2 4 5 7
However if I wrap my dcast.data.table call into a function:
f <- function (tbl, fun.aggregate) {
dcast.data.table(tbl, id ~ k, value.var='v', fun.aggregate=fun.aggregate)
}
f(t, last)
Error in `[.data.table`(data, , eval(fun.aggregate), by = c(ff_)) :
could not find function "fun.aggregate"
It looks like the symbol fun.aggregate is being evaluated (eval(fun.aggregate)) and not found (since the function "fun.aggregate" does not exist).
Is there a way to "protect" a function argument from the eval() call when I pass it in (i.e. perhaps this behaviour is intended), or perhaps this is a bug. My personal opinion is that it is a bug, because this works for dcast and not dcast.data.table, and the two should be consistent.
@MichaelChirico I have. I made a Stack Overflow post a few weeks ago, which was added as a bug by one of the moderators here, but closed as a duplicate of #1369. Oh well.
Activity
arunsrinivasan commentedon Jul 3, 2014
@mathematicalcoffee, thank you very much. Will fix asap.
mathematicalcoffee commentedon Jul 3, 2014
Definitely a bug, because
returns what I would expect to get from
f(t, length).Better fix for #713; GForce is deployed if possible.
Horizon-cmchugh commentedon Apr 12, 2017
Was this every fixed in the CRAN data.table package? I am still having this issue of a passed in function argument failing in eval.
MichaelChirico commentedon Apr 12, 2017
@Horizon-cmchugh considering it was closed almost 3 years ago, this issue is on CRAN. Have you looked at #1369, which is still open?
Horizon-cmchugh commentedon Apr 12, 2017
@MichaelChirico I have. I made a Stack Overflow post a few weeks ago, which was added as a bug by one of the moderators here, but closed as a duplicate of #1369. Oh well.