[Rd] strwrap

J. Hosking jh910 at juno.com
Thu Sep 1 15:46:19 CEST 2005


The maximum length of a character string returned by strwrap,
i.e. max(nchar(strwrap(x,width))), never in my experience exceeds
width-2 (unless x contains a word that is longer than this).
This is not exactly a bug -- width is described only as a "target
column for wrapping lines" -- but seems odd.

Though I don't understand everything that the code is doing,
I think that the first line in the "while" block,

   k <- max(sum(lens < maxLength), 1)

should be replaced by

   k <- max(sum(lens <= maxLength + 1), 1)

-- here lens[i] is the number of characters in the string
that consists of the first i words *with a trailing space*,
so there is no need to split unless this number exceeds
the target width plus one.

A code snippet for testing:

    x <- paste(sapply(sample(10, 100, rep=T),
      function(x) substring("aaaaaaaaaa", 1, x)), collapse=" ")
    sapply(10:40, function(m) c(requested.width=m,
      actual.width=max(nchar(strwrap(x, m)))))

Tested on:

   > R.version
          _
   platform i386-pc-mingw32
   arch     i386
   os       mingw32
   system   i386, mingw32
   status   Under development (unstable)
   major    2
   minor    2.0
   year     2005
   month    08
   day      29
   svn rev  35457
   language R



J. R. M. Hosking



More information about the R-devel mailing list