如何使用整洁和编织的方式在R源代码中包装文本 [英] How to wrap text in R source with tidy and knitr

查看:71
本文介绍了如何使用整洁和编织的方式在R源代码中包装文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在使用knitr,尽管其中的大多数方面都进行得很顺利,但存在一个格式问题,即我尚未弄清在完成的文档中包含R代码.我经常需要在我的R块中创建相对较长的文本字符串,例如xtable()函数的标题.整洁通常在包装R代码并将其保存在LaTeX的阴影框中方面做得很出色,但它不知道如何处理文本字符串,因此它不会将它们包装起来,并且它们会从文本框的右侧流出.页面.

I'm working with knitr lately and while most aspects of that have gone quite smoothly, there's one formatting issue with including R code in the finished document that I haven't figured out. I often need to create relatively long text strings in my R chunks, e.g. captions for xtable() functions. While tidy generally does a great job at wrapping R code and keeping it in the shaded boxes in LaTeX, it doesn't know what to do with text stings, so it doesn't wrap them, and they flow off the right side of the page.

我会很高兴能完成所有工作的解决方案.但是,我也对可以手动应用于Rnw源中R块中的长字符串的解决方案感到满意.我只是不想编辑由KnitR创建的tex文件.

I would be most happy with a solution that has tidy doing all the work. However, I'd also be satisfied with a solution that I can apply manually to long strings in R chunks in my Rnw source. I just don't want to have to edit the tex file created by KnitR.

下面是一个最小的工作示例.

Below is a minimal working example.

\documentclass[12pt, english, oneside]{amsart}

\begin{document}

<<setup, include=FALSE, cache=FALSE, tidy=TRUE>>=
options(tidy=TRUE, width=50)
@

<<>>=
x <- c("This","will","wrap","nicely","because","tidy","knows","how","to","deal","with","it.","So","nice","how","it","stays","in","the","box.")
longstr <- "This string will flow off the right side of the page, because tidy doesn't know how to wrap it."
@

\end{document}

推荐答案

这是一种非常手动的解决方案,但我已经使用过.

This is an extremely manual solution, but one which I have used.

您可以使用paste0来构建字符串,这样可以使整齐地拆分字符串.

You build the string up, using paste0 and that gives tidy a chance to split it.

longstr <- paste0("This string will flow off the right side"," of the page, because tidy doesn't know how to wrap it.")

这篇关于如何使用整洁和编织的方式在R源代码中包装文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆