1

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!

DaniCee
  • 2,397
  • 6
  • 36
  • 59
  • you can `import` `.SD` from `data.table` – MichaelChirico Apr 09 '20 at 11:20
  • 2
    https://cran.r-project.org/web/packages/data.table/vignettes/datatable-importing.html – MichaelChirico Apr 09 '20 at 11:21
  • Let me try... but I already do `import(data.table)`, it should import all the `data.table` functions... I shouldn't have to do `importFrom(data.table, .SD)` on top of that right? – DaniCee Apr 09 '20 at 11:25
  • no, but I understood from your question you prefer not to do import(data.table) and Depends: data.table – MichaelChirico Apr 09 '20 at 11:28
  • No no, I just need to call my function as `mypackage::myfunction()` without the need to do `library()` first... apparently using `Depends:` is discouraged – DaniCee Apr 09 '20 at 11:30
  • The problem is, if I don't use `Depends:` (like they suggest in the link you provide), it never works, whether I do `library()` or not, I always get the error – DaniCee Apr 09 '20 at 11:37
  • could you provide a link to your package? or make example in question reproducible? – jangorecki Apr 09 '20 at 12:41
  • 1
    it should be sufficient to use `import(data.table)` in `NAMESPACE` file and `Imports: data.table` in `DESCRIPTION` file – jangorecki Apr 09 '20 at 12:42
  • If I only do that, it doesn't work weather I use `library()` or not... I don't have a MWE, let me work on one and post it – DaniCee Apr 10 '20 at 04:11
  • Alright, I made an example package that reproduces the error. Please see my edit in the question for the link as well – DaniCee Apr 10 '20 at 06:35

2 Answers2

4

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!

jangorecki
  • 16,384
  • 4
  • 79
  • 160
  • Oh really? using my package as is you didn't get the error? I built and installed with RStudio, does it make a difference? – DaniCee Apr 12 '20 at 10:28
  • @DaniCee yes it makes a difference. RStudio is an interface to R. – jangorecki Apr 12 '20 at 10:34
  • Is it discouraged then to build a package with Rstudio when data.table is used? – DaniCee Apr 12 '20 at 10:43
  • 2
    No idea. I do use RStudio toghether with data.table, but nowadays I don't use it to build packages, but editing code only. In the past I had multiple different issues when building packages, not related to data.table, and it wasted enough of my time to not try that again. – jangorecki Apr 12 '20 at 10:55
  • If you would like to seek more support on your issue, you can try to contact RStudio support or RStudio community website. – jangorecki Apr 12 '20 at 11:02
  • But you would still need RStudio to create the `.Rproj` file right? – DaniCee Apr 12 '20 at 17:01
  • 1
    I don't use .Rproj files, they are not part of R, they are not being used by R in any way. They are used only by RStudio (or probably some rstudio packages like usethis, devtools, etc.). – jangorecki Apr 12 '20 at 19:30
  • But you used my package with the `.Rproj` file inside and didn't remove it right? Fun thing is if I only install through RStudio I get the error, and if I only use your commands from scratch, I also get the error. It only works if I do a combination of the 2... So running your 2 commands starting from my package as I uploaded it – DaniCee Apr 13 '20 at 02:38
  • It is possible that .Rproj files are automatically excluded from build via rules defined in `.Rbuildignore`. If the file will remain inside the package after build, it is basically ignored, eventually with a note/warning during package check. – jangorecki Apr 14 '20 at 10:55
  • yes, but I still had to build it with RStudio first, then with the R CMD commands in the terminal... if I just do those commands from scratch, it doesn't work (I get the same error). It only works with a combination of RStudio built and the commands you posted (those 2 approaches individually yield the same error) – DaniCee Apr 16 '20 at 08:17
  • Why do you think you need RStudio to build it first? if mean to create Rd manual files then you can use roxygen directly, from R console as well. – jangorecki Apr 16 '20 at 10:44
  • Cause if I only use the commands you posted from scratch on my scripts (without building with RStudio first) I still get the error. That's why I'm asking – DaniCee Apr 16 '20 at 12:13
  • It only works when I build with Rstudio first, then use the commands above. Not if I do either of the things alone – DaniCee Apr 16 '20 at 12:13
  • When you are using only R console and not RStudio, what error you are getting? or at which point it stucks? – jangorecki Apr 16 '20 at 13:07
0

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.

Daniel Hoop
  • 652
  • 1
  • 5
  • 16