Skip to content

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

Closed

Description

@mathematicalcoffee

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.

Activity

  1. arunsrinivasan commented on Jul 3, 2014

    @arunsrinivasan
    Member

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

  2. added this to the v1.9.4 milestone on Jul 3, 2014
  3. mathematicalcoffee commented on Jul 3, 2014

    @mathematicalcoffee
    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).

  4. removed this from the v1.9.4 milestone on Jul 3, 2014
  5. added a commit that references this issue on Aug 7, 2014
  6. Horizon-cmchugh commented on Apr 12, 2017

    @Horizon-cmchugh

    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.

  7. MichaelChirico commented on Apr 12, 2017

    @MichaelChirico
    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?

  8. Horizon-cmchugh commented on Apr 12, 2017

    @Horizon-cmchugh

    @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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions