[Rd] Handling masked methods

Gad Abraham gabraham at csse.unimelb.edu.au
Thu Jul 16 04:07:17 CEST 2009


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?

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.

-- 
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