Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R knitr::kable: avoid text wrap of one column

I'm creating a table in a rmarkdown/html document, where the columns are selected dynamically by a knitr parameter.

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

If I knit that, the dates in Col4 are wrapped. How can I avoid text wrapping just for column Col4, without changing the column names and the total width of the table?

Col4                Col4
------              ------
2018                2018-11-01
-11-01      ===>    2018-11-02
2018                2018-11-03
-11-02
2018
-11-03
like image 933
chgrl Avatar asked Sep 06 '25 02:09

chgrl


1 Answers

You could use kableExtra::column_spec:

library(kableExtra)
kable(testdata) %>% column_spec(column = 4, width = "100px")
like image 64
Martin Schmelzer Avatar answered Sep 07 '25 23:09

Martin Schmelzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!