我想在我的kable表中包装长文本。下面是一个表格的简单示例,其中列文本太长,需要为表格包装以适合页面。
---
title: "test"
output: pdf_document
---
```{r setup, include=FALSE}
library(knitr)
```
This is my test
```{r test, echo=FALSE}
test <- data.frame(v1=c("This is a long string. This is a long string. This is a long string. This is a long string. This is a long string.",
"This is a another long string. This is a another long string. This is a another long string. This is a another long string. This is a another long string."),
v2=c(1, 2))
kable(test)
```
答案 0 :(得分:21)
除了令人敬畏的pander
包之外的其他解决方案是在column_spec
中使用kableExtra
。在这种情况下,以下代码将起到作用。
kable(test, "latex") %>%
column_spec(1, width = "10em")
答案 1 :(得分:18)
我已经创建了pander
包,以灵活的方式生成降价表。默认情况下,它会将具有长字符串的单元格拆分为30个字符,但是有一堆global options和fn参数可以覆盖它,启用连字符和其他调整。快速演示:
> pander::pander(test)
-----------------------------------
v1 v2
------------------------------ ----
This is a long string. This is 1
a long string. This is a long
string. This is a long string.
This is a long string.
This is a another long string. 2
This is a another long string.
This is a another long string.
This is a another long string.
This is a another long string.
-----------------------------------
> pander::pander(test, split.cell = 80, split.table = Inf)
------------------------------------------------------------------------------------
v1 v2
------------------------------------------------------------------------------- ----
This is a long string. This is a long string. This is a long string. This is a 1
long string. This is a long string.
This is a another long string. This is a another long string. This is a another 2
long string. This is a another long string. This is a another long string.
------------------------------------------------------------------------------------