将长文本包装在kable表列中 [英] wrap long text in kable table column

查看:78
本文介绍了将长文本包装在kable表列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将长文本包装在我的kable表中.这是一个表的简单示例,该表的列文本太长,需要包装起来才能使该表适合页面.

I would like wrap long text in my kable table. Here is a simple example of a table with column text that is too long and needs to be wrapped for the table to fit on the page.

---
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包,以灵活的方式生成降价表.默认情况下,它将长字符串的单元格拆分为30个字符,但是有一堆全局选项和fn参数来覆盖它,启用连字符和其他调整.快速演示:

I've created the pander package to produce markdown tables in a flexible way. By default, it will split cells with long string to 30 chars, but there are a bunch of global options and fn arguments to override that, enable hyphenation and other tweaks. Quick demo:

> 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.        
------------------------------------------------------------------------------------

这篇关于将长文本包装在kable表列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆