0

I'm developing my first package - well, actually I'm packaging some existing code - in RStudio. I've marked up all the comments with roxygen2 and this has generated a correct NAMESPACE file which includes:

import(sp)
importFrom(geosphere,distMeeus)
importFrom(geosphere,distm)

I'm using the distm() function from the geosphere package to calculate a distance matrix but it is failing:

Error in .pointsToMatrix(x) : could not find function "is.projected" 4 .pointsToMatrix(x) 3 distm(OCC, SPAD, fun = distMeeus) at peta.R#79

now is.projected() is in the sp package, which I have imported so it should be in my search path. Shouldn't it?

As an experiment I just called library(geosphere) (which also loads sp) prior to calling my package code and my function got past that point. Soooo, do I still need to call library() for every package??? That seems a bit redundant and at this point I think I'll stop and see if someone can set me straight before I break anything further...

www
  • 38,575
  • 12
  • 48
  • 84
Michael Henry
  • 599
  • 2
  • 4
  • 17
  • 1
    Have you added `IMPORTS: sp` to your `DESCRIPTION` file? – Andrie Aug 12 '14 at 12:38
  • Hi @Andrie the DESCRIPTION file does have "Imports: ... sp, geosphere" – Michael Henry Aug 12 '14 at 23:35
  • Moving `sp` from `Imports` to `Depends` works. I've been reading [Writing R Extensions](http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-Dependencies) but I can't actually figure out why this might be the case. I have a feeling it's to do with the difference between loading a package vs. attaching it, but the penny hasn't yet dropped for me. – Michael Henry Aug 13 '14 at 04:52
  • Try the following: Add `methods` to your Imports, i.e. `Imports: methods` as well as to your NAMESPACE, i.e. `import(methods)`. I know this sounds random, but if it works I'll do my best to explain why. – Andrie Aug 13 '14 at 07:16
  • @Andrie Umm, no actually it didn't work. Bizarre that it worked for me yesterday after I added `methods` to `DESCRIPTION` and `NAMESPACE`. Maybe sp was still `attach`ed??? (Even though the R session is restarted after every `Build & Reload`.) I tried to run it today after the computer's been shut down and got the same error. So I've had to put `sp` back in `Depends:` (which mirrors the configuration of `geosphere`). – Michael Henry Aug 14 '14 at 10:54
  • Do you have this package on github (or other public repo) so we can take a look? – Andrie Aug 14 '14 at 11:04
  • Unfortunately not. Currently working with our legal team to get it released under an open source licence. If you like I can code up a "minimum working example" as it seems to be quite reproducible. I think the fact that `geosphere` has `sp` in `Depends` is a big clue as to what's going on. I've e-mailed that package maintainer so hopefully he'll be able to shed some light... – Michael Henry Aug 14 '14 at 11:34
  • Hi. I think there is a general problem here. I've seen the same (or similar) issue in a different set of packages. If you are prepared to make a minimal example and put it on github, I promise to add a bounty to this question to attract some attention. – Andrie Aug 14 '14 at 13:04

1 Answers1

1

Edited

I don't understand why this is, but you need to add methods to your package imports:

  • Add to your DESCRIPTION:

    Imports: methods
    
  • Add to your NAMESPACE:

    import(methods)
    
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • I downloaded the source package for geosphere and had a poke around. There is no `is.projected()` defined in that package, although that function is called six times. Looking at the `DESCRIPTION` I found: `Depends: sp, R (>= 2.10.0) Suggests: methods, raster` The `NAMESPACE` for that package contains: `importFrom("sp", SpatialPolygons, SpatialLines)` – Michael Henry Aug 13 '14 at 10:14
  • @MichaelHenry Thank you for checking. Let me remove this bit of my answer. – Andrie Aug 13 '14 at 10:19