Here are three examples where this matters, and I think the bug is
elsewhere!
1) Package accuracy does
ZeligHooks<-function (...) {
if (exists(".simHooked",envir=.GlobalEnv)) {
return(TRUE)
}
origsim=get("sim",envir=as.environment("package:Zelig"))
sim.replacement=function (object, x, ...) {
if (inherits(object,"sensitivity")) {
psim(object,x,...)
} else {
origsim(object,x,...)
}
}
assignInNamespace("sim",sim.replacement,"Zelig")
unlockBinding("sim",as.environment("package:Zelig"))
assign("sim",sim.replacement, envir=as.environment("package:Zelig"))
assign("sim",sim.replacement, envir=.GlobalEnv)
assign(".simHooked",TRUE,envir=.GlobalEnv)
}
Now, origsim() becomes a generic calling "sim", with defining environment
namespace:Zelig. However, sim in namespace:Zelig has been altered to be a
new function, whose enclosure is not namespace:Zelig and hence cannot see
the methods registered on the original sim() in namespace:Zelig. I think
that is the correct behaviour (the new sim might have nothing to do with
the old one). The fix would appear to be to set the environment of the
replacement to namespace:Zelig, but then origsim will not be visible from
sim.
Note that the package writes in the workspace and clobbers any object
called 'sim' there. Surely a less intrusive solution is needed?
There's a similar (maybe the same) problem in package VDCutil.
2) Package arules fails its tests. The problem is in Matrix:
function (x)
UseMethod("as.matrix")
<environment: namespace:base>
library(Matrix)
base::as.matrix
standardGeneric for "as.matrix" defined from package "base"
function (x)
standardGeneric("as.matrix")
<environment: 0x1453cc8>
Methods may be defined for arguments: x
Now is converting to an S4 generic *not* supposed to alter the function in
the original package/namespace? It does not do it if I do it by hand:
setClass("foo", "numeric")
setMethod("as.matrix", "foo", function(x) x)
Creating a new generic function for 'as.matrix' in '.GlobalEnv'
[1] "as.matrix"
function (x)
UseMethod("as.matrix")
<environment: namespace:base>
and this looks like a bug.
3) Package R.oo has things like UseMethod("$") whereas this is documented
to work for functions (not operators). This is unnecessary ($ does
internal dispatch) and the existing code is getting the wrong defining
environment (and although I've reinstated this as a workaround, I think it
should be an error).