How to tell what method is being used by a function call when `methods` fails?
Partial answer: the method is sp:::plot.SpatialPolygons
. I have no idea why those commas are showing up in the results of methods(plot)
; plot,SpatialPolygons,missing-method
makes no sense to me (and I see it too).
I found the answer by causing an error. I don't have your shape file, so I took the example from help("SpatialPolygonsDataFrame-class")
, which I got to from ?SpatialPolygons"
. The example is below:
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
plot(SpP, col = 1:3, pbg="white")
I then changed the plot call to give an error:
plot(SpP, col = kasjdhfkjasdfhkas, pbg = "white")
# Error in plot.SpatialPolygons(x, ...) : object 'kasjdhfkjasdfhkas' not found
(should work unless you have kasjdhfkjasdfhkas
defined in your workspace as a vector). Sure enough,
sp:::plot.SpatialPolygons
showed the code for the method.
To answer a couple of questions raised by Gregor. The methods
function did not formerly display results for S4-methods, but now it does. Doing a search in the NEWS document I see this was added with version 3.2.0:
methods() reports S4 in addition to S3 methods; output is simplified when the class
argument is used. .S3methods() and methods::.S4methods() report S3 and S4 methods separately.
SpatialPolygons are S4 objects, and so have slots and S4-methods that are dispatched. You can display an S4 method with:
showMethods(f='plot', classes='SpatialPolygons', includeDefs=TRUE)
# ---- result ---
Function: plot (package graphics)
x="SpatialPolygons", y="missing"
function (x, y, ...)
plot.SpatialPolygons(x, ...)
That tells you that there is an S4 function for that class. You can execute either sp:::SpatialPolygons
or getAnywhere(plot.SpatialPolygons)
to see the function code. (Often that call using includeDefs=TRUE
would display the R-code, but not here.) The result for methods(plot)
when package 'sp' is loaded tell you that there are 8 different plot S4 methods registered in the workspace. The items following the commas are the "signatures" that are used for function dispatch. "missing-method" refers to situations where the second argument is not specified, wherein the missing
function executed inside a function body would return TRUE
:
[1] plot,ANY,ANY-method plot,color,ANY-method
[3] plot,Spatial,missing-method plot,SpatialGrid,missing-method
[5] plot,SpatialLines,missing-method plot,SpatialPixels,missing-method
[7] plot,SpatialPoints,missing-method plot,SpatialPolygons,missing-method
This will show you what then gets used to dispatch a plot
-call when the the object passed to it does not match any of the S4 methods:
showMethods(f='plot', classes='ANY', includeDefs=TRUE)
Function: plot (package graphics)
x="ANY", y="ANY"
function (x, y, ...)
UseMethod("plot")
x="color", y="ANY"
function (x, y, ...)
{
.local <- function (x, y, pch = 20, cex = 3)
pairs(coords(x), col = hex(x, fix = TRUE), pch = pch, cex = cex)
.local(x, y, ...)
}
Basically the R interpreter first checks to see if any S4 methods are appropriate, and failing that, will start going through the S3 methods until it gets to plot.default
.
> getMethod(f='plot', signature=c(x='SpatialPolygons', y='missing'))
Method Definition:
function (x, y, ...)
plot.SpatialPolygons(x, ...)
<environment: namespace:sp>
Signatures:
x y
target "SpatialPolygons" "missing"
defined "SpatialPolygons" "missing"
And BTW I see this in the response from ?getMethods
:
## Deprecated in 2010 and defunct in 2015 for \code{table = FALSE}:
getMethods(f, where, table = FALSE)