【发布时间】:2020-03-17 09:48:07
【问题描述】:
我一直在处理 Rmarkdown 文档以输出为 html 文档。我想停止它在代码块中包装文本。例如,如果我运行一段使某些内容变得很宽的代码,它会包装输出,例如
---
output: html_document
---
```{r, collapse = TRUE}
cbind(iris[1:3, 1:4], iris[1:3, ])
```
返回(明显取决于浏览器的宽度)
cbind(iris[1:3, 1:4], iris[1:3, ])
## Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length
Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 5.1
3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 4.9
3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 4.7
3.2 1.3 0.2 setosa
如何指定要在代码块中关闭文本换行?
我还设置了 CSS,以便 .r{overflow-x: scroll;} 并尝试使用 .r{max-width: 10000pt;}、{.r{white-space: ... } 和 normal、pre-wrap、pre-line、pre 和 nowrap,以及 Rmarkdown 文件中的一些选项(options(width = 2000) 和 knitr::opts_chunk$set(tidy.opts=list(width.cutoff=2000),tidy=FALSE) 但没有成功。
输出应该是我的数据框的每一行(或代码行等)在浏览器中只占一行:
cbind(iris[1:3, 1:4], iris[1:3, ])
## Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 4.7 3.2 1.3 0.2 setosa
【问题讨论】:
-
嗯。将
options(width=200)放入代码块中在我的机器上运行良好。至少当我在浏览器中查看文档时。在 RStudio Viewer 中仍有一些包装。 -
对我来说,这改变了 r 分配给每一行的文本宽度,而不是换行行为......即
options(width=10)将允许每行占用 10 个字符,显示代码中的第一行将是cbind(iris,第二行[1:3, 1:4]等等。 -
好的。我懂了。对不起。然后我误解了你想要达到的目标。
-
不,我认为我的示例没有很好地说明它...我尝试对其进行修改以使其更清晰