[R] Sweave line breaks

Marc Schwartz marc_schwartz at me.com
Fri Jul 30 14:52:46 CEST 2010


On Jul 30, 2010, at 4:13 AM, soeren.vogel at eawag.ch wrote:

> Hello, when I print x in Sweave, the lines do not wrap. However, I want them to wrap (perhaps at a specified width). How? Thanks, *S*
> 
> <<keep.source=TRUE>>=
> x <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
> print(x)
> @


See ?strwrap and ?paste. Also use cat() instead of print().

The double "\\" at the end of each line tells LaTeX to force a line break. Since R will detect the "\" as an escape character, you need to double them when cat()ing. I also add a newline ("\n") so that the output after the "\\" goes to the next line.

> cat(paste(strwrap(x, width = 70), collapse = "\\\\\n"), "\n")
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\\
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\\
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut\\
aliquip ex ea commodo consequat. Duis aute irure dolor in\\
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla\\
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in\\
culpa qui officia deserunt mollit anim id est laborum. 


HTH,

Marc Schwartz



More information about the R-help mailing list