Updated based on some feedback from MichaelChirico and comments by Arun and Soheil.
Roughly speaking, there's two approaches you might consider. The first is building the dependency into your package itself, while the second is including lines in your R code that test for the presence of data.table
(and possibly even install it automatically if it is not found).
The data.table
FAQ specifically addresses this in 6.9, and states that you can ensure that data.table
is appropriately loaded by your package by:
Either i) include data.table in the Depends: field of your DESCRIPTION file, or ii) include data.table in the Imports: field of your DESCRIPTION file AND import(data.table) in your NAMESPACE file.
As noted in the comments, this is common R behavior that is in numerous packages.
An alternative approach is to create specific lines of code which test for and import the required packages as part of your code. This is, I would contend, not the ideal solution given the elegance of using the option provided above. However, it is technically possible.
A simple way of doing this would be to use either require
or library
to check for the existence of data.table
, with an error thrown if it could not be attached. You could even use a simple set of conditional statements to run install.packages
to install what you need if loading them fails.
Yihui Xie (of knitr fame) has a great post about the difference between library
and require
here and makes a strong case for just using library
in cases where the package is absolutely essential for the upcoming code.