Closed edzer closed 4 years ago
Thanks @edzer and sorry for answering such a long time after you opened the issue!
With which version of sf
did you try this?
If I try on my side (with sf 0.7-2,
plot(polygons, add = TRUE)
throws a warning, not an error. Also, no polygon is drawn. So I guess the issue might be with the plot.sf
method? If I only do
plot(polygons)
I get the same warning but the polygon is actually drawn. What do you think?
I think these errors can be triggers when you set environment variables _R_CHECK_LENGTH_1_LOGIC2_=package:_R_CHECK_PACKAGE_NAME_,abort,verbose
, or e.g. try with winbuilder.
Actually there's something strange. If I don't load sf
, the output of concaveman
is of class data.frame
(where I would expect sf - data.frame
). If I load sf
before running concaveman
, the output is of class sf
...
reprex?
library(concaveman)
class(points)
#> [1] "sf" "data.frame"
polygon <- concaveman(points)
class(polygon)
#> [1] "data.frame"
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
polygon <- concaveman(points)
class(polygon)
#> [1] "sf" "data.frame"
Created on 2019-01-10 by the reprex package (v0.2.1)
That is because the summarise.sf
method is only picked up (i.e., used) when sf
is loaded. Without, you'll go through summarise.data.frame
, provided by dplyr
, which won't create sf
objects.
Right. But I can't force the call to summarise.sf, can I ?
Your package needs to import it (can't see it in the NAMESPACE).
OK. But I wanted to avoid importing sf
if it is not needed, and my understanding was that for S3 methods I didn't need to. See what Hadley says:
S3 generics are just functions, so the same rules for functions apply. S3 methods always accompany the generic, so as long as you can access the generic (either implicitly or explicitly), the methods will also be available. In other words, you don’t need to do anything special for S3 methods. As long as you’ve imported the generic, all the methods will also be available.
Am I getting it wrong?
But how would your package then spontaneously jump to sf::summarise.sf without even knowing it exists?
You can leave it in Suggests: but then need to do sth like this:
if (!requireNamespace("sf", quietly = TRUE))
stop("sf required: install that first") # nocov
sf::summarise( ...)
so that your code imports the sf namespace on the fly, when needed.
BTW I've made sf a strong dependency to avoid this issue.
With:
If I later on do something with
points
, things run fine. Maybe loadsf
before running the example? Let me know if you need help.