How can I tell R's flextable to add linebreaks within words if necessary to fit my table in the available width?

Viewed 555

I like the flextable package for R. I am using it to create tables for a Shiny app. Sometimes the tables occupy half the browser width, which might be quite a narrow space on some browsers. I would like to constrain flextable to not exceed its container width, even if this means inserting linebreaks within words.

Here is some example flextable code. Once you narrow the viewer window beyond a certain width, the flextable will not shrink further - instead the table will scroll horizontally.

library(magrittr)

mtcars %>%
  tibble::rownames_to_column("car") %>%
  flextable::flextable() %>%
  flextable::autofit()

The reactable package deals with this by inserting line breaks within words if necessary in order to remain within the maximum table width. This is often an ugly solution, but sometimes preferred.

mtcars %>%
  tibble::rownames_to_column("car") %>%
  reactable::reactable(defaultColDef = reactable::colDef(minWidth = 1))

Is there any way to get flextable to insert line breaks within words if that is necessary to not exceed the container width?

0 Answers
Related