From murdoch at stats.uwo.ca  Sat Aug 18 22:02:58 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Sat, 18 Aug 2007 16:02:58 -0400
Subject: [Rd] Overriding S4 methods in an installed package
In-Reply-To: <46C74E23.10408@research.telcordia.com>
References: <200708172217.l7HMHG6a025509@mcintosh.research.telcordia.com>	<m28x88u4em.fsf@fhcrc.org>
	<46C74E23.10408@research.telcordia.com>
Message-ID: <46C75072.3050101@stats.uwo.ca>

Allen McIntosh wrote:
> Seth Falcon wrote:
>   
>> Allen McIntosh <mcintosh at research.telcordia.com> writes:
>>     
>>> Is it possible to override S4 methods in an installed package?
>>> The naive
>>> library("pkg")
>>> setMethod("foo", signature(obj = "bar"),
>>>         function(obj , x, y) { new definition }
>>> 	, where="package:pkg")
>>> results in the error
>>> Error in setMethod("foo", signature(obj = "bar"), function(obj,  :
>>>         the environment "pkg" is locked; cannot assign methods for function "foo"
>>>       
>> If foo is a generic that you are calling directly, then you can
>> probably define it in the global environment (omit the where arg) and
>> test it that way.
>>
>> OTOH, if foo is used by pkg internally, then it will be much easier to
>> simply edit the source for pkg, reinstall and test.  If you find and
>> fix a bug, most package maintainers will be quite happy to integrate
>> your fix.
>>     
>
> Thanks for the suggestion.  Unfortunately, foo() uses internal 
> functions.  When foo() is defined in the global environment, these are 
> not visible.
>   
I think you can set the environment of your method to see the package 
internals.  Those internals won't see your method, though.

To do this, you'd do something like

newfoo <- function(obj , x, y) { new definition }
environment(newfoo) <- environment(foo) # or some other function from 
the package

setMethod("foo", signature(obj = "bar"),
        newfoo)


> I was hoping to avoid recompiling and installing under Windows.  Looks 
> like I may not have a choice.
>   
This is getting easier:  we're down to a single install, as long as you 
don't need all possible formats of man pages.

Duncan Murdoch
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] Overriding S4 methods in an installed package
Duncan Murdoch