Home > Mobile >  Kable, Flextable, Huxtable to HTML: force the display of cell contents on a single line
Kable, Flextable, Huxtable to HTML: force the display of cell contents on a single line

Time:06-29

I want the contents of my cells to be displayed in a single line. I'm using Rmarkdown to HTML. But no matter which package I use (Kable, Flextable, Huxtable), the column width specification is ignored and a line break is introduced, which makes the very ugly and unreadable results. In HTML, with a drop-down box, the total width shouldn't be a problem. I just want the results to be readable.

library(kableExtra)
library(flextable)

table = as.data.frame(matrix(rep("value [value1 - value2]",20), ncol = 10))

kbl(table) %>%
  kable_paper() %>%column_spec(1:ncol(table), width = "3.5cm", bold = TRUE, italic = TRUE)%>%
  scroll_box(width = "1000px", height = "500px")

tb = flextable(table)%>% flextable::width(width = 10)
knit_print(tb)

CodePudding user response:

With flextable, this code forces (note the usage of autofit()) the display on one single line:

library(flextable)

as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) %>% 
  flextable()%>% theme_box() %>% autofit()

This will produce a table display in an HTML window, this window has a width that is limited (the size of your window or the max-width of your HTML page). If the width of the browser window is less than the width of the table, it will be compressed to fit the window or the available space.

If you need to make this flextable horizontally scrollable (it is already implemented for bookdown but not yet for all HTML format), you can add this CSS code to your r markdown so that flextables can be scrollable (soon integrated into flextable then soon not necessary):

```{css echo=FALSE}
.flextable-shadow-host{
  overflow: scroll;
  white-space: nowrap;
}
```

An HTML 'R Markdown' document with it:

---
output: html_document
---


```{css echo=FALSE}
.flextable-shadow-host{
  overflow: scroll;
  white-space: nowrap;
}
```

```{r}
library(flextable)

as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) |> 
  flextable()|> theme_box() |> autofit()
```


enter image description here

  • Related