[R] Getting code for functions

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Mon Sep 27 18:31:09 CEST 2004

Peter Flom wrote:

> Hello
> Pardon for the elementary question, I did try searching the archives
> with various terms, but to no avail.  I am using R1.9.1 on a windows
> machine
> One of the great advantages of R (to me, anyway) is being able to see
> the code for a function , e.g. by typing sd one sees the code for
> getting a standard deviation.
> However, for many functions this only provides info. including
> UseMethod, eg. typing mosaicplot yields
> function (x, ...) 
> UseMethod("mosaicplot")
> <environment: namespace:graphics>
> How can I see the code for such functions?


UseMethod implies that mosaicplot is a generic method. You can see which 
classes allow mosaicplot by using ?methods:

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

     Non-visible functions are asterisked

Now that last statement tells me that mosaicplot.default and 
mosaicplot.formula are not exported objects in the graphics namespace. 
To actually see the code for say the default mosaicplot, use 
?getAnywhere (or ?getS3method):

 > getAnywhere("mosaicplot.default")
A single object matching 'mosaicplot.default' was found
It was found in the following places
   registered S3 method for mosaicplot from namespace graphics
with value

function (x, main = deparse(substitute(x)), sub = NULL, xlab = NULL,
     ylab = NULL, sort = NULL, off = NULL, dir = NULL, color = FALSE,
     shade = FALSE, margin = NULL, cex.axis = 0.66, las = par("las"),
     type = c("pearson", "deviance", "FT"), ...)


You can learn about object-oriented programming in R from several 
places, but "Writing R Extensions" might be a good place to start.


More information about the R-help mailing list