[Rd] print.default and attributes

ripley@stats.ox.ac.uk ripley@stats.ox.ac.uk
Thu Jan 30 08:43:03 2003


On Thu, 30 Jan 2003 Mark.Bravington@csiro.au wrote:

> When something gets printed by the "print.default" function, any extra
> attributes are printed without regard to their class attribute (if any). For
> example:
> 
> > x <- 1:3
> > attr( x, 'other') <- factor( c( 'cat', 'dog'))
> > attr( x, 'other')
> [1] cat dog
> Levels: cat dog
> > x
> [1] 1 2 3
> attr(,"other")
> [1] 1 2
> 
> which is perhaps surprising. Would it be preferable for "print.default" to
> call the generic "print" for each attribute, so that any class-specific
> print methods are respected?

That does seem unintentional, and not what S does.

> BTW, the same applies to "print.matrix" (which doesn't rely on "print"), and
> perhaps to other print methods. Of course, it will also apply to any
> class-specific print method which invokes "print" or "NextMethod", too.

In R-devel print.matrix *does* rely on print, and *is* a print method.
But the old-style prmatrix aka print.matrix does not print attributes 
normally:

> x <- matrix(1:4, 2, 2)
> attr( x, 'other') <- factor( c( 'cat', 'dog'))
> prmatrix(x)
     [,1] [,2]
[1,]    1    3
[2,]    2    4

So how did you make it print attributes?

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595