[R] Word Wrap

Martin Morgan mtmorgan at fhcrc.org
Sun Jan 15 18:20:10 CET 2012


On 01/15/2012 09:10 AM, Barry Rowlingson 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.

Also ?strwrap, along the lines of

wrap = strwrap("All the world's a stage. And all the men and women
   merely players.", width=30)
cat(paste(wrap, collapse="\n"), "\n")

>
> 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.


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-help mailing list