[Rd] Handling masked methods

Gad Abraham gabraham at csse.unimelb.edu.au
Fri Jul 17 02:29:29 CEST 2009


On 16/7/09 12:18 PM, Duncan Murdoch wrote:
> Gad Abraham wrote:
>> Hi,
>>
>> Say I have two packages, test1 and test2, that both define the generic
>> method train (identical definition), and each has a specific train
>> method for a different S4 object (foo and bar, resp.)
>>
>> I want to be able to call train(foo, x, y) and train(bar, x, y), which
>> doesn't work since test2 masks test1, as seen below.
>>
>> The two solutions I can think of are to a) prefix train,
>> test1::train(foo) and test2::train(bar), which gets cumbersome for
>> non-trivial code, or b) make test2 depend on test1 so it doesn't have
>> to define the generic, but I'd rather keep the packages compatible but
>> not dependent. Any suggestions?
>>
> Two other possibilities:
>
> Make just one package, with just one definition, and put all the other
> stuff from test1 and test2 into it.
>
> Make a third package to hold the generic, and have both packages depend
> on that.

Thanks, but these approaches only really work if you're the author of 
both packages. I'm more interested in having compatible packages which 
just work together, just like predict works on any object.

Is there a way to tell R that the two generics are really the same so 
not to complain about it, or alternatively, to search for the method in 
each object's own package first?

I ended up assigning the respective train method as an attribute to each 
object, and then extracting that when needed; it works but it's a bit ugly.

-- 
Gad Abraham
MEng Student, Dept. CSSE and NICTA
The University of Melbourne
Parkville 3010, Victoria, Australia
email: gabraham at csse.unimelb.edu.au
web: http://www.csse.unimelb.edu.au/~gabraham



More information about the R-devel mailing list