[R] expressing functions

Jason Turner jasont at indigoindustrial.co.nz
Wed Dec 10 05:37:39 CET 2003


Remington, Richard wrote:
> # Why does expressing one function
> 
> require(ctest)
> t.test
> 
> # return only
> 
> function (x, ...)
> UseMethod("t.test")
> <environment: namespace:ctest>
> 
> # but expressing another function
> 
> shapiro.test
> 
> # returns more complete code?
> 
> function (x)
> {
>     DNAME <- deparse(substitute(x))
>     x <- sort(x[complete.cases(x)])
>     n <- length(x)
>     if (n < 3 || n > 5000)
>         stop("sample size must be between 3 and 5000")
...

Short answer:  Unless you're programming your own functions, you don't 
need to worry about that.

Long answer:  Because the first is generic - it looks at what kind of 
data you're testing (two vectors, a formula, whatever, ...) and calls 
the appropriate sub-function.  shapiro.test does not; it just takes one 
data format, and  stops in its tracks if that's not what you've provided.

The ideas behind this are documented in "Writing R Extensions" 
(R-exts.pdf) which is supplied with binary R distributions, and is 
available from CRAN.  See chapter 6, "Generic functions and methods", in 
the version that accompanies R-1.8.1.

Cheers

Jason
-- 
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
jasont at indigoindustrial.co.nz




More information about the R-help mailing list