7

I'm working on an R package that uses the spTransform function in the sp package. The rub is that this function need rgdal loaded to work, or I get an error message:

Error in eval(expr, envir, enclos) : load package rgdal for spTransform methods

My Imports statement in the DESCRIPTION file includes the following:

Imports: sp,
    rgdal

But I'm still getting the error. However, if I explcitly load rgdal (using library(rgdal)) before using the package, everything works fine. I'm guessing that when my package is loaded rgdal is not attached because none of my code uses it explcitly via :: etc.

So I think my question is: How can I make my package attach a package that I am not explicitly using?

Jesse Anderson
  • 4,507
  • 26
  • 36
  • 2
    This has come up in the r-dev mailing list. I think you need to also use the correct specifications in your NAMESPACE file. – IRTFM Apr 30 '15 at 21:53
  • Can you show what your NAMESPACE file looks like? – BrodieG Apr 30 '15 at 22:15
  • It should probably be included in Imports if the package won't work without it and needs to use the namespace. I'm hesitant to say Depends but possibly that too/instead – Rich Scriven Apr 30 '15 at 22:20

1 Answers1

7

As said by BondedDust, you need to import the required packages into your package NAMESPACE. To do so edit the file, adding a new line import(sp, rgdal). Further reading http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Specifying-imports-and-exports

  • I'd recommend not editing `NAMESPACE` by hand though - `roxygen2` http://cran.rstudio.com/web/packages/roxygen2/ can make life easier – sckott May 01 '15 at 06:01
  • So, following this answer and http://stackoverflow.com/questions/8597993/does-roxygen2-automatically-write-namespace-directives-for-imports-packages I added an `#' @import rgdal` directive above the function that uses `spTransform` then ran `devtools::document()`. This correctly added the `import(rgdal)` call to `NAMESPACE` – Jesse Anderson May 01 '15 at 14:48