[R] function code

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Jan 25 16:02:16 CET 2008


hits=-2.6 testsºYES_00
X-USF-Spam-Flag: NO

On Fri, 2008-01-25 at 14:46 +0000, Luis Ridao Cruz wrote:
> R-help,
> 
> Sorry for this question (I guess it has been addressed before but 
> I could not find it in the archives)
> but how can I see a function code
> when the following comes up:
> 
> > svymean
> function (x, design, na.rm = FALSE, ...) 
> {
>     .svycheck(design)
>     UseMethod("svymean", design)
> }
> <environment: namespace:survey>

Dear Luis,

In this case, svymean is a generic function with several methods, and it
is these methods that contain the function code. We can see what methods
are available using methods()

> methods(svymean)
[1] svymean.survey.design*  svymean.survey.design2* svymean.svyrep.design* 
[4] svymean.twophase*      

   Non-visible functions are asterisked
>

You can also see this in the help for svymean (?svymean), which
documents several methods.

Notice in the output from methods() above that all the methods are
asterisked and therefore non-visible, which means you can't do this:

> svymean.survey.design
Error: object "svymean.survey.design" not found

There are several options. If you know the namespace you can do the
following:

> survey:::svymean.survey.design
function (x, design, na.rm = FALSE, deff = FALSE, ...) 
{
    if (!inherits(design, "survey.design")) 
        stop("design is not a survey design")
### Lots of code deleted ###
}
<environment: namespace:survey>

Another way is to use getAnywhere(foo), where foo is the function you
are looking for:

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

function (x, design, na.rm = FALSE, deff = FALSE, ...) 
{
### Lots of code deleted ###

All of this is documented in Uwe Ligges excellent article in R News from
2006 (Uwe Ligges. R help desk: Accessing the sources. R News,
6(4):43-45, October 2006.), which you can access here:

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

HTH

G

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