garej
garej

Reputation: 565

How to keep some space between tables in a row in knitr::kable?

I use the following code from bookdown package tutorial in R markdown file:

```{r}
d1 <- head(cars, 3)
d2 <- head(mtcars[, 1:3], 5)
knitr::kable(
  list(d1, d2),
  caption = 'Two tables placed side by side.',
  booktabs = TRUE, valign = 't'
)
```

Unlike the book, the code returns the result without space between two tables, like so.

enter image description here

In some questions I see the solutions with cat() function and some html injections.

(Also we need to set the option results = 'asis')

I wonder is there a more elegant and simple way to set some space between tables and preserve default html formatting at the same time?

Upvotes: 4

Views: 1672

Answers (1)

Martin C. Arnold
Martin C. Arnold

Reputation: 9678

If you're not willing to do some tweaking using HTML/CSS, layout functions such as fluidRow() and column() from the Shiny package might help:

```{r, echo=F}
library(kableExtra)
library(shiny)

d2 <- head(mtcars[, 1:3], 5) %>% kable() %>% kable_styling()

fluidRow(
    column(2, HTML(d2), offset = 2),
    column(2, HTML(d2), offset = 2),
)
fluidRow(align = "center", column(12, "Figure 1: Two tables side by side"))
```

enter image description here

Upvotes: 1

Related Questions