4

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")
astro person
  • 381
  • 5
  • 16

2 Answers2

9

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()
Amanda
  • 12,099
  • 17
  • 63
  • 91
0

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")
user2922935
  • 439
  • 4
  • 12