Search code examples
rdata.tableroxygen2

How to import data.table indexing functionality?


What is the proper way to import the data.table indexing functionality A[ , , by = ...] into my package?

Should I have my roxygen docs contain:

importFrom data.table [.data.table


Solution

  • Looks like you need to import just data.table function and [ method will be correctly dispatched.

    subdt/R/mysubdt.R

    mysubdt = function(x) {
        stopifnot(class(x)[1L]=="data.table", c("s","g","v") %in% names(x), is.logical(x[["s"]]))
        x[s==TRUE, sum(v), by=g]
    }
    

    subdt/NAMESPACE

    importFrom(data.table, data.table)
    export(mysubdt)
    

    After build and install package (you don't need :: if you load data.table too, code assumes you didn't):

    library(subdt)
    x = data.table::data.table(
        s = c(TRUE,TRUE,FALSE,TRUE),
        g = c("a","b","a","b"),
        v = 1:4/10
    )
    mysubdt(x)
    #   g  V1
    #1: a 0.1
    #2: b 0.6