在kable table列中包装长文本

Eri*_*een 27 r knitr

我想在我的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)
```
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Hao*_*Hao 28

比其他真棒一种替代解决方案pander包是使用column_speckableExtra.在这种情况下,以下代码将起到作用.

kable(test, "latex") %>%
  column_spec(1, width = "10em")
Run Code Online (Sandbox Code Playgroud)

  • 如果您的表格浮动在页面之外,则 kableExtra 包中的“kable_styling(full_width = TRUE)”也会在需要时引入文本换行。 (3认同)

dar*_*zig 22

我已经创建了一个pander包,以灵活的方式生成降价表.默认情况下,它会将带有长字符串的单元格拆分为30个字符,但是有一堆全局选项和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.        
------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)