Search code examples
rr-markdownkable

Displaying list of kable dataframes loses formatting


I would like to display multiple tables side by side in Viewer/html. One way I found is using kable and specifying the list of datatables like so below, but the formatting is lost when a list of datatables are displayed versus displaying just a single datatable. Is there a way to retain kable formating and possibly add more formatting to make tables more differentiable with lines and spacing? Or is there another way to display side by side multiple tables in one Viewer/html besides kable? Thanks!

Example with displaying multiple tables where formatting is lost:

kable(list(head(cars), head(iris))) %>%
  kable_styling()

Formatting retained when only one table is displayed:

kable(head(cars)) %>%
  kable_styling()

Solution

  • You can use knitr::kables to get two tables side by side in the Viewer. Further, it will also work for knitting to HTML (or saving as HTML from the RStudio Viewer).

    library(tidyverse)
    library(kableExtra)
    
    knitr::kables(list(
      kable(caption = "Left Table",
            head(cars)) %>% kable_styling(),
      kable(caption = "Right Table",
            head(iris)) %>% kable_styling()
    )) %>%
      kable_styling()
    

    Output

    enter image description here