[R] test for existance of a method for given class

William Valdar valdar at well.ox.ac.uk
Wed Nov 14 11:37:19 CET 2007


Dear All,

I want to test whether a method exists for given object. For example, 
whether a function "deviance" is defined for an object of the "lm" class.

My imperfect understanding leads me to think something like

  hasMethod("deviance", object)
  hasMethod("deviance", "lm")
  existsMethod("deviance", signature(class="lm"))

or similar might work (I don't fully understand how to manipulate 
signatures), but all the variations on this I have tried return FALSE. 
(Except, interestingly, when I first load library lme4, after which all 
return TRUE even for non-existant classes and functions).

I realize there are several ways in which R implements function 
polymorphism and that this is all documented somewhere but a hint would 
save me considerable time. I would also prefer not to resort to the hack 
solution of try()ing the function with the object and then catching the 
error to determine whether it was defined.

Thanks,

Will

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Dr William Valdar               ++44 (0)1865 287 589
Wellcome Trust Centre           valdar at well.ox.ac.uk
for Human Genetics, Oxford      www.well.ox.ac.uk/~valdar



More information about the R-help mailing list