[R] shorten str() output for long list

Peng Yu pengyu.ut at gmail.com
Fri Dec 4 07:53:38 CET 2009


On Fri, Dec 4, 2009 at 12:18 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Dec 3, 2009, at 10:11 PM, Peng Yu wrote:
>
>>> x=split(1:1000,1:1000)
>>> str(x)
>>
>> Although str() can suppress long output for vectors, but it can not
>> suppress long output for list. I'm wondering how to suppress the
>> output for long lists.
>
> Very simple ... You examine the code (for str.default it's not short, I will
> admit) and modify it to your specifications:
>
> str.default contains a section which is clearly for lists. These minor
> modifications to the list portion of the function will achieve what you
> request:
>
> Argument ... ,max.list=200 )   defined in the invocation segment:
>
> +                 for (i in seq_len(min(max.list,le) ) ){
>                                    ^^^^^^^^^^^^
> +                   cat(indent.str, comp.str, nam.ob[i], ":", sep = "")
> +                   envir <- if (typeof(object[[i]]) == "promise") {
> +                     structure(object, nam = as.name(nam.ob[i]))
> +                   }
>
> ...and ... since it is a recursive data structure...
>
> +                   str(object[[i]], nest.lev = nest.lev + 1, indent.str =
> paste(indent.str,
> +                     ".."), nchar.max = nchar.max, max.level = max.level,
> +                     vec.len = vec.len, digits.d = digits.d, give.attr =
> give.attr,
> +                     give.head = give.head, give.length = give.length,
> +                     width = width, envir = envir, max.list)
>                                                    ^^^^^^^^
>> x=split(1:10,1:10)
>> str(x)
> List of 10
>  $ 1 : int 1
>  $ 2 : int 2
>  $ 3 : int 3
>  $ 4 : int 4
>  $ 5 : int 5
>  $ 6 : int 6
>  $ 7 : int 7
>  $ 8 : int 8
>  $ 9 : int 9
>  $ 10: int 10
>> x=split(1:10,1:10)
>> str(x, max.list=5)
> List of 10
>  $ 1 : int 1
>  $ 2 : int 2
>  $ 3 : int 3
>  $ 4 : int 4
>  $ 5 : int 5

This might be useful to others. Could you add this to the future release of R?




More information about the R-help mailing list