1

I have a behaviout that makes no sense to me, could someone explain the following bahaviour

I have created 2 packages A,B

A depends on data.table and defines the function readFile

B depends on A and defines the function useFile

useFile <- function(path){
  DT <- readFile(path)
  print(class(DT))
  return(DT[x == 'a'])
}

When I do

library(A)
library(B)
useFile(myFile)

I see that DT is a data.table from the print statement but it crashes saying that object x not found (of course the data.table has a x column)

Now when I also make B depends on data.table it works fine !

Can someone explain the behaviour, I would assume that all the data.tableness should be there when library(A) is called !!

PS: I know I should supply reproducible but we are taking 2 packages here, if there is no obvious answer I will upload 2 packages online

statquant
  • 13,672
  • 21
  • 91
  • 162
  • Package data.table should usually be in Imports in the DESCRIPTION file and be imported in the NAMESPACE of your first package. You should then either import your first package into your second package or import data.table there too. If I'm uptodate, depending on packages in contrast to importing them is usually not recommended anymore. – Roland Sep 01 '16 at 11:40
  • See http://stackoverflow.com/questions/10527072/using-data-table-package-inside-my-own-package Does your error state `Error in ``[.data.frame``(x, i, j) : object 'x not found` – mnel Sep 01 '16 at 11:43
  • @mnel: not this message but that's the pb yes, though I use Rstudio and roxygen2 to do all for me. I guess data.table NEEDS to be in imports in my case then ? – statquant Sep 01 '16 at 11:45
  • 2
    Most importantly, you must import data.table into the package's namespace. I think you can do that indirectly, i.e. import data.table's namespace into package1's namespace and then package1's namespace into package2's namespace, but I have never tried that. With roxygen2 you can just do `@import data.table`. – Roland Sep 01 '16 at 12:13
  • please provide NAMESPACE files of both packages, the part related to data.table – jangorecki Sep 01 '16 at 13:01
  • 1
    Possible [duplicate](http://stackoverflow.com/questions/10527072/using-data-table-package-inside-my-own-package) – Christoph Sep 01 '16 at 15:17

1 Answers1

0

To be sure, add Depends: data.table in DESCRIPTION to all packages that use data.table even if other packages they use already use data.table

statquant
  • 13,672
  • 21
  • 91
  • 162