[R] make methods work in lapply - remove lapply's environment

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Sep 9 09:06:33 CEST 2008


This is a side-effect of lapply being in the base namespace and not 
evaluating its arguments, as explained on its help page which also points 
out that using a wrapper is sometimes needed.  It also points out that 
code has been written that relies on the current behaviour.

On Mon, 8 Sep 2008, Tim Hesterberg wrote:

> I've defined my own version of summary.default,
> that gives a better summary for highly skewed vectors.
>
> If I call
>  summary(x)
> the method is used.
>
> If I call
>  summary(data.frame(x))
> the method is not used.
>
> I've traced this to lapply; this uses the new method:
>  lapply(list(x), function(x) summary(x))
> and this does not:
>  lapply(list(x), summary)
>
> If I make a copy of lapply, WITHOUT the environment,
> then the method is used.
>
> lapply <- function (X, FUN, ...) {
>    FUN <- match.fun(FUN)
>    if (!is.vector(X) || is.object(X))
>        X <- as.list(X)
>    .Internal(lapply(X, FUN))
> }
>
> I'm curious to hear reactions to this.
> There is a March 2006 thread
>    object size vs. file size
> in which Duncan Murdoch wrote:
>> Functions in R consist of 3 parts: the formals, the body, and the
>> environment. You can't remove any part, but you can change it.
> That is exactly what I want to do, remove the environment, so that
> when I define a better version of some function that the better
> version is used.
>
> Here's a function to automate the process:
> copyFunction <- function(Name){
>  # Copy a function, without its environment.
>  # Name should be quoted
>  # Return the copy
>  file <- tempfile()
>  on.exit(unlink(file))
>  dput(get(Name), file = file)
>  f <- source(file)$value
>  f
> }
> lapply <- copyFunction("lapply")
>
> 	[[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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list