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

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Tue, 15 Jun 2010 14:10:04 +0100

On Tue, 2010-06-15 at 14:56 +0200, Sergey Goriatchev wrote:
> Maybe I have to much stuff loaded in the workspace, Gavin, you are right:

OK, so now do

showMethods("apply")

And R should list out the available methods. See which package (re)defines apply.

But it is likely going to be simpler to start a clean session and look at the code in there. If you need the S4 method/generic code then you'll have to find out which package is redefining apply and look in the sources for that package.

HTH G

> > sessionInfo()
> R version 2.10.1 (2009-12-14)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=German_Switzerland.1252
> LC_CTYPE=German_Switzerland.1252
> LC_MONETARY=German_Switzerland.1252
> [4] LC_NUMERIC=C LC_TIME=German_Switzerland.1252
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] PerformanceAnalytics_1.0.0 quantmod_0.3-13 TTR_0.20-1
> Defaults_1.1-1 xts_0.7-0
> [6] fPortfolio_2100.78 Rglpk_0.3-5 slam_0.1-9
> fAssets_2100.78 fCopulae_2110.78
> [11] sn_0.4-14 mnormt_1.3-3
> fBasics_2110.79 timeSeries_2110.87 timeDate_2110.87
> [16] robustbase_0.5-0-1 quadprog_1.4-12 MASS_7.3-5
> fEcofin_290.76 foreach_1.3.0
> [21] codetools_0.2-2 iterators_1.0.3 zoo_1.6-3
>
> loaded via a namespace (and not attached):
> [1] grid_2.10.1 lattice_0.18-3 tools_2.10.1
>
>
> On Tue, Jun 15, 2010 at 14:56, Gavin Simpson <gavin.simpson_at_ucl.ac.uk> wrote:
> > 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
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >
> >
>
>
>

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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 - 13:13:42 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