[R] how to override/replace a function in a package namespace?
Ben Bolker
bolker at ufl.edu
Mon Aug 18 17:10:40 CEST 2008
Henrik Bengtsson <hb <at> stat.berkeley.edu> writes:
>
> The few times I want to patch a function like this, I use:
>
> unlockBinding(name, env);
> assignInNamespace(name, value, ns=pkgName, envir=env);
> assign(name, value, envir=env);
> lockBinding(name, env);
>
> /Henrik
>
OK, I admit that I don't get it.
I eventually managed to figure out that I needed the
environment corresponding to getNamespace("package")
in this case, not the one corresponding to
as.environment("package"), but I still seem to
be doing something wrong (perhaps I'm following the
recipe too mindlessly).
The goal here is to replace a bit of code
that uses parent.frame(2) with parent.frame(1) --
that's what my "rjhack.file" does.
tstf <- function() {
print(environment(jags))
a = grep("get,",deparse(jags),value=TRUE) ## parent.frame(2)
length(grep("1",a))>0
}
library(R2jags)
tstf()
rjhack.file <- "~/lib/R/pkgs/mixstock/pkg/inst/myjags.R"
source(rjhack.file) ## read hacked version into global workspace
tstf()
RJ <- getNamespace("R2jags")
## attempt to replace package version with hacked version ...
unlockBinding("jags", RJ)
assignInNamespace("jags",jags,ns="R2jags", envir=RJ)
assign("jags", jags, envir=RJ)
lockBinding("jags",RJ)
rm(jags) ## get rid of global copy
I'm beginning to feel it would be a lot easier
to build my own local copy of the package ... in
what way am I being a bonehead?
library(fortunes)
gsub("S4 methods","namespaces",fortune("S4")[1])
thanks
Ben Bolker
More information about the R-help
mailing list