Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the table header bold with Knitr (for pdf output)?

Tags:

r

knitr

How can I make the header bold when using knitr::kable() function? It is automatically formatted as bold in HTML or word output, however, I couldn't change the format of the header when pdf output is selected.

Sample Code:

library(knitr)
kable(head(iris))

Output:

Image - The table header in pdf is not bold.

like image 672
Can H. Avatar asked Dec 21 '17 22:12

Can H.


People also ask

What is knitr :: Kable?

10.1 The function knitr::kable() 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 package is Kable in R?

The kableExtra package (Zhu 2021) is designed to extend the basic functionality of tables produced using knitr::kable() (see Section 10.1).


1 Answers

Use kableExtra and format the table header with bold text.

library(knitr)
library(kableExtra)
kable(head(iris),format="latex") %>% row_spec(0,bold=TRUE) %>% 
kable_styling()

...and the output:

enter image description here

like image 94
Len Greski Avatar answered Sep 26 '22 09:09

Len Greski