如何使用Sweave控制回波宽度

时间:2011-08-29 20:42:42

标签: r sweave

我在sweave中的echo输出宽度有问题,我有一个包含大量文本的列表。问题是来自R的回声响应在pdf中运行。我尝试过使用

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

但这并没有改变任何事情。

示例:设置列表(不显示在乳胶中)。

<<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.")
@

显示列表的输入。

<<>>=
my_list
@

有没有什么方法可以让我无需用cat语句拆分列表就可以使用它。

2 个答案:

答案 0 :(得分:3)

您可以使用capture.output()捕获列表的打印表示,然后使用writeLines()strwrap()来显示此输出,包装得很好。当capture.output()返回包含对象的打印表示的字符串向量时,我们可以将它们中的每一个映射到屏幕/页面,但使用strwrap()进行换行。这种方法的好处是结果看起来像是由R打印的。这是解决方案:

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

产生:

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

答案 1 :(得分:1)

从马克施瓦茨2010年发布到rhelp:

cat(paste(strwrap(x, width = 70), collapse = "\\\\\n"), "\n")