From murdoch.duncan at gmail.com  Thu Aug  9 14:08:03 2012
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Thu, 9 Aug 2012 08:08:03 -0400
Subject: [Rd] Inserting and testing revised functions in a package
In-Reply-To: <loom.20120808T223723-762@post.gmane.org>
References: <4C409988.2070208@r-project.org>
	<loom.20120808T223723-762@post.gmane.org>
Message-ID: <5023A823.8080606@gmail.com>

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



[Rd] Inserting and testing revised functions in a package
Duncan Murdoch