[R] shorten str() output for long list

David Winsemius dwinsemius at comcast.net
Fri Dec 4 07:18:56 CET 2009


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

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




More information about the R-help mailing list