[Rd] str(..., strict.width="wrap") changes alignment of colons

G See gsee000 at gmail.com
Wed Nov 28 19:49:43 CET 2012


Hi,

This was surprising to me and does not appear to be documented in
?str.  After reading ?strwrap, I see that the problem is that
"Whitespace ... in the input is destroyed".  However, I thought I'd
mention it because I'm not sure it is intended behavior for str().

I have a list, and the names of the list have a variable number of characters.

> x <- list(A=1:10, ABCDEF=LETTERS[1:10])
> str(x)
List of 2
 $ A     : int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ ABCDEF: chr [1:10] "A" "B" "C" "D" ...

The above looks as expected in my R session (although it looks a
little different when I paste it into this e-mail).  There are 5
spaces between the A and the colon.  i.e. there are the same number of
characters before the colon for both components of the list.

However, if I use strict.width="wrap", the colons (:) are no longer aligned

> str(x, width=20, strict.width='wrap')
List of 2
$ A : int [1:10] 1
   2 3 4 5 6 7 8 9
   10
$ ABCDEF: chr
   [1:10] "A" "B"
   "C" "D" ...

Notice that now there is only 1 space between the A and the colon instead of 5.

It looks like the call to strwrap() is causing the issue (i.e.
destroying whitespace as it is documented to do).

This is fine:

> cat(capture.output(utils:::str.default(x)), sep="\n")
List of 2
 $ A     : int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ ABCDEF: chr [1:10] "A" "B" "C" "D" ...

This isn't:

> cat(strwrap(capture.output(utils:::str.default(x)), 20), sep="\n")
List of 2
$ A : int [1:10] 1
2 3 4 5 6 7 8 9 10
$ ABCDEF: chr
[1:10] "A" "B" "C"
"D" ...

I'm wondering if it would be possible for the call to strwrap() to not
include `indent.str`, `comp.str`, or the component names; then those
things could be added afterwards.   Although, I guess that probably
requires adding an argument to str.default to control whether names
are printed or not.

Regards,
Garrett



More information about the R-devel mailing list