Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change fontface (bold/italics) for a cell in a kable table in rmarkdown?

Is there a way to format a single cell in a table in rmarkdown? I am using kable to generate a table as follows:

library(knitr)
kable(data.frame(c('a','b','c'),c(1,2,3)))

I wish to bold "c" in the last row and also add a horizontal line at the end of the table. Any pointers?

like image 530
Avinash Avatar asked Jan 27 '15 08:01

Avinash


2 Answers

This also works: the first argument is row number, so you can bold rows programmatically or columns using column_spec.

library(kableExtra)

library(tidyverse)


kable(data.frame(letter =c('a','b','c'),number =c(1,2,3)))%>%
  kable_styling()%>%
  row_spec(3,bold=T,hline_after = T)
like image 83
Isaac Fratti Avatar answered Sep 20 '22 11:09

Isaac Fratti


Highlighting cells, rows or columns with pander is pretty straightforward:

> df <- data.frame(c('a','b','c'),c(1,2,3))
> emphasize.strong.cells(which(df == 3, arr.ind = TRUE))
> pander(df)

-------------------------------
 c..a....b....c..   c.1..2..3. 
------------------ ------------
        a               1      

        b               2      

        c             **3**    
-------------------------------

But adding horizontal line to the table is out of the scope of markdown table specifications.

like image 40
daroczig Avatar answered Sep 18 '22 11:09

daroczig