[R] The code behind the function

Erik Iverson eiverson at NMDP.ORG
Wed Sep 9 21:48:53 CEST 2009


----------------------------------------------------------
How can I see the code behind the function. For example,

> boxplot
function (x, ...) 
UseMethod("boxplot")
<environment: namespace:graphics>

I really would like to see how people code this.
----------------------------------------------------------

That *is* the code.  "boxplot" is a generic function, which calls another function based on what class of object you pass to it. You can find out which classes the boxplot method works on by using 

> methods("boxplot") 

[1] boxplot.default  boxplot.formula*

   Non-visible functions are asterisked

Next, try typing 

> boxplot.default

to see the implementation for the default function, and then since boxplot.formula is hidden, usually

getAnywhere("boxplot.formula") 

will retrieve it.

Section 10.9 of An Introduction to R explains this more:

http://cran.r-project.org/doc/manuals/R-intro.html#Object-orientation

Best Regards,
Erik Iverson   




More information about the R-help mailing list