[R] with for objects

Hadley Wickham h.wickham at auckland.ac.nz
Mon Dec 1 23:54:51 CET 2003


>>Why not? A data frame is a convenient way of grouping related data
>>together.  So is an object.  Writing with(expr, a + b) is just shorthand
>>for writing expr$a + expr$b, so why shouldn't I be able to write
>>with(obj, a + b) for obj at a + obj at b?
>>    
>>
>
>It is a bad idea because it has to break the information hiding that is an
>important point of objects.
>  
>
Good point - but with can be abused in this way too, eg. with(lm(...), 
plot(fitted.values ~ residuals)). 

What about if with(obj, a + b) was shorthand for a(obj) + b(obj)?  ie. 
it uses accessor functions instead of the slots (assuming that the 
accessor functions take no other arguments).

>If you define a class "A" that either includes or inherits from another
>class "B" then you don't know what slots your object has (without looking
>at the internals of the implementation of class "B"). 
>
I thought you did.  If I say x at A I expect that R will find the correct 
slot regardless of where in the class heirachy it is, though I suppose 
that is a slightly different question.

>Suppose you define
>slots a and b in addition to whatever you have inherited.
>You don't know what
>  with(obj, a+x)
>will do: is it obj at a+x or obj at a+obj@x for some inherited slot x? 
>
I'm not sure I understand the problem - I would expect it to try the 
slot in the object "environment" (if such a thing existed) first, and 
the parent environment etc.  Isn't this effectively what with() does?

>And even worse, if someone extends your class and adds a slot x in the subclass,
>what should it do?
>  
>
Yes, that is a (big) problem.  What do you suggest as an alternative? 

I am experimenting visualising microarray data using trellis.  I end up 
with a whole lot of calls like xyplot(maA(swirl[,1]) ~ maM(swirl[,1]) | 
maPrintTip(swirl[,1])) and then if I want to plot the same thing for 
array 2, I need xyplot(maA(swirl[,2]) ~ maM(swirl[,2]) | 
maPrintTip(swirl[,2]))...  I don't want to limit myself to a fixed set 
of plots because I'm still exploring different ways to look at the data, 
but I would like to save myself some typing.

>Without rewriting the internal code for eval() it is also hard to do: it
>would require either a recursive search through the expression changing
>slot names to obj at slot, or a function to convert objects into environments
>that could be fed to eval().
>  
>
I did end up writing something (very rough) to create an environment 
containing the results from the necessary function calls.  It works for 
my needs, but is very frail against changes in the objects I'm using.

Hadley




More information about the R-help mailing list