我有一个长的向量字符串(DNA序列),长度长达几千个连续字符,我想将其添加到我的knitr报告输出中。在RStudio中,文本换行处理得非常完美,但当我生成knitr HTML输出时,只能看到一行文本,并且它会顺着页面滚动。
RStudio输出
knitr输出
有没有办法调整knitr输出以换行?
谢谢。
RStudio输出
knitr输出
有没有办法调整knitr输出以换行?
谢谢。
pre
/code
的CSS定义实现的,例如word-wrap: break-word; word-break: break-all;
。这些定义实际上来自Bootstrap(目前rmarkdown使用Bootstrap 2.3.2)。str_break()
手动打断长字符串:A helper function `str_break()`:
```{r setup}
str_break = function(x, width = 80L) {
n = nchar(x)
if (n <= width) return(x)
n1 = seq(1L, n, by = width)
n2 = seq(width, n, by = width)
if (n %% width != 0) n2 = c(n2, n)
substring(x, n1, n2)
}
```
See if it works:
```{r test}
x = paste(sample(c('A', 'C', 'T', 'G'), 1000, replace = TRUE), collapse = '')
str_break(x)
cat(str_break(x), sep = '\n')
```
paste(sample(c("A","C","T","G"),1000,replace=T),collapse="")
paste(sample(c("A","C","T","G"),1000,replace=T),collapse="")
</div> - biomiha