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

dcast.data.table eval(fun.aggragate) -- cannot pass as an argument #713

Closed
mathematicalcoffee opened this issue Jul 3, 2014 · 5 comments
Assignees
Labels

Comments

@mathematicalcoffee
Copy link

Referred from SO question

Affects 1.9.3 (and not 1.9.2)

TL;DR

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.

@arunsrinivasan
Copy link
Member

@mathematicalcoffee, thank you very much. Will fix asap.

@arunsrinivasan arunsrinivasan added this to the v1.9.4 milestone Jul 3, 2014
@arunsrinivasan arunsrinivasan self-assigned this Jul 3, 2014
@mathematicalcoffee
Copy link
Author

Definitely a bug, because

fun.aggregate=length
f(t, last)
   id a b c
1:  1 1 1 1
2:  2 1 1 2

returns what I would expect to get from f(t, length).

@Horizon-cmchugh
Copy link

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
Copy link
Member

@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
Copy link

Horizon-cmchugh commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants