[R] Word Wrap

Florent D. flodel at gmail.com
Sun Jan 15 18:18:58 CET 2012


see the 'fill' argument for cat().

On Sun, Jan 15, 2012 at 12:10 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> 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
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list