[R] Curry with `[.function` ?

From: Kenn Konstabel <lebatsnok_at_gmail.com>
Date: Mon, 21 Mar 2011 12:46:19 +0000

Dear all,

I sometimes use the following function:

Curry <- function(FUN,...) {

   # by Byron Ellis,

   .orig <- list(...)
   function(...) do.call(FUN,c(.orig,list(...)))    }

... and have thought it might be convenient to have a method for [ doing this. As a simple example,

> apply(M, 1, mean[trim=0.1]) # hypothetical equivalent to apply(M, 1,
Curry(mean, trim=0.1))

 would be easier to understand than passing arguments by ...

> apply(M, 1, mean, trim=0.1)

and much shorter than using an anonymous function

> apply(M, 1, function(x) mean(x, trim=0.1)

This would be much more useful for complicated functions that may take several functions as arguments. For example (my real examples are too long but this seems general enough),

foo <- function(x, ...) {

     dots <- list(...)
     mapply(function(f) f(x), dots)

foo(1:10, mean, sd)
foo(c(1:10, NA), mean, mean[trim=0.1, na.rm=TRUE], sd[na.rm=TRUE])

Defining `[.function` <- Curry won't help:

> mean[trim=0.1]

Error in mean[trim = 0.1] : object of type 'closure' is not subsettable

One can write summary and other methods for class "function" without such problems, so this has something to do with [ being a primitive function and not using UseMethod, it would be foolish to re-define it as an "ordinary" generic function.

Redefining mean as structure(mean, class="function") will make it work but then one would have to do it for all functions which is not feasible.

> class(mean) <- class(mean)
> class(sd)<-class(sd)
> foo(c(1:10, NA), mean, mean[na.rm=TRUE], mean[trim=0.1, na.rm=TRUE],
[1] NA 5.500000 5.500000 3.027650

Or one could define a short-named function (say, .) doing this:

> rm(mean, sd) ## removing the modified copies from global environment
> .<-function(x) structure(x, class=class(x))
> foo(c(1:10, NA), mean, .(mean)[na.rm=TRUE], .(mean)[trim=0.1, na.rm=TRUE],

But this is not as nice. (And neither is replacing "[" with "Curry" by using substitute et al. inside `foo`, - this would make it usable only within functions that one could be bothered to redefine this way - probably none.)

Thanks in advance for any ideas and comments (including the ones saying that this is an awful idea)

Best regards,


Kenn Konstabel
Department of Chronic Diseases
National Institute for Health Development Hiiu 42
Tallinn, Estonia

        [[alternative HTML version deleted]]

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. Received on Mon 21 Mar 2011 - 13:01:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Mar 2011 - 15:40:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive