我想在长桌中包裹长文字。这是一个表的简单示例,该表的列文本太长,需要包装以使该表适合页面。

---
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)
```

最佳答案

我创建了pander包,以灵活的方式生成 Markdown 表。默认情况下,它将长字符串的单元格拆分为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.
------------------------------------------------------------------------------------

07-27 15:33