On Tue, Aug 28, 2012 at 9:05 PM, Duncan Murdoch
<murdoch.dun...@gmail.com> wrote:
> On 28/08/2012 2:16 PM, Liviu Andronic wrote:
>> 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)
>> }
>>
[..]
>> However this still feels like a hack, and the function should be
>> modified if the object in question contains some other crucial
>> attributes.
>
> 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.
>
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, ...)
}

> x <- dlply(iris, .(Species), function(x) describe(x[, 'Sepal.Length']))
> attributes(x)
$split_type
[1] "data.frame"

$split_labels
     Species
1     setosa
2 versicolor
3  virginica

$names
[1] "setosa"     "versicolor" "virginica"

> print2(x, c("split_type", "split_labels"))
$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

______________________________________________
R-help@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.

Reply via email to