Adjusting Width of Tables Made with Kable() in Rmarkdown Documents

Is it possible to change the column width using kable without kableExtra?

If you have raw HTML or LaTeX code in the data, remember to put escape = FALSE in kable() to avoid escaping of special characters.

```{r}
library(kableExtra)
kable(df.table, "latex", booktabs = TRUE, escape = FALSE) %>%
kable_styling(full_width = FALSE) %>%
column_spec(4, width = "18em")
```

Sample Image

Scale kable table to fit page width

One option is to use kable_styling from the kableExtra package. The option latex_options="scale_down" will fit the table within the paper margins. See the vignette for detailed examples on all of the formatting options.

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
```

```{r}
kable(cbind(mtcars[1:5,], mtcars[1:5,]))
```

```{r}
kable(cbind(mtcars[1:5,], mtcars[1:5,]), format="latex", booktabs=TRUE) %>%
kable_styling(latex_options="scale_down")
```

Sample Image

Kable column width in html rmarkdown

You'll need to set width_min in column_spec. I think 3in is already enough for your column 1 in the example.

If you really need 10in for some reason, you can add scroll_box() to enable x-axis scrolling in the table (not included in my answer).

kable(df, col.names = colnames(df), escape = F) %>%
kable_styling(full_width = T) %>%
column_spec(1, width_min = '3in')

column_spec_width_min



Related Topics



Leave a reply



Submit