r - 将长文本换行到 kable 表列中

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

enter image description here

最佳答案

除了很棒的 pander 包之外,另一种解决方案是在 kableExtra 中使用 column_spec。在这种情况下,下面的代码就可以解决问题。

kable(test, "latex") %>%
  column_spec(1, width = "10em")

关于r - 将长文本换行到 kable 表列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425499/

相关文章:

r - 在 knitr/rmarkdown 中绘制为 png

r - 在 latex 表中显示\复选标记

RStudio - 更改默认代码块

r - 试图识别 Landsat 图像中的断点 - 为什么 BFAST 会错过明显的断点?

r - 将 5 位数字和日期的变量转换为日期值

r - 在 R 中将句子转换为单词

r - 编织后不要打开 RStudio 内部浏览器

r - 如何使用 r-markdown 编织与多行文本对齐的内嵌图像?

R_Multiple 使用 for 循环绘制同一图形

动态重命名变量