Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the echo width using Sweave

Tags:

r

sweave

I have a problem with the width of the output from echo within sweave, I have a list with a large amount of text. The problem is the echo response from R runs off the page within the pdf. I have tried using

<<>>=
options(width=40)
@

but this has not changed anything.

An example: Set up the list (not showing in latex).

<<echo=FALSE>>=
my_list <- list(example="Site location was fixed using a Silvia Navigator handheld GPS     in October 2003.  Point of reference used was the station Bench Mark. If the bench mark location was remote from the site then the point of reference used was changed to the 0-1 metre gauge. Bench Mark location was then recorded as a separate entry in the Site History section [but not used as the site location].\r\nFor a Station location map and all digital photograph's of the station, river reach, and site details see H:\\hyd\\dat\\doc.  For non digital photo's taken prior to October 2003 please see the relevant station file at Tumut office.")
@

And show the entry of the list.

<<>>=
my_list
@

Is there any way that I can get this to work without having to break up the list with cat statements.

like image 375
Jase_ Avatar asked Oct 11 '22 03:10

Jase_


1 Answers

You can use capture.output() to capture the printed representation of the list and then use writeLines() and strwrap() to display this output, nicely wrapped. As capture.output() returns a vector of strings containing the printed representation of the object, we can cat each of them to the screen/page but wrapped using strwrap(). The benefit of this approach is that the result looks like it was printed by R. Here's the solution:

writeLines(strwrap(capture.output(my_list)))

which produces:

$example
[1] "Site location was fixed using a Silvia Navigator
handheld GPS in October 2003.  Point of reference used
was the station Bench Mark. If the bench mark location
was remote from the site then the point of reference used
was changed to the 0-1 metre gauge. Bench Mark location
was then recorded as a separate entry in the Site History
section [but not used as the site location].\r\nFor a
Station location map and all digital photograph's of the
station, river reach, and site details see
H:\\hyd\\dat\\doc.  For non digital photo's taken prior
to October 2003 please see the relevant station file at
Tumut office."
like image 58
Gavin Simpson Avatar answered Oct 17 '22 10:10

Gavin Simpson