[R] shorten str() output for long list

0 views
Skip to first unread message

Peng Yu

unread,
Dec 3, 2009, 10:11:12 PM12/3/09
to r-h...@stat.math.ethz.ch
> 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.

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

David Winsemius

unread,
Dec 4, 2009, 1:18:56 AM12/4/09
to Peng Yu, r-h...@stat.math.ethz.ch

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

Peng Yu

unread,
Dec 4, 2009, 1:53:38 AM12/4/09
to r-h...@stat.math.ethz.ch

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

Henrik Bengtsson

unread,
Dec 4, 2009, 1:58:48 AM12/4/09
to David Winsemius, r-help
str(head(x))

str(head(x, n=5))

/H

Martin Maechler

unread,
Dec 4, 2009, 3:01:00 AM12/4/09
to David Winsemius, r-h...@stat.math.ethz.ch

>>>> "DW" == David Winsemius <dwins...@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-h...@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.

Greg Snow

unread,
Dec 4, 2009, 1:14:09 PM12/4/09
to Peng Yu, r-h...@stat.math.ethz.ch
An alternative to str is the TkListView function in the TeachingDemos package. You still get the long listing, but it is in a separate window that you can control the scrolling on by hand. For more complicated lists/objects it provides a tree structure so that you can look at only the detailed parts that you are interested in.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg...@imail.org
801.408.8111

Reply all
Reply to author
Forward
0 new messages