[R] looking at a function's code

Karl Ove Hufthammer Karl.Hufthammer at math.uib.no
Mon Jul 16 14:32:45 CEST 2007


Walter Paczkowski:

> I'd like to look at the code for the R function head.  When I type just
> the word head, I get back
> 
> function(x, ...)
> UseMethod("head")
> <environment: namespace:utils>

This basically means that 'head' is a generic function that works in
different ways for different classes of objects (data frames, matrices,
tables &c.) Many function, e.g., 'plot' and 'mean', work the same way.

Type ?UseMethod for a better/longer explanation.

Now type

methods(head)

You will get:

[1] head.data.frame* head.default*    head.ftable*     head.function*
[5] head.matrix      head.table*

Now, ordinarily you should be able to write 'head.data.frame' to see the
code for this function, but, since it is starred, '*', this doesn't work.
The easiest way to get hold of it is using 'getAnywhere':

getAnywhere(head.data.frame)

-- 
Karl Ove Hufthammer



More information about the R-help mailing list