[R] Word Wrap

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sun Jan 15 18:10:59 CET 2012


On Sun, Jan 15, 2012 at 4:40 PM, Francisco <franciscororolaio at google.com> wrote:
> Hello,
> I have to write a big sentence with cat() and I would like that R
> automatically adds a new line when it is needed (when the text arrives at
> the end of the window), the same as Windows Notepad does (Word Wrap).
> How could I do?

A little bit of research wouldn't hurt:

http://en.wikipedia.org/wiki/Word_wrap#Algorithm

You can split text in R with strsplit, get the length of strings with
nchar, and add up the lengths with + or sum.

 The greedy algorithm just takes as many strings until the next one
would go over the line length, and then spits those out and a new
line, and carries on. The other algorithm on Wikipedia looks much more
fun.

Barry



More information about the R-help mailing list