[Rd] returning information from functions via attributes rather than return list

Duncan Murdoch murdoch.duncan at gmail.com
Wed Jan 4 22:23:42 CET 2012

On 12-01-04 3:19 PM, Paul Johnson wrote:
> On Tue, Jan 3, 2012 at 3:59 PM, Simon Urbanek
> <simon.urbanek at r-project.org>  wrote:
>> Paul,
>> On Jan 3, 2012, at 3:08 PM, Paul Johnson wrote:
>>> I would like to ask for advice from R experts about the benefits or
>>> dangers of using attr to return information with an object that is
>>> returned from a function. I have a feeling as though I have cheated by
>>> using attributes, and wonder if I've done something fishy.
>>> Maybe I mean to ask, where is the dividing line between attributes and
>>> instance variables?  The separation is not clear in my mind anymore.
>>> Background: I paste below a function that takes in a regression object
>>> and make changes to the data and/or call and then run a
>>> revised regression.  In my earlier effort, I was building a return
>>> list, including the new fitted regression object plus some
>>> variables that have information about the changes that a were made.
>>> That creates some inconvenience, however.  When the regression is in a
>>> list object, then methods for lm objects don't apply to that result
>>> object. The return is not an lm anymore.
>> Why don't you just subclass it? That's the "normal" way of doing things - you simply add additional entries for your subclass (e.g. m$myItem1, m$myItem2, ...), prepend your new subclass name and you're done. You can still dispatch on your subclass before the superclass while superclass methods just work as well..
>> Cheers,
>> Simon
> Yes. I see that now.
> But I'm still wondering about the attribute versus variable question.
> To the programmer, is there any difference between returning
> information as attributes or variables?
> Does R care if I do this:
> class(res)<- c("mcreg", "lm")
> attr(res, "centeredVars")<- nc
> Or this:
> class(res)<- c("mcreg", "lm")
> res$centeredVars<- nc
> Is there some place "down there," in the C foundations of R,  where an
> attribute is just a variable, as is centeredVars?

Yes, the internal implementation is different (res$centeredVars implies 
res is a list, but attributes are stored as a pairlist).  But the thing 
itself (your nc) can't tell where it is stored.

Attributes are slightly harder to work with, so Simon's recommendation 
is good advice.  But in cases where you want other functions to work 
with the result, and the result isn't a named list with a class, then 
attributes are a convenient way to go.

The only two snags I can think of are 1, that R uses a few attributes 
for its own purposes (e.g. "names", "dim" and "dimnames") and if you use 
those attribute names for something incompatible you'll probably run 
into all sorts of problems and 2, that S4 objects use attributes 
internally.  I wouldn't recommend using attributes on an S4 object.

Duncan Murdoch

More information about the R-devel mailing list