[Rd] Patch proposal for match.fun()

Berwin A Turlach berwin at maths.uwa.edu.au
Sat May 27 13:07:57 CEST 2006


G'day all,

some time ago I sent an email regarding the following behaviour of
match.fun():

> x <- matrix(rnorm(200), ncol=2)
> var <- "fred"
> apply(x, 2, var)
Error in get(x, envir, mode, inherits) : variable "fred" of mode "function" was not found

and asked whether it would be desirable to change this behaviour such
that the function var would be found and no error would be produced.
I also asked whether there are arguments against such a change.

As I did not receive any arguments against such a change, I looked
into changing match.fun() such that the example above would work.  The
result is the patch attached below.  On my machine, r-devel passes
"make check FORCE=FORCE" with this patch applied.  Thus, hopefully
this change to match.fun() does not break anything.

I realise that there is now a lot of code replication in the function,
but was not sure whether it would be worthwile to write a small helper
function to reduce this replication.  (In particular, I was not sure
whether that would then involve using substitute thrice.  Since I
rarely use this command I am not so sure about its proper use.)

Also, I presume it is debatable whether a warning should be issued if
the lookup using get() fails if the argument was a character string of
length one.  Personally, I like it because the user still gets some
feedback that he used an (important) function name as variable name:

   > x <- matrix(rnorm(200), ncol=2)
   > var <- "foo"
   > apply(x, 2, var)
   [1] 1.055595 1.098397
   Warning message:
   Error in get(x, envir, mode, inherits) : variable "foo" of mode "function" was not found
    in: match.fun(FUN) 

and the combination of warning + error should make it really easy to
track down situations like this:

   > foo <- "bar"
   > apply(x, 2, foo)
   Error in get(x, envir, mode, inherits) : variable "foo" of mode "function" was not found
   In addition: Warning message:
   Error in get(x, envir, mode, inherits) : variable "bar" of mode "function" was not found
    in: match.fun(FUN) 

Of course, other people might have other tastes. :)

Please consider applying this patch (or a variation) to r-devel.

Thanks.

Cheers,

        Berwin

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: R-patch
Url: https://stat.ethz.ch/pipermail/r-devel/attachments/20060527/71da1900/attachment.pl 


More information about the R-devel mailing list