I am trying to add a spatial method to merge
which needs to be S4 (since it dispatches on the types of two different objects).
I have tried using an earlier solution as follows:
#' Merge a SpatialPolygonsDataFrame with a data.frame
#' @param SPDF A SpatialPolygonsDataFrame
#' @param df A data.frame
#' @param \dots Parameters to pass to merge.data.frame
#'
#' @export
#' @docType methods
#' @rdname merge-methods
setGeneric("merge", function(SPDF, df, ...){
cat("generic dispatch\n")
standardGeneric("merge")
})
#' @rdname merge-methods
#' @aliases merge,SpatialPolygonsDataFrame,data.frame-method
setMethod("merge",c("SpatialPolygonsDataFrame","data.frame"), function(SPDF,df,...) {
cat("method dispatch\n")
})
Which does work:
x <- 1
class(x) <- "SpatialPolygonsDataFrame"
y <- data.frame()
> merge(x,y)
generic dispatch
method dispatch
You're going to have to trust me that if x is really a SPDF instead of a faked one, that it doesn't return the slot error which you get if you actually run that code (or don't, and just use the more permissive generic below which doesn't return the error). SPDFs are a pain to create.
The problem is that it seems to have overwritten S3 dispatch:
> merge(y,y)
generic dispatch
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function "merge", for signature "data.frame", "data.frame"
How do I avoid that? I've tried eliminating the function definition from setGeneric
so that it simply reads setGeneric("merge")
but that doesn't work either. Do I need to somehow import the merge
S3 generic from base
?