[R] accessing source code in R packages

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 22 11:10:08 CEST 2005

The original reply was deliberately (I guess) vague. (I've removed the 
history, as attributions had already been removed, in violation of 
copyright law. If you cite someone, you MUST credit the author.)

Sometimes a little knowledge is a dangerous thing, and we have had a 
number of partially true answers.

Spreading confusion between the S4 classes of the 'methods' package and 
the (sometimes called S3) classes of base R is also dangerous.  The R 
documentation refers to S3 methods and classes unless otherwise stated 
(and in the methods package documentation).  Please follow that lead.

On Thu, 22 Sep 2005, Spencer Graves wrote:

> 	  Is there general documentation on a procedure to follow to:
> 	  (a) Find what methods are available for a particular class of 
> objects?

?methods, unless you mean an S4 class.

Be careful here: methods `for a particular class' are not all that might 
be dispatched, as methods for classes the object inherits from may also be 
used.  Thus "lm" methods may be invoked for "glm" objects, and you may 
need to call methods() for all the classes the object inherits from.

> 	  (b) Find what classes of objects have methods defined for a partilar
> generic function?

?methods, unless you mean S4 classes (and that help page leads you to the
right place for those).

> 	  (c) Get the code that's actually used?

getAnywhere() on the asterisked results of (a) or (b).

For a specific generic and a specific class, getS3method().

[There is a potential gap here as the "bar" method for class "foo" need 
not be called foo.bar().  So guessing the name may not work, but 
getS3method("foo", "bar") will.  AFAIK there are no live examples of 

> 	  For example, I recently needed to access numbers associated with an
> object of class "lmer".  Sundar suggested I use with 'getMethod("show",
> "summary.lmer")'.  However, this doesn't work with the example below.

(I think that was intended to refer to the default method for princomp, 
which is not an S4 generic in base R.

> methods("princomp")
[1] princomp.default* princomp.formula*

    Non-visible functions are asterisked
> getAnywhere("princomp.default")    # works
> getS3Method("princomp", "default") # works
> showMethods("princomp")

Function "princomp":
  <not a generic function>

show() is an S4 generic, not an S3 generic.  ?methods points you to how to 
explore S4 generics.

> library(lme4)
... (and drink some coffee while you wait)
> methods(show)
no methods were found
Warning message:
function 'show' appears not to be generic in: methods(show)
> showMethods("show")

Function "show":
object = "ANY"
object = "traceable"
object = "ObjectsWithPackage"
object = "MethodDefinition"
object = "MethodWithNext"
object = "genericFunction"
object = "classRepresentation"
object = "ddenseMatrix"
object = "Matrix"
object = "lmer"
object = "summary.lmer"
object = "VarCorr"
object = "sparseMatrix"
object = "lmList"

> selectMethod("show", "summary.lmer")
Method Definition:

function (object) ...

Here getMethod() will also work, but selectMethod() is more likely to 
find `the code that's actually used'.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list