Adjust the width of tables made with kable () in RMarkdown docs

Is it possible to adjust the width of columns when creating tables using the kable () function in knitr?

Such a block for a table with two columns creates a table that occupies the entire width of the document. I would like to make the columns narrower. Can this be done with kable () or another package?

This fragment is rmarkdown

```{r} df <- data.frame(x = 1:10, y = 11:20) library(knitr) kable(df) ``` 

Produces this table enter image description here

Left alignment with kable(df, align = "l") helps a bit, but I would like the two columns to be adjacent to each other.

+11
r knitr r-markdown
source share
2 answers

You can try the kableExtra package.

 kable(x, "html") %>% kable_styling(full_width = F) 
+13
source share

My new favorite thing is the flextable package, because 1) the tables are beautiful, 2) you can more easily manage things like font width and size, and 3) they render HTML and Word well. Using your data:

 '''{r} library(flextable) library(magrittr) df <- data.frame(x = 1:10, y = 11:20) df %>% regulartable() %>% autofit() %>% width(j=~x,width=1) %>% width(j=~y,width=1) ''' 

If both width are 1, the columns are close together:

flextable-1

If you set the last row to width(j=~x,width=2) %>% width(j=~y,width=2) table looks like this:

flextable-2

+1
source share

All Articles