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!
As there is an reproducible exaple now in your question, I was able to dig into it.
I downloaded zip file from your link, unzip it, renamed myexample-package
to mypackage
. Then...
R CMD build myexample
R CMD INSTALL myexample_0.0.0.9000.tar.gz
R -q
then in R.
mymat <- cbind(matrix(rexp(100), 10), IN=c(rep(1,2), rep(2,3), rep(3,2), rep(4,1), rep(5,2)))
mymat
# [1,] 0.83010264 0.4778802 1.15826121 0.304299143 0.5781483 1.81660550
# [2,] 0.03895798 2.3709480 0.69694839 0.730800823 0.3319984 0.53348461
# [3,] 0.03383199 0.2842029 1.74151827 1.019573035 0.1863635 0.89487309
# [4,] 0.53533254 0.2814782 0.78563371 0.309180422 1.4393098 1.07450638
# [5,] 0.53010142 1.3132409 0.67072292 1.212244007 0.1984360 0.06208641
# [6,] 0.45916016 0.5576434 0.67770401 0.056270575 0.5065829 0.83416626
# [7,] 0.25404953 0.2730706 0.01070633 0.132406274 1.6186573 0.37083771
# [8,] 3.42254715 0.6489950 0.81291881 0.003048744 1.3522848 0.18066361
# [9,] 1.29994319 0.3170614 1.71145805 1.141222719 1.1832478 0.18091907
#[10,] 0.23622615 0.4473482 3.07774816 1.441207092 0.9761152 0.28132707
# IN
# [1,] 6.1868517 2.44880203 0.55261438 0.3459453 1
# [2,] 0.8177218 0.90554629 1.00106158 1.0427756 1
# [3,] 4.3910329 0.56068780 0.24262243 1.7036666 2
# [4,] 0.8712083 0.02439399 0.80927766 1.6596570 2
# [5,] 0.6613734 0.12954737 1.01661648 1.2446795 2
# [6,] 0.2858442 2.32610958 0.26553789 0.4856818 3
# [7,] 3.6628536 0.26447698 0.70633274 2.0283399 3
# [8,] 0.0515666 0.99916985 0.06102821 0.9413485 4
# [9,] 4.7781407 1.47764414 1.92598562 0.4581395 5
#[10,] 0.8770661 2.78552022 0.07543095 0.6886183 5
mynewmat <- myexample::aggregate_mean(mymat, "IN")
mynewmat
# get V1 V2 V3 V4 V5 V6 V7
#1: 1 0.4345303 1.4244141 0.9276048 0.517549983 0.4550734 1.1750451 3.5022868
#2: 2 0.3664220 0.6263073 1.0659583 0.846999155 0.6080364 0.6771553 1.9745382
#3: 3 0.3566048 0.4153570 0.3442052 0.094338425 1.0626201 0.6025020 1.9743489
#4: 4 3.4225471 0.6489950 0.8129188 0.003048744 1.3522848 0.1806636 0.0515666
#5: 5 0.7680847 0.3822048 2.3946031 1.291214905 1.0796815 0.2311231 2.8276034
# V8 V9 V10 IN
#1: 1.6771742 0.77683798 0.6943604 1
#2: 0.2382097 0.68950553 1.5360010 2
#3: 1.2952933 0.48593531 1.2570109 3
#4: 0.9991699 0.06102821 0.9413485 4
#5: 2.1315822 1.00070829 0.5733789 5
So I am not able to reproduce your problem. I encourage you to follow the same steps as described above, to narrow down, if the issue lies somewhere in the way how you install your package. If you have more followup question, rather than editing question, best to put them in comments under my answer.
Hope that helps!
Add Imports: data.table
to your package description and declare .datatable.aware <- TRUE
somewhere in an R-File of your package.
That solved the problem for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.