Local package dependency to R data.table ":="

I have a local R package and some functions are operating with data.table operations. When I install package, it installs the data.table since it is a dependent. However, I couldn't figure out this part in the package.

This one is working in the function:

dt=data.table::as.data.table(dt)

so it converts dt to a data.table

This one is not working

dt[SuperDummyDate == '',SuperDummyDate := NA]

the package can't find that ":=" is a datatable function. since it is in [], I can't add data.table::..

I tried require(data.table) within the function, but still didn't work.

Thanks in advance!


Solution 1:

data.table should be imported in the NAMESPACE file of the package :

import(data.table)

With Roxygen, you could require this import in the function header, it will be automatically added to NAMESPACE:

#' Your function title & description
#'
#' @parameter data
#' @import data.table
#'
DTfunction <- function(data) {
  data[,newcol:=.SD[,1]]
}

Test after loading the function:

DTfunction(as.data.table(mtcars[,1:2]))

      mpg   cyl newcol
    <num> <num>  <num>
 1:  21.0     6   21.0
 2:  21.0     6   21.0
 3:  22.8     4   22.8
 4:  21.4     6   21.4
...