Re: [R] How to see how a function is written

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Tue, 15 Jun 2010 13:56:54 +0100

On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote:
> Erik, I see the following when I type "apply" at the prompt:
>
> > apply
> standardGeneric for "apply" defined from package "base"

Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method.

Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet).

If you start R in a clean session, you should see the normal definition of apply

R --vanilla
apply

On Windows you may need to add that option to the shortcut you use to start R.

You could also try

base:::apply

to see the version in the base R namespace (at least I think that should work).

>
> function (X, MARGIN, FUN, ...)
> standardGeneric("apply")
> <environment: 0x03cad7d0>
> Methods may be defined for arguments: X, MARGIN, FUN
> Use showMethods("apply") for currently available ones.
>
> Also, whether I type "mean" at the prompt, or I type "edit(mean)", I
> do not see the underlying code for function "mean". How would I be
> able to see it?

The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything.

> methods(mean)

[1] mean.data.frame mean.Date mean.default mean.difftime [5] mean.POSIXct mean.POSIXlt
> getS3method("mean", "default")

function (x, trim = 0, na.rm = FALSE, ...) {

    if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {

        warning("argument is not numeric or logical: returning NA")
        return(NA_real_)

    }
    if (na.rm)

        x <- x[!is.na(x)]
    if (!is.numeric(trim) || length(trim) != 1L)

        stop("'trim' must be numeric of length one")     n <- length(x)
    if (trim > 0 && n) {

        if (is.complex(x)) 
            stop("trimmed means are not defined for complex data")
        if (any(is.na(x))) 
            return(NA_real_)
        if (trim >= 0.5) 
            return(stats::median(x, na.rm = FALSE))
        lo <- floor(n * trim) + 1
        hi <- n + 1 - lo
        x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
    }
    .Internal(mean(x))
}
<environment: namespace:base>

Although here, none of the mean methods are hidden so you could just type their names directly.

The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point.

HTH G

>
> ---
> My machine:
> platform i386-pc-mingw32
> arch i386
> os mingw32
> system i386, mingw32
> status
> major 2
> minor 10.1
> year 2009
> month 12
> day 14
> svn rev 50720
> language R
> version.string R version 2.10.1 (2009-12-14)
>
>
>
>
>
>
>
> On Tue, Jun 15, 2010 at 14:26, Erik Iverson <eriki_at_ccbr.umn.edu> wrote:
> > 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.
> >>
> >
> > Well, it is supposed to work, and it works for me. So you need to tell us
> > what "does not work" means, and all the info the posting guide requests, OS,
> > versions, etc.
> >
>
>
>

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
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.
Received on Tue 15 Jun 2010 - 12:53:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Jun 2010 - 13:20:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive