I'm writing my own R package and would like to plot a spatialPolygonsDataFrame
object. If I were writing it as a script I would simply load the necessary packages (maptools
, rgdal
, and rgeos
) with library()
and plot with plot(x)
.
When writing a package to build using library()
is not advised, instead it is usual to load the package by adding it to Imports:
in the NAMESPACE
. If I do this I receive the following error:
Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'
This is is corrected by loading the maptools
package with library()
if writing a script.
I know you can load individual methods with ImportMethodsFrom
in the NAMESPACE
so have tried to import a plot method from maptools
using this approach but have had no luck. When I looked in the NAMESPACE
of the maptools
package I couldn't find a plot method exported. I've seen there is a plot.Spatial
function which I have tried to import to my NAMESPACE
without success:
No methods found in "maptools" for requests: plot.Spatial
Finally, I have tried adding maptools
to Depends:
instead of Imports:
in my NAMESPACE
and this does work. Is this the canonical way to do this? It seems overkill to attach a whole package for one method (plus I don't know what functions have been masked, etc.). What is the best way to load the necessary tools to plot maps within a self-authored function?
Edit 1: In response to @Hack-R's question, I don't know if plot.Spatial is the only method I need, or even if it's the correct one. It's my educated guess that this will enable me to plot spatial
objects.