Search code examples
rr-markdownkablekableextra

Control table's width when displayed side by side


Is there a way to control each table's width separately when displayed side-by-side like in the example below?

```{r sample, echo=FALSE}
library(knitr)
library(kableExtra)
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
```

```{r, echo = FALSE}
kable(t1) %>%
  kable_styling(full_width = FALSE, position = "float_left")
kable(t2) %>%
  kable_styling(full_width = FALSE, position = "left")
```

For example, I would like the first table below to have 2 times the width of the second table.

I tried kable(format = 'html', table.attr = "style='width:80%;'") but it doens't work and I don't know why. The width doesn't change at all.

image


Solution

  • I tried kable(format = 'html', table.attr = "style='width:80%;'") but it doens't work and I don't know why. The width doesn't change at all.

    You were very close, just specify full_width to TRUE:

    ```{r, echo = FALSE}
    kable(t1, format = "html", table.attr = "style = 'width: 69%;'") %>%
      kable_styling(full_width = TRUE, position = "float_left")
    kable(t2) %>%
      kable_styling(full_width = FALSE, position = "left")
    ```
    

    Output:
    enter image description here