4

I'm trying to write a simple wrapper around data.table for split-apply-combine use (like aggregate, ddply etc.) in order to gain from data.table's speed but not use its data structures or syntax. I have tried the following.

fold = function(df, by, ...)
{
    library(data.table)
    dt = data.table(df)
    dt = dt[, eval(substitute(list(...))), by]
    df = as.data.frame(dt)
    return(df)
}

a = data.frame(x=c(1,1,1,2,2,2), y=runif(6))
b = fold(a, "x", y_min=min(y), y_max=max(y))
print(a)
print(b)

This works fine when I put it in a script and source it, but when I put that function in a package, install it and try to use it, I get

Error in eval(expr, envir, enclos) : object 'y' not found
Calls: fold -> [ -> [.data.table -> [.data.frame -> eval -> eval

What am I doing wrong, apart from trying this in general?

otsaw
  • 1,034
  • 8
  • 13
  • Not sure, but maybe worth checking [**this post**](http://stackoverflow.com/questions/14837902/how-to-write-a-function-that-calls-a-function-that-calls-data-table) – Arun Mar 05 '13 at 12:21
  • Do you have `data.table` in your `Depends:` list? – Andrie Mar 05 '13 at 12:26
  • 1
    @Andrie: No, I didn't and it does work once added. Why doesn't `Suggests:` and loading `data.table` in the function body work here? – otsaw Mar 05 '13 at 13:08
  • 2
    @otsaw Good question. Because at the top of most `data.table` functions is a call to `data.table:::cedta` (Calling Environment Data.Table Aware). That needs you specifically Depend, Import, or, create `.datatable.aware=TRUE`. See http://stackoverflow.com/a/10529888/403310 – Matt Dowle Mar 05 '13 at 13:25
  • You might also want to talk to @Hadley, who is/was working on something like this, too. – Matt Dowle Mar 05 '13 at 13:32

1 Answers1

1

Following comments, FAQ 6.9 answered this one :

FAQ 6.9 I have created a package that depends on data.table. How do I ensure my package is data.table-aware so that inheritance from data.frame works?
You don't need to do anything special. Just include data.table in either the Imports: or Depends: field of your package's DESCRIPTION file.

Some technical detail of how inheritance from data.frame maintains full compatibility with [.data.frame is here :

Using data.table package inside my own package

Community
  • 1
  • 1
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224