0

It appears that the order of loading packages matters when using Rscript with the sf library.

library(sf)
library(methods)

showMethods("coerce")

will not contain

from="sf", to="Spatial"
from="Spatial", to="sf"

in the output, and the function as(x, "Spatial) cannot be used

however

library(methods)
library(sf)

showMethods("coerce")

will contain the desired methods.

If using R console the problem does not occur as that loads the package methods by default.

Is this due to an error in the sf package?

It seems brittle and undesirable to have to library(methods) first in every script, and also this could not be the only situation where this is a problem. Is there a general way to avoid having order matter when loading R packages?

user2711915
  • 2,223
  • 1
  • 15
  • 17
  • 1
    It;s a FAQ, and surely a duplicate: `Rscript` does not load `methods` by default so you need to do it. (The alternative front-end [littler](http://dirk.eddelbuettel.com/code/littler.html) does,) So with `Rscript` you _must_ use your second approach. – Dirk Eddelbuettel Aug 24 '17 at 11:13
  • I am aware that Rscript does not load `methods` by default. That does not address the issues that: 1: it is undesirable for packages to silently fail to have desired behaviour and 2: it is not clear that this is the only situation where there is order dependent package loading behaviour. I am interested to see if there are other situations or packages where this is a problem. 3: Is the behaviour of the sf package here actually as desired, or could it be modified so that it does not have this order dependent loading issue? – user2711915 Aug 24 '17 at 11:23
  • Dude, it is one and the same issue: _In order to load `sf`_ (or any other S4-based package) _you need `methods`._ No more, no less. You have that in R itself, and in sane frontends like littler. What Rscript does here _never_ made sense but hey, it's R Core's call. – Dirk Eddelbuettel Aug 24 '17 at 11:29
  • Also use the search box above, eg with `[r] Rscript methods` to search for questions tagged for R containing Rscript and methods. – Dirk Eddelbuettel Aug 24 '17 at 11:34
  • The issue is that loading in the wrong order you get the vast majority of the sf package, and it _works_. The only functions I can find that _don't_ work are the two conversions listed in the post (and the equivalent ones for sfc). Is it good practice for the sf package to be built in such a way that is it 99% functional, but fails in some use cases because it doesn't automatically load a dependency? – user2711915 Aug 24 '17 at 11:34
  • You. Don't. Understand. `methods` needs to be present for `sf` to be loaded correctly. And _the vast majority of packages_ do not use S4 so it does not apply. But it *does* for every package using S4. Such as `sf`. End of thread for me. – Dirk Eddelbuettel Aug 24 '17 at 11:35
  • The `sf` package gives no error message if loaded without the `methods` package having been loaded. The `sf` package performs any tasks not requiring the listed functions. I hope you are agreeing with me that either the sf package is incorrectly not automatically loading a dependency, or it is incorrectly not reporting that it has not had a dependency loaded. – user2711915 Aug 24 '17 at 11:37
  • `as` is implemted using S4. See my previous posts, and sit patiently. The penny will drop eventually. – Dirk Eddelbuettel Aug 24 '17 at 11:51
  • 1
    Although I entirely agree with Dirk, I decided to move methods from Imports: to Depends: in pkg sf (like it is done in sp) so that this issue no longer comes up. The reason is not that it was brought up here, but that it was brought up independently three times over the last 10 days. – Edzer Pebesma Sep 05 '17 at 19:18
  • @Edzer Pebesma This is a satisfactory resolution for this specific issue. – user2711915 Sep 06 '17 at 13:01

0 Answers0