rkableextrastargazerxtable

General approach to line wrapping in R markdown tables


When including a table in a Rmarkdown document, I would like to enable automated line wrapping for tables that are wider than the page.

There are some questions facing similar issues, like here, here, or here

I know, that in kabelExtra, defining a fixed width using column_spec, or setting full.width = TRUE enables line wrapping, but this solution comes at a cost: The distances between lines become a bit janky. This can partly be fixed by setting booktabs = TRUE and linesep = "" or linesep = "\\addnewline". There are of course also workarounds with manually inserting linebreaks. See also this document by Hao outlining approaches specificially for kableExtra.

For stargazer or xtable, I only find approaches that involve post-processing of the latex output, like wrapping the output in a table environment or using gsub to replace parts of the latex code.

My problems with all the solutions is not that they don't work, but that they are tedious, sensitive to changes in the data, usually limit the document to tex/pdf output and require you to customize every table, if you value consistency. They also often complicate cross-referencing the table elsewhere and just "in general" complicate things.

What I'm looking for is a out-of-the-box solution with strong defaults, that applies linewrapping where needed, if the table would overflow otherwise. The best case would be something that also works for other output than pdf.

Is anyone aware of an approach that comes close to this, or knows whether this is a feature planned by any of the table packages? If someone knows, why this (seemingly simple) feature is absent of all the packages, I would be interested in that aswell.


"```"{r chunk-name}
df <- data.frame(
  ShortText = c("Row 1", "Row 2", "Row 3"),
  LongText = c(
    "This is a longer piece of text for the first row.",
    "Here is another extended description, this time for the second row. It's very long and would overflow easily",
    "Finally, the third row contains its own lengthy explanation."
  )
)
"```"

kable(df)


Solution

  • I loved knitr::kable and kableExtra, but ended up switching to flextable because of this very reason.

    With flextable the widths can be easily adjusted, and the line wrapping goes by default. The syntax is different to kable's, so it took me some time to get used to it, but it's a very flexible package that allows one to do a lot of things.

    library(flextable)
    flextable(df <- data.frame(
      ShortText = c("Row 1", "Row 2", "Row 3"),
      LongText = c(
        "This is a longer piece of text for the first row.",
        "Here is another extended description, this time for the second row. It's very long and would overflow easily",
        "Finally, the third row contains its own lengthy explanation."
      )
    ))
    

    Which produces this output:

    enter image description here

    You can check the flextable book.