r - 如何使用 Sweave 控制回波宽度

标签 r sweave

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

<<>>=
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 拆分列表的情况下使用它声明。

最佳答案

您可以使用 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."

关于r - 如何使用 Sweave 控制回波宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7235737/

相关文章:

RStudio Sweave 错误 -> 退出代码 -1073740791

r - 如何滞后时间变量并保持格式?

r - 如何在R中的散点图上的特定数据点周围放置圆圈?

r - 带有观星器的 LaTeX 表格的简短说明

r - 无法使用 R sweave 编译 pdf。 latex 错误 : file 'ae.sty' not found

r - 在 Sweave 中制作扁平化的 pdf

r - odfWeave 和 MS Word

r - data.table::fread 中的 dec 参数

r - 在 R 中使用 Friedman.test 没有对类公式对象进行整洁的方法

r - 将一个阿拉伯语句子分成单词会导致不同数量的单词具有不同的功能