[R] The code behind the function

cls59 chuck at sharpsteen.net
Wed Sep 9 21:55:12 CEST 2009




Chunhao Tu wrote:
> 
> Hi R users,
> I have a question. 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. Could someone please show
> me how to see the code behind the function?
> Many Thanks
> Tu
> 
> 


When you type a function name and see something like:

UseMethod("boxplot")

This means the the function you are inquiring about is a "generic function"-
that is it examines the class of the object passed to it and then calls a
class function. These class functions, also known as methods, all have the
form:

boxplot.<class-name>

So, a quick way to see which methods may be available is to use the methods
function()

> methods( boxplot )
[1] boxplot.default  boxplot.formula* boxplot.matrix

This shows that there are visible methods for objects of class matrix and
stats and a default method that is used in all other cases. Sometimes there
may be other methods that have been hidden in away in package namespaces
where it is harder to find them as in the case of boxplot.formula.

For visible methods, simply type their name to see their contents. I suspect
you will be most interested in boxplot.default:

> boxplot.default
[output omitted]

For non-visible methods, like boxplot.formula, use the getAnywhere function:

> getAnywhere( 'boxplot.formula' )
[output ommitted]

Hope this helps!

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/The-code-behind-the-function-tp25370743p25372134.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list