Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add more space between columns of knitr::kable() in RStudio notebooks?

I want to use function knitr::kable() in an RStudio notebook. If column names are short, the spaces between columns are small and it makes information hard to understand (see figure below). It seems that padding does not work in this situation. Is there a way to make spaces between the columns (indicated by yellow arrows) larger in RStudio notebooks?

library(magrittr)
iris %>% 
  setNames(c("A", "B", "C", "D", "E")) %>%
  head() %>%
  knitr::kable(padding = 20)

like image 905
GegznaV Avatar asked Sep 10 '19 14:09

GegznaV


People also ask

How do I add a space between columns in R?

Add space to both left and right of the column in RStr_pad function along with width argument and side=”both” adds space to both the sides of the column.

How do you make Kable wider?

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%;'") . This may lead to loosing the table lines. kableExtra offer nice styling tools to add for instance lines.

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?

Description. A very simple table generator, and it is simple by design. It is not intended to replace any other R packages for making tables. The kable() function returns a single table for a single data object, and returns a table that contains multiple tables if the input object is a list of data objects.


1 Answers

You could use kableExtra for nicer styling in general, including wider column spacing by default.

library(magrittr)
library(kableExtra)

iris %>% 
  setNames(c("A", "B", "C", "D", "E")) %>%
  head() %>%
  knitr::kable() %>% 
  kable_styling()
like image 87
joshpk Avatar answered Nov 11 '22 22:11

joshpk