如何使用Sweave控制回声宽度

5
我在使用Sweave时遇到了一个问题,输出的内容宽度无法控制。我的列表包含大量文本,在生成PDF文件时,R的回应超出了页面范围。我尝试使用

标签进行调整,但是并没有解决问题。
<<>>=
options(width=40)
@

但这并没有改变任何事情。
例如:设置列表(在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.")
@

并展示列表的入口。

<<>>=
my_list
@

有没有什么方式可以让这个工作正常运行,而不必通过cat语句来分割列表。

1
它在我的Mac上完美运行。您能否提供有关您的LaTeX安装和平台的更多详细信息? - Ramnath
1
在使用 Texshop 2.43 的 Mac 上,TeX 版本为 3.1415926(TeX Live 2010),R 版本为 2.13.0。它可以在我的电脑上运行,但是 PDF 中的文本超出了页面范围。 - Jase_
你说的“你的机器”是什么意思? - Roman Luštrik
2个回答

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

这是Mark Schwartz在2010年在rhelp上发布的帖子:

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

哇,那是2个小时前还是89年后呢?;-) - Andrie
1
你不知道Mark是时间领主吗? - IRTFM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接