Following up on this question, I built my own package, which makes use of data.table.
Inside the package, I use data.table to calculate means per column in a matrix according to another column.
Something in the lines of this:
datam <- cbind(matrix(rexp(100), 10), IN=c(rep(1,2), rep(2,3), rep(3,2), rep(4,1), rep(5,2)))
dd <- data.table::data.table(datam)
result <- dd[, lapply(.SD, mean), by=IN]
The only way I get it to work is including data.table in the Depends: field of my DESCRIPTION file AND import(data.table) in the NAMESPACE file.
Now I want to be able to call the function from my package that uses data.table like this:
mypackage::myfunction(...)
However, I get the following error:
Error in lapply(.SD, mean) : object '.SD' not found
This error only goes away if I call my function after loading the package with library(), like this:
library(mypackage)
myfunction(...)
Since my package would be subsequently called from other packages, is there a way that I can make it work without having to use library() everytime, like I do for every other package I need a function from?
Thanks
EDIT
I have just made a MWE package that reproduces the error. Please download from the Google Drive link below:
https://drive.google.com/open?id=1yHxmQeoIOx9VIuL4EBrFWlGDBstnKJQs
I used the usethis package to build it, in the usethis_myexample.R file.
The package itself is called myexample and is contained in the myexample-package folder. Inside there you can see the DESCRIPTION file contains data.table in the Imports section, and the NAMESPACE file contains import(data.table).
There is only one function named aggregate_mean in the functions.R file inside the R folder.
Next to the myexample-package folder, there is a tests folder with a test file named mytest.R to run the aggregate_mean function like this:
mymat <- cbind(matrix(rexp(100), 10), IN=c(rep(1,2), rep(2,3), rep(3,2), rep(4,1), rep(5,2)))
mymat
mynewmat <- myexample::aggregate_mean(mymat, "IN")
mynewmat
I always encounter the error:
Error in lapply(.SD, mean) : object '.SD' not found
Thanks!
 
     
    