Skip to content
Prev 77667 / 398503 Next

accessing source code in R packages

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:

            
?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.
?methods, unless you mean S4 classes (and that help page leads you to the
right place for those).
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 
this.]
(I think that was intended to refer to the default method for princomp, 
which is not an S4 generic in base R.
[1] princomp.default* princomp.formula*

    Non-visible functions are asterisked
Function "princomp":
  <not a generic function>
)

show() is an S4 generic, not an S3 generic.  ?methods points you to how to 
explore S4 generics.
... (and drink some coffee while you wait)
no methods were found
Warning message:
function 'show' appears not to be generic in: methods(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"
Method Definition:

function (object) ...

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