[R] Comparing two functions

Bert Gunter gunter.berton at gene.com
Mon Nov 29 08:22:07 CET 2010


It is perhaps worth mentioning in this context that in the R language,
functions and language components (like formulas, expressions, etc.)
are full first class objects -- which means basically that they can be
treated like any data object: e.g. passed as arguments to a function,
returned as values of a function, etc.

So, for example, Gabor's suggestion to pass the function as the
argument of your "master" function is a common R construct that
returns the values computed by the function you passed as an argument.
But you could also do it this way:

masterf <- function(rng=c("norm","unif"))
{
   rng <- match.arg(rng)
   switch(rng,
         norm = rnormlra,
         unif = runiflra
   )
}

Note that the return object is the function matching the possibly
abbreviated argument name (courtesy of match.arg()), _not_ the value
of the function. So this function could be called as, e.g.

masterf("n")(n=100, mean=10)

To be sure, this is a silly and cumbersome way to do things
here.Gabor's solution is what you want I think.**  But this kind of
approach can be very handy in certain circumstances and does give you
some sense of what can be done in R that may be unfamiliar to those
used to procedural languages like C or Fortran (or Java?).

Cheers,
Bert Gunter
Genentech Nonclinical Statistics

** His approach also has the advantage of explicitly passing the
function you want to masterf rather than forcing R to look for it (in
the environment in which masterf was defined). More typically, the
function would actually be defined within masterf using the arguments
passed to masterf. But that's another story (scoping).

On Sun, Nov 28, 2010 at 4:58 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Sun, Nov 28, 2010 at 5:53 AM, Ira Sharenow <irasharenow100 at yahoo.com> wrote:
>> Hi. I am new to R and facing a problem that I cannot solve.
>>
>> I am writing some basic functions. Then I would like to have a master function that allows me to call one of the functions which I created, but I cannot figure out how to do so.
>>
>> Below is an example. The functions rnormIra and runifIra both seem to work fine. I do not get an error message when entering the definition of the master function randomIra; however, it fails when I use it to call another function.
>>
>> rnormIra = function(n, mean) {
>>  return(rnorm(n,mean,1))
>> }
>> rnormIra(10,100) #works fine
>>
>> runifIra = function(n,min,max) {
>>  return(runif(n,min,max))
>> }
>> runifIra(5,20,30) #works fine
>>
>> randomIra = function(f, ...) {
>>  if(f == rnormIra) {
>>   return(rnormIra(n,mean))
>>  }
>>  else {
>>        return(runifIra(n,min,max))
>>  }
>> } #no error messages
>>
>> randomIra(rnormIra, n = 5, mean = 20) #FAILS
>> randomIra("runifIra", n = 5, min = 10, max = 25) #FAILS
>>
>> do.call(randomIra,list("rnormIra",5,20)) #FAILS
>>
>
> Try this:
>
> randomIra <- function(f, ...) if (identical(f, rnormIra))
> rnormIra(...) else runifIra(...)
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list