[R] Function suggestion

Jonathan Rougier J.C.Rougier at durham.ac.uk
Fri Jan 8 16:59:38 CET 1999


I hope this is the right forum (it'll be second time lucky if it is!).  I
note in the current distribution that functions passed as arguments (eg to
outer, sweep and the applys) are not handled consistently.  As I often
pass functions myself, I find the following simple function useful ...

match.fun <- function (FUN, descend = F) 
{
        if (is.character(FUN)) 
                get(FUN, mode = "function")
        else if (mode(FUN) != "function") {
                FUN <- deparse(substitute(FUN))
                if (descend) 
                        get(FUN, mode = "function")
                else stop(paste("\"", FUN, "\" is not a function", 
                        sep = ""))
        }
        else FUN
}

Note that by default this function does not look under an object that is
not a function -- this is my natural caution and contrary to the code in
apply().  I just thought it would be nice to tidy apply() etc by replacing
the first few lines with FUN <- match.fun(FUN), or FUN <- match.fun(FUN,
T); a bit similar to match.arg(), really.

Just a thought.  Cheers, Jonathan.

Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE

"[B]egin upon the precept ... that the things we see are to be 
 weighed in the scale with what we know"  (Meredith, 1879, The Egoist)

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list