Quantcast
Channel: Active questions tagged kable - Stack Overflow
Viewing all articles
Browse latest Browse all 109

Full Cell in background color in html Rmarkdown table in R

$
0
0

I have a data frame called df that I want to print it in html format of Rmarkdown file. Globally I have set :

options(knitr.table.format = "html") 

in R setup chunk.

Below I wrote some code to conditional format the cells of the table. But the color background is only in the text not in the full cell.

df <- data.frame(  Name = c("John", "Helen", "George"),  Pizza = c(5, 4, 3.4),  Pasta = c(3.9,4.2, 4.2),  Sweet = c(4, 3, 5))set_background  = function(x, color = "white") {  kableExtra::cell_spec(x,                        bold = TRUE,                        align = "center",                        color = "white",                         background = color,                        extra_css ="background-color;display: block;text-align: center;padding: 0px;")}set_background2  = function(x, color = "black") {  kableExtra::cell_spec(x,                        bold = TRUE,                        color="black",                         background = color,                        extra_css ="background-color;display: block;text-align: center;padding: 0px;")}df %>%  mutate( across(Name, \(x) case_when(Name == "Helen" ~set_background2(x, "yellow") ,                                              Name == "John" ~set_background2(x, "yellow"),                                              TRUE ~ Name)))%>%  mutate(     across(Pizza:Sweet, \(x)           case_when(             x > 3  & x <= 3.5  ~ set_background(x, "blue"),             x > 3.5  & x <= 4.5  ~ set_background(x, "lightgreen"),             x > 4.5  & x <= 5    ~ set_background(x, "darkgreen"),             TRUE ~ set_background(x))))%>%  kableExtra::kbl(align = "c",caption = "Project Scoring Table", escape=FALSE)%>%  kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),                             full_width = F,                              font_size = 16)%>%  kableExtra::row_spec(0,color = "white", background = "grey")%>%  kableExtra::column_spec(1,                           bold = TRUE,                          border_right = TRUE)%>%  kableExtra::row_spec(0:3, align = "c",extra_css = "vertical-align:middle;")

resulting to the table as shown in the picture below :

enter image description here

What modifications I have to make in order the background color to cover the hole cell ?


Viewing all articles
Browse latest Browse all 109

Trending Articles