Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting width of tables made with kable() in RMarkdown documents

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

A chunk like this for a table with two columns produces a table that takes up the entire width of the document. I'd like to make the columns narrower. Can this be done with kable() or is another package needed?

This rmarkdown chunk

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

Produces this table enter image description here

Aligning left with kable(df, align = "l") helps a little but I'd like the two columns adjacent to each other.

like image 714
N Brouwer Avatar asked Jan 27 '17 18:01

N Brouwer


People also ask

How do I change the width of a table in R markdown?

By defining the table format and adding some CSS styling you can change the size of the like so: knitr::kable(x, format = "html", table. attr = "style='width:30%;'") .

What does the Kable function do in R?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.

What is Kable package in R?

Function 'kable()' is a light weight table generator coming from 'knitr'. This package simplifies the way to manipulate the HTML or 'LaTeX' codes generated by 'kable()' and allows users to construct complex tables and customize styles using a readable syntax.


1 Answers

You can try out the kableExtra package.

kable(x, "html") %>%   kable_styling(full_width = F) 
like image 61
Hao Avatar answered Sep 18 '22 13:09

Hao