Wrap Long Text in Kable Table Column

wrap long text in kable table column

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

wrap text in knitr::kable table cell using \n

A fire-and-forget solution for flexible dual HTML/PDF of standard tables. It incorporates kableExtra's linebreak function outlined by @snoram.

Assumption: You use <br> as your line break indicator.

```{r}
knit_table(df1)
```

image

Code

library(dplyr)
library(knitr)
library(kableExtra)

knit_table <- function(df){
if (is_html_output()) {
df %>%
kable("html", escape = F) %>%
kable_styling()
} else {
df <- data.frame(lapply(df, function(x) {gsub("<br>", "\n", x)}), stringsAsFactors = F)

df %>%
mutate_all(linebreak) %>%
kable("latex", booktabs = T, escape = F)
}
}

Data

df1 <- data.frame(col1 = c("a", "b"),
col2 = c("one<br>two", "three<br>four"))

Wrap Column title text, column_spec not working for kable_styling

Add col.names() to the kable() function in the beggining and remove underline=TRUE from the row_spec() function.

kable(ranknew, format = "latex",  row.names = FALSE, 
col.names = c("Provinces","Brokerage Name","Written Pren....","Loss Ratio","New Business( Whatever comes here)"),
align = c("c", "l", "r", "r", "r"),
caption = paste("We had", nrow(ranknew) ,
"brokerages give us 100+ new policies in", year(Sys.Date())-1),
longtable = TRUE, booktabs = TRUE) %>%
kable_styling(latex_options = c("HOLD_position", "repeat_header"), full_width = F, font_size = 9, position = "center") %>%
column_spec(column = 1, width = "3cm")%>%
column_spec(column = 2, width = "8cm") %>%
column_spec(column = 3, width = "2cm") %>%
column_spec(column = 4, width = "1.5cm") %>%
column_spec(column = 5, width = "1.5cm") %>%
row_spec(row = 0, bold = TRUE, background = "orange",
color = "black", italic = FALSE, monospace = FALSE, strikeout = FALSE) %>%
print(floating = FALSEFALSE, row.names = FALSE)

This example works on my end with the iris dataset.

library(knitr)
library(kableExtra)
library(tidyverse)

kable(iris, format = "latex", row.names = FALSE,
col.names = c("Provinces","Brokerage Name","Written Pren....","Loss Ratio","New Business( Whatever comes here)"),
longtable = TRUE, booktabs = TRUE) %>%
kable_styling(latex_options = c("HOLD_position", "repeat_header"), full_width = F, font_size = 9, position = "center") %>%
column_spec(column = 1, width = "3cm")%>%
column_spec(column = 2, width = "8cm") %>%
column_spec(column = 3, width = "2cm") %>%
column_spec(column = 4, width = "1.5cm") %>%
column_spec(column = 5, width = "1.5cm") %>%
row_spec(row = 0, bold = TRUE, underline = TRUE, background = "orange",
color = "black", italic = FALSE, monospace = FALSE, strikeout = FALSE) %>%
print(floating = FALSEFALSE, row.names = FALSE)

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

You could use kableExtra::column_spec:

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

Efficient way to wrap column names of proportion tables in rmarkdown pdf output

At least, for kableExtra, you need to specify format in your kable function to be either latex or html.
Sample Image

To make it dynamic, you can save the table to a variable before it goes into kable and use 2:(ncol(your_table) + 1) in the column_spec function (+1 for the column_name column).

wrap long lines in kable() for use in Rnw/LaTeX

If you are compiling the pdf with latex I recommend package xtable you can replicate almost everything you have in the tabular environment. The align option gives you what you want: l - left, c - center, r - right, or the size of the column. I added other options for you to play around. In the chunk options you have to add results = 'asis'.

library(xtable)
print(xtable(test, caption="A caption", align="lp{2cm}p{2cm}"),
comment=F, include.rownames=F, table.placement="ht",
size=getOption("xtable.size", "tiny"))

Getting Column-Names to Wrap in R/Kable() HTML Table

It is possible to create linebreaks with HTML syntax. In order for that to work, you will have to set the escape argument of kable to FALSE.

library(knitr)
data(iris)

sink('my_file_path.html')
names_spaced <- c(
'Sepal Length', 'Sepal Width', 'Petal Length',
'Petal Width<br/> Long Col Name', ## add <br/>
'Species')

kable(head(iris),
format='html',
digits=1,
row.names=FALSE,
align='lccccc',
col.names = names_spaced,
escape = FALSE) ## disable html escape to
## make <br/> work

sink()


Related Topics



Leave a reply



Submit