Skip to content

make methods work in lapply - remove lapply's environment

3 messages · Tim Hesterberg, Duncan Murdoch, Brian Ripley

#
On 08/09/2008 9:37 PM, Tim Hesterberg wrote:
But that's not removing the environment, that's changing it.  Your 
function has globalenv() as its environment.
A shorter version is

copyFunction <- function(fn){
   environment(fn) <- globalenv()
   fn
}

(which doesn't require quoting the function name).

But getting back to your original question:  the real problem is with S3 
method dispatch and its interaction with lapply.  In the bad case, 
lapply calls the generic, which calls the dataframe method, which calls 
methods (probably via the generic again, but I didn't look) for each of 
the columns.  This is an ambiguous case:  should the dataframe method 
act the way its author expected, and call the standard method, or should 
it follow the search order you want?

I'd tend to think authors of functions should be able to depend on their 
behaviour not changing based on what's happening in the global 
environment.  That's not an absolute rule:  you should be able to define 
a new class and a method for it and have things work, but I don't think 
the fact that you have a summary.default should affect functions in 
namespaces that were written for the standard summary.default.

Duncan Murdoch
#
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: