I have a dataframe named df and I am utilizing html format within Rmarkdown to generate a table. However, due to varying cell sizes, the background color does not accurately fill each cell, leaving white spaces in between.
Here is the code I am using:
df = structure(list(projects = c("Restaurant, Pizzeria and Pasta House Works for New York",
"London Project - New Restaurant Project",
"Athens - Beautiful Lands for Aigaleo City Project",
"Berlin City - Exhibition near the airport with restaurants",
"Pizzeria Buenos Aires New italian restaurant - pasta, tartufo and vino",
"area mean value", "total mean value"),
happiness.and.joyfullness = c(3.5,4, 3, 3.2, 4, 5, 3),
joy.for.children = c(3, 5, 3, 4, 5,4, 4),
area = c(3, 5, 3, 3, 3, 4, 4),
damages.from.environment = c(2,4, 2, 3, 3, 5, 5),
approach = c(3, 1, 5,3, 5, 5, 4),
expensive = c(3, 5, 3, 4, 5, 5, 5),
safety.comes.first = c(5,5, 5, 5, 5, 5, 4),
hungry = c(3, 5, 2, 4, 5,5, 5)),
row.names = c(NA, -7L),
class = c("tbl_df", "tbl","data.frame"))
set_background_color = function(x, color = "white") {
kableExtra::cell_spec(x,
bold = TRUE,
align = "center",
color = "black",
background = color,
extra_css ="align-items: center;
justify-content: center;margin: -5px; padding: 15px; display: flex;",
background_as_tile = FALSE
)}
set_custom_background = function(x, color = "black") {
kableExtra::cell_spec(x,
bold = TRUE,
color="black",
background = color,
extra_css ="align-items: center;
justify-content: center;margin: -5px; padding: 10px; display: flex;",
background_as_tile = FALSE)
}
df %>%
dplyr::mutate( across(projects, \(x) case_when(projects == "area mean value" ~set_custom_background(x, "yellow") ,
projects == "total mean value" ~set_custom_background(x, "yellow"),
TRUE ~ projects)))%>%
dplyr::mutate(
across(happiness.and.joyfullness:hungry, \(x)
case_when(
x <= 3.5 ~ set_background_color(x, "lightblue"),
x > 3.5 & x <= 4.5 ~ set_background_color(x, "pink"),
x > 4.5 & x <= 5 ~ set_background_color(x, "purple"),
TRUE ~ set_background_color(x))))%>%
kableExtra::kbl(align = "c",position = "center",caption = "Project Scoring Table", escape=FALSE)%>%
kableExtra::kable_paper()%>%
kableExtra::row_spec(0,color = "white", background = "grey")%>%
kableExtra::column_spec(1:9,
bold = TRUE,
border_left = TRUE,
border_right = TRUE)%>%
kableExtra::row_spec(0:7, align = "center",extra_css = "vertical-align:middle;")
This resulted in: https://i.sstatic.net/YpXCI.png
How can I resolve this issue in R for my html Rmarkdown document? I have been experimenting with padding and margin but the problem persists when rendered to html via rmarkdown.