[R] How to see how a function is written
Gavin Simpson
gavin.simpson at ucl.ac.uk
Tue Jun 15 14:56:54 CEST 2010
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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list