[Rd] Inserting and testing revised functions in a package

Duncan Murdoch murdoch.duncan at gmail.com
Thu Aug 9 14:08:03 CEST 2012

On 12-08-08 4:59 PM, Chris Warren wrote:
> Thanks, that was helpful in finding a bug in a library I was using, but now I'd
> like to run the original code (the revised code is unable to access some of the
> functions in the package, apparently, but I think I can work around it by
> running and checking the original function).
> How can I recover the original function?  I tried using
> insertSource("SourceFileWithOriginalFunctionCode.R", package = "Package",
> functions = "functionOfInterest")
> but I run into the same inability to access some the package functions used by
> the function.
> I also tried reloading but got
> "The following object(s) are masked _by_ ‘.GlobalEnv’:
>      probtrans"

When you reply to a message that is more than 2 years old, it's helpful 
to give some context.

Generally the reason a function can't see things in a package is because 
its environment is wrong.  Since all functions in a package normally use 
the same environment, a simple fix is to put

environment(newfoo) <- environment(oldbar)

to copy the assignment of an unchanged function oldbar from the package 
to the new function you are trying to work with.

But the general way to "recover the original function" is just to 
restart R and attach the package.

Duncan Murdoch

More information about the R-devel mailing list