Create a kable table with cells containing multiline

I imported data from an .xlsx file and one column contained text with multiline. Example:

Obs <- c("A", "B")
Feature <- c("0= none; 1=low; 2=med; 3=high", "0= Impossible; 1= possible")
Dat <- as.data.frame(Feature, Obs)

Pretty <- Dat %>% 
  kable(caption="Table 1. XXX", booktabs= T, escape=F) %>%
  kable_styling() 
Pretty

I would like having the element contains in feature on multiple lines. Knowing that read.excel kept the multiline by introducing "\n". I tried linebreak function, but it did nothing.

Obs <- c("A", "B")
Feature <- c("0= none; 1=low; 2=med; 3=high", "0= Impossible; 1= possible")
Dat <- as.data.frame(Feature, Obs)
linebreak(Dat)

Pretty <- Dat %>% 
  kable(caption="Table 1. XXX", booktabs= T, escape=F) %>%
  kable_styling() 
Pretty

Any advice on how to create multiline in a cell? Your inputs are greatly appreciated.
Bersard

With escape=F in your table, you can replace all "; " with "<br>" in the Feature column.

library(tidyverse)

Pretty <- Dat %>% 
  mutate(Feature = str_replace_all(Feature, '; ', '<br>')) %>%
  kable(caption="Table 1. XXX", booktabs= T, escape=F) %>%
  kable_styling()

image

Thanks; it solved my issues. Greatly appreciated.

Bersard

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.