Skip to content

don't print object attributes

8 messages · Peter Ehlers, Liviu Andronic, Duncan Murdoch +1 more

#
Dear all
Suppose the object below:
How can I print the object without displaying the attributes? I
inspected ?print and ?print.default with no luck.
$setosa
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      15   5.006    4.40    4.59    4.80    5.00    5.20
    .90     .95
   5.41    5.61

          4.3 4.4 4.5 4.6 4.7 4.8 4.9  5 5.1 5.2 5.3 5.4 5.5 5.7 5.8
Frequency   1   3   1   4   2   5   4  8   8   3   1   5   2   2   1
%           2   6   2   8   4  10   8 16  16   6   2  10   4   4   2

$versicolor
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      21   5.936   5.045   5.380   5.600   5.900   6.300
    .90     .95
  6.700   6.755

lowest : 4.9 5.0 5.1 5.2 5.4, highest: 6.6 6.7 6.8 6.9 7.0

$virginica
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      21   6.588   5.745   5.800   6.225   6.500   6.900
    .90     .95
  7.610   7.700

lowest : 4.9 5.6 5.7 5.8 5.9, highest: 7.3 7.4 7.6 7.7 7.9

attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
     Species
1     setosa
2 versicolor
3  virginica

Regards
Liviu
#
On 28/08/2012 1:12 PM, Liviu Andronic wrote:
Assign a class to the object, and write a print method for it.

For example, this doesn't quite do what you want, but it's a start:

print.noattributes <- function(x, ...) {
    attributes(x) <- NULL
    print(x)
}

class(x) <- "noattributes"
x

It loses some attributes that you probably want to keep (e.g. the 
names), but otherwise works on your example.

Duncan Murdoch
#
On 2012-08-28 10:34, Duncan Murdoch wrote:
It seems that class "listof" also works:

   class(x) <- "listof"
   x

Peter Ehlers
#
On Tue, Aug 28, 2012 at 7:34 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
I've already tried this solution but that's exactly the trouble with
this approach. Do this on a data.frame and loses important
information.

I came up with a modified version of the above:
print_noattr <- function(x, keep.some=T, ...){
    if(keep.some) xa <- attributes(x)[c('names', 'row.names', 'class')]
    attributes(x) <- NULL
    if(keep.some) attributes(x) <- xa
    print(x)
}
$setosa
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      15   5.006    4.40    4.59    4.80    5.00    5.20
    .90     .95
   5.41    5.61

          4.3 4.4 4.5 4.6 4.7 4.8 4.9  5 5.1 5.2 5.3 5.4 5.5 5.7 5.8
Frequency   1   3   1   4   2   5   4  8   8   3   1   5   2   2   1
%           2   6   2   8   4  10   8 16  16   6   2  10   4   4   2

$versicolor
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      21   5.936   5.045   5.380   5.600   5.900   6.300
    .90     .95
  6.700   6.755

lowest : 4.9 5.0 5.1 5.2 5.4, highest: 6.6 6.7 6.8 6.9 7.0

$virginica
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      21   6.588   5.745   5.800   6.225   6.500   6.900
    .90     .95
  7.610   7.700

lowest : 4.9 5.6 5.7 5.8 5.9, highest: 7.3 7.4 7.6 7.7 7.9


However this still feels like a hack, and the function should be
modified if the object in question contains some other crucial
attributes.
On Tue, Aug 28, 2012 at 7:44 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
This works great. Thanks.

Liviu
#
On Tue, Aug 28, 2012 at 1:16 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
Well, you're sort of stuck between the fact that things you consider
important (dimensionality, names, etc.) are attributes not treated too
differently from "less important" attributes and I'm not sure there's
a way to do it entirely automatically. Though, untested, perhaps

mostattributes(x) <- NULL

gives you what you are looking for.

Cheers,
Michael
#
On 28/08/2012 2:16 PM, Liviu Andronic wrote:
I think it's reasonable to say it feels like a hack, because it is. x 
should have had a class and a print method for that class in the first 
place, if the attributes are not something that users should see.  If 
they are things you should see, then suppressing them is a bad idea.

Duncan Murdoch
#
On Tue, Aug 28, 2012 at 9:05 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
[..]
I think I like most the approach below.
print2 <- function(x, rm.attr=NULL, ...){
    if(!is.null(rm.attr)) attributes(x)[rm.attr] <- NULL
    print(x, ...)
}
$split_type
[1] "data.frame"

$split_labels
     Species
1     setosa
2 versicolor
3  virginica

$names
[1] "setosa"     "versicolor" "virginica"
$setosa
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      15   5.006    4.40    4.59    4.80    5.00    5.20
    .90     .95
   5.41    5.61

          4.3 4.4 4.5 4.6 4.7 4.8 4.9  5 5.1 5.2 5.3 5.4 5.5 5.7 5.8
Frequency   1   3   1   4   2   5   4  8   8   3   1   5   2   2   1
%           2   6   2   8   4  10   8 16  16   6   2  10   4   4   2

$versicolor
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      21   5.936   5.045   5.380   5.600   5.900   6.300
    .90     .95
  6.700   6.755

lowest : 4.9 5.0 5.1 5.2 5.4, highest: 6.6 6.7 6.8 6.9 7.0

$virginica
x[, "Sepal.Length"]
      n missing  unique    Mean     .05     .10     .25     .50     .75
     50       0      21   6.588   5.745   5.800   6.225   6.500   6.900
    .90     .95
  7.610   7.700

lowest : 4.9 5.6 5.7 5.8 5.9, highest: 7.3 7.4 7.6 7.7 7.9


This way the user has complete control over what attributes are
displayed or not. Wouldn't it be reasonable to have such an argument
in print()?

Regards
Liviu
#
On Wed, Aug 29, 2012 at 4:23 AM, Liviu Andronic <landronimirc at gmail.com> wrote:
I'd say no: firstly, print() is incredibly generic (type
methods(print) in a typical session) and gets called at every top
level eval so you  don't really want to add any overhead / complexity
to it. But more than that, and to Duncan's point, this is a hack:
attributes are usually used to define classes (not rigorously for S3,
but in practice) and allowing the user to turn some of them off is
likely to break some of the downstream print methods for little
perceptible gain. Attributes are there for good reason, I'm not sure
why you think they should be generally ignored.

That said, if you define print like that locally, it should mask
base::print just as well as changing base::print would.

Cheers,
Michael