Skip to content
Prev 21507 / 63424 Next

How do I modify an exported function in a locked environment?

As others have mentioned its not really a good idea
to modify the namespace of a package and writing
a wrapper as Duncan suggested is much preferable.

An intermediate approach that
is not as good as the wrapper but better than modifying
the namespace is to copy the objects of interest
to your workspace, change their environments
appropriately and then modify their functionality:

library(zoo)

# copy objects of interest and set their environment

rollmean <- zoo:::rollmean
environment(rollmean) <- .GlobalEnv

rollmean.zoo <- zoo:::rollmean.zoo
environment(rollmean.zoo) <- .GlobalEnv

rollmean.default <- zoo:::rollmean.default
environment(rollmean.default) <- .GlobalEnv

# modify functionality

rollmean.default0 <- rollmean.default
rollmean.default <- function(x, ...) 100 * rollmean.default0(x, ...)

# test

rollmean(1:5, 3) # 100* used
rollmean(zoo(1:5), 3)  # 100* used
On 7/20/06, Steven McKinney <smckinney at bccrc.ca> wrote: