astro person
astro person

Reputation: 381

R Notebook: place knitr::kable tables side-by-side?

I'm writing up some figures and tables in an R Notebook, and I have a few tables I would like to place side-by-side. I am knitting the notebook to a html. The code I have at the moment (below) works, but both tables are aligned to the left. What I would really like is for them to appear side-by-side but also be centered. Any suggestions please? dt_tot and dt_tot_week are data.tables.

knitr::kable(dt_tot, "html", caption = caption) %>%
  kableExtra::kable_styling(bootstrap_options = c("hover"),
                            full_width = FALSE, position = "float_left")

knitr::kable(dt_tot_week, "html", caption = caption) %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
                          full_width = FALSE, position = "float_left")

Upvotes: 4

Views: 7854

Answers (2)

Amanda
Amanda

Reputation: 12737

If you're knitting to HTML, you should be able to use knitr::kables. This gives me two tables, side by side:

library(tidyverse)
library(kableExtra)

knitr::kables(list(
  kable(caption = "Left Table",
    starwars %>%
      count(species) %>%
      filter(n > 1)
    ) %>% kable_styling(),
    kable(caption = "Right Table",
      starwars %>%
        count(homeworld) %>%
        filter(n > 1)
    ) %>% kable_styling()
    
  )
) %>% kable_styling()

Upvotes: 10

user2922935
user2922935

Reputation: 439

You just need to change the position of table formed by dt_tot_week to float_right instead of float_left. I' am sure that must have been a typo in your code.

knitr::kable(dt_tot, "html", caption ="left Tbl") %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
                            full_width = FALSE, position = "float_left")

knitr::kable(dt_tot_week, "html", caption ="right Tbl") %>%
kableExtra::kable_styling(bootstrap_options = c("hover"),
                          full_width = FALSE, position = "float_right")

Upvotes: 0

Related Questions