[R] don't print object attributes

R. Michael Weylandt michael.weylandt at gmail.com
Tue Aug 28 20:27:19 CEST 2012


On Tue, Aug 28, 2012 at 1:16 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
> On Tue, Aug 28, 2012 at 7:34 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com> 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.
>>
> 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)
> }
>
>> x <- dlply(iris, .(Species), function(x) describe(x[, 'Sepal.Length']))
>> print_noattr(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.
>

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 Tue, Aug 28, 2012 at 7:44 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
>> It seems that class "listof" also works:
>>
>>   class(x) <- "listof"
>>   x
>>
> This works great. Thanks.
>
> Liviu
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list