I may have something you can work with. Thanks for the data but it was a bit of a mess. you really should use dput()
. See the data in dput format below.
I have radically changed your program mainly because I do not normally use {dplyr} and I could not get those dplyr::rename() statements to work. Instead of using {dplyr} I used {data.table}. The syntax is rather different so you may want to have a look at A data.table and dplyr tour to see what I am doing.
I have produced two tables, Table1 uses kable_classic() which is closest to the "booktabs" format that is only available for LaTeX. Table2 uses the standard kable_styling() format.
I changed
cell_spec(rate_Change, background=ifelse(rate_Change > 0, "red", "green"))
to
cell_spec(rate_Change, background=ifelse(rate_Change >= 0, "red", "green"))
just to check that the statement was working. Your data has no rate_Change > 0
. You will want to change it back. For some reason I do not understand "% change\ncases" in not giving a line break.
suppressMessages(library(data.table))
suppressMessages(library(tidyverse))
suppressMessages(library(kableExtra))
setDT(Table1) # Convert data.frame to data.table to data.table
Table1[, rate_Change := cell_spec(rate_Change,
background=ifelse(rate_Change >= 0, "red", "green"))]
setnames(Table1, c("rate_Change", "district", "last_case", "last7day_case", "last14day_case"),
c("% change\nCases","District", "Date last Report Case", "last 7days Case", "last 14days Case"))
New_Table1 <-Table1 %>% kbl(format = "html", escape = FALSE ) %>%
kable_classic(full_width = FALSE, position = "center", font_size = 20) %>%
row_spec(22, extra_css = "border-bottom: 2px solid;")
New_Table1
New_Table2 <-Table1 %>% kbl(format = "html", escape = FALSE ) %>%
kable_styling(full_width = FALSE, position = "center", font_size = 20) %>%
row_spec(22, extra_css = "border-bottom: 2px solid;")
New_Table2
##=========================Data===================================##
Table1 <- structure(list(district = c("balaka", "blantyre", "chiradzulu",
"chitipa", "dedza", "karonga", "kasungu", "likoma", "lilongwe",
"machinga", "mangochi", "mchinji", "mzimba north", "mzimba south",
"neno", "nkhata bay", "nkhotakota", "ntcheu", "phalombe", "rumphi",
"thyolo", "zomba", "Total"), last_case = c(" 2024-03-16", " 2024-03-17",
" 2024-03-15", " 2024-03-16", " 2024-03-15", " 2024-03-17", " -Inf",
" 2024-03-15", " 2024-03-16", " 2024-03-17", " 2024-03-17", " 2024-03-16",
" 2024-03-15", " 2024-03-16", " 2024-03-15", " 2024-03-17", " 2024-03-15",
" 2024-03-13", " 2024-03-14", " 2024-03-14", " 2024-03-16", " 2024-03-17",
" -"), last7day_case = c(32L, 197L, 33L, 211L, 13L, 1301L, 0L,
171L, 302L, 72L, 235L, 88L, 165L, 37L, 3L, 374L, 221L, 5L, 8L,
27L, 43L, 26L, 3564L), last14day_case = c(37L, 253L, 33L, 221L,
15L, 1687L, 24L, 211L, 488L, 75L, 264L, 94L, 186L, 41L, 7L, 510L,
221L, 5L, 8L, 129L, 56L, 26L, 4591L), rate_Change = c(-13.5,
-22.1, 0, -4.5, -13.3, -22.9, -100, -19, -38.1, -4, -11, -6.4,
-11.3, -9.8, -57.1, -26.7, 0, 0, 0, -79.1, -23.2, 0, -22.4)), class = "data.frame", row.names = c(NA,
-23L))