[R] shorten str() output for long list

Martin Maechler maechler at stat.math.ethz.ch
Fri Dec 4 09:01:00 CET 2009


>>>> "DW" == David Winsemius <dwinsemius at comcast.net>
>>>>>     on Fri, 4 Dec 2009 01:18:56 -0500 writes:

    DW> 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.

    DW> Very simple ... You examine the code (for str.default it's not short,  
    DW> I will admit) and modify it to your specifications:

    DW> str.default contains a section which is clearly for lists. These minor  
    DW> modifications to the list portion of the function will achieve what  
    DW> you request:

    DW> Argument ... ,max.list=200 )   defined in the invocation segment:

    DW> +                 for (i in seq_len(min(max.list,le) ) ){
    DW> ^^^^^^^^^^^^
    DW> +                   cat(indent.str, comp.str, nam.ob[i], ":", sep = "")
    DW> +                   envir <- if (typeof(object[[i]]) == "promise") {
    DW> +                     structure(object, nam = as.name(nam.ob[i]))
    DW> +                   }

    DW> ...and ... since it is a recursive data structure...

    DW> +                   str(object[[i]], nest.lev = nest.lev + 1,  
    DW> indent.str = paste(indent.str,
    DW> +                     ".."), nchar.max = nchar.max, max.level =  
    DW> max.level,
    DW> +                     vec.len = vec.len, digits.d = digits.d,  
    DW> give.attr = give.attr,
    DW> +                     give.head = give.head, give.length = give.length,
    DW> +                     width = width, envir = envir, max.list)
    DW> ^^^^^^^^

(defensive programming requires to use  'max.list=max.list' in the line above.)

David, I know you as a smart R user.
If you provide patches against the *sources* , i.e.
  https://svn.r-project.org/R/trunk/src/library/utils/R/str.R
            *and* the help file                     man/str.Rd

I'll incorporate these into str() ,
not anymore for R 2.10.1 though.

With regards,
Martin Maechler, ETH Zurich

    >> x=split(1:10,1:10)
    >> str(x)
    DW> List of 10
    DW> $ 1 : int 1
    DW> $ 2 : int 2
    DW> $ 3 : int 3
    DW> $ 4 : int 4
    DW> $ 5 : int 5
    DW> $ 6 : int 6
    DW> $ 7 : int 7
    DW> $ 8 : int 8
    DW> $ 9 : int 9
    DW> $ 10: int 10
    >> x=split(1:10,1:10)
    >> str(x, max.list=5)
    DW> List of 10
    DW> $ 1 : int 1
    DW> $ 2 : int 2
    DW> $ 3 : int 3
    DW> $ 4 : int 4
    DW> $ 5 : int 5

    DW> -- 
    DW> David Winsemius, MD
    DW> Heritage Laboratories
    DW> West Hartford, CT

    DW> ______________________________________________
    DW> R-help at r-project.org mailing list
    DW> https://stat.ethz.ch/mailman/listinfo/r-help
    DW> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    DW> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list