R knitr :: kable:避免一列文字换行

时间:2018-11-26 08:46:46

标签: r r-markdown knitr

我正在rmarkdown / html文档中创建一个表,其中的列是通过knitr参数动态选择的。

---
output: html_document
---

```{r echo=FALSE}
library(knitr)
header <- c("Col1", "Col2", "Col3", "Col4", "Col6") # actually comes from knitr parameter
testdata <- data.frame(Col1 = c("text", "very long long long long long    long long long long long text", "texttext"),
             Col2 = c("looooooooooooooooooooooooooooooooooooooooooong long long long long long text", "textxyz", "abc"),
             Col3 =  1:3,
             Col4 = c("2018-11-01", "2018-11-02", "2018-11-03"),
             Col5 = c(100000, 200000, 300000),
             Col6 = c(123, 456, 789))
testdata <- testdata[,colnames(testdata) %in% header]
kable(testdata)
```

如果我这样编织的话,Col4中的日期将被包装。如何在不更改列名和表格总宽度的情况下避免只为Col4列换行?

Col4                Col4
------              ------
2018                2018-11-01
-11-01      ===>    2018-11-02
2018                2018-11-03
-11-02
2018
-11-03

1 个答案:

答案 0 :(得分:1)

您可以使用kableExtra::column_spec

library(kableExtra)
kable(testdata) %>% column_spec(column = 4, width = "100px")