From murdoch at stats.uwo.ca  Thu Jul 16 04:18:53 2009
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed, 15 Jul 2009 22:18:53 -0400
Subject: [Rd] Handling masked methods
In-Reply-To: <4A5E8B55.2020602@csse.unimelb.edu.au>
References: <4A5E8B55.2020602@csse.unimelb.edu.au>
Message-ID: <4A5E8E0D.3020803@stats.uwo.ca>

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.

Duncan Murdoch
> You can get the code at
> http://www.cs.mu.oz.au/~gabraham/test1_0.1.tar.gz
> and
> http://www.cs.mu.oz.au/~gabraham/test2_0.1.tar.gz
>
> Thanks,
> Gad
>
>  > library(test1)
>  > f <- new('foo')
>  > f
> An object of class ?foo?
> Slot "x":
>       [,1]
> [1,]   NA
>
> Slot "y":
> logical(0)
>
>  > x <- matrix(rnorm(9), 3, 3)
>  > y <- rnorm(3)
>  > train(f, x, y)
> An object of class ?foo?
> Slot "x":
>             [,1]       [,2]      [,3]
> [1,] -1.1274528  1.5079401 1.5252550
> [2,] -1.8574282  0.1638099 0.4446156
> [3,] -0.7004087 -0.6221687 2.2455707
>
> Slot "y":
> [1] -0.2074759  0.2224259  1.1194120
>
>  > library(test2)
>
> Attaching package: 'test2'
>
>
> 	The following object(s) are masked from package:test1 :
>
> 	 train
>
>  > train(f, x, y)
> Error in function (classes, fdef, mtable)  :
>    unable to find an inherited method for function "train", for 
> signature "foo", "matrix", "numeric"
>  > test2::train
> standardGeneric for "train" defined from package "test2"
>
> function (object, x, y, ...)
> standardGeneric("train")
> <environment: 0x160a22d0>
> Methods may be defined for arguments: object, x, y
> Use  showMethods("train")  for currently available ones.
>  > test1::train
> standardGeneric for "train" defined from package "test1"
>
> function (object, x, y, ...)
> standardGeneric("train")
> <environment: 0x16075070>
> Methods may be defined for arguments: object, x, y
> Use  showMethods("train")  for currently available ones.
>
>



[Rd] Handling masked methods
Duncan Murdoch