Skip to content
Prev 155426 / 398513 Next

make methods work in lapply - remove lapply's environment

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