[R] How to see how a function is written

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jun 15 14:40:22 CEST 2010


On Tue, 2010-06-15 at 14:20 +0200, Sergey Goriatchev wrote:
> Hello,
> 
> If I want to see how, say, apply function is written, how would I be
> able to do that?
> Just typing "apply" at the prompt does not work.

In what sense does it not work? If I do this, I get:

> apply
function (X, MARGIN, FUN, ...) 
{
    FUN <- match.fun(FUN)
    d <- dim(X)
    dl <- length(d)
    if (dl == 0L) 
        stop("dim(X) must have a positive length")
    ds <- 1L:dl
    if (length(oldClass(X))) 
        X <- if (dl == 2)
....

Perhaps you a referring to S3 methods (and just used apply as an easy
example)? In which case you could try:

> fitted
function (object, ...) 
UseMethod("fitted")
<environment: namespace:stats>

> methods(fitted)
[1] fitted.default*       fitted.isoreg*        fitted.nls*          
[4] fitted.smooth.spline*

   Non-visible functions are asterisked

> fitted.isoreg
Error: object 'fitted.isoreg' not found

## hmm, not found, use getAnywhere instead

> getAnywhere(fitted.isoreg)
A single object matching ‘fitted.isoreg’ was found
It was found in the following places
  registered S3 method for fitted from namespace stats
  namespace:stats
with value

function (object, ...) 
{
    if (object$isOrd) 
        object$yf
    else object$yf[order(object$ord)]
}
<environment: namespace:stats>

Alternatively, use:

getS3method("fitted", "isoreg")

You might also consult Uwe Ligges 2006 article in R-News (now The R
Journal):

Uwe Ligges. R Help Desk: Accessing the sources. R News, 6(4):43-45,
October 2006.

http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf

If that doesn't help, can you show exactly what you types, what the
error was and what you were looking for, along with your R version etc.

My system/R version info (used for the above) is:

> sessionInfo()
R version 2.11.1 Patched (2010-06-14 r52272) 
x86_64-unknown-linux-gnu 

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=C              LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
base     

loaded via a namespace (and not attached):
[1] tools_2.11.1

HTH

G

> 
> Thank you for help!
> 
> Sergey
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list