Skip to content
Prev 42176 / 63424 Next

returning information from functions via attributes rather than return list

On 12-01-04 3:19 PM, Paul Johnson wrote:
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