Search code examples
rdtkablekableextraformattable

Unable to Left align table columns using KableExtra


I've a table which I'm trying to visualize using KableExtra and formattable package. The problem I'm facing is that despite specifying the alignment of the columns to be left, some of the columns are not being left aligned. See screenshot here:

enter image description here

Here's the codeI'm using:

df%>%
  mutate(
    N = comma(N, digits = 0),
    `Strongly agree`=color_bar("#D8E2F1")(percent(`Strongly agree`,1)),
    `Agree`=color_bar("#D8E2F1")(percent(Agree,1)),
    `Neither agree nor disagree`=color_bar("#D8E2F1")(percent(`Neither agree nor disagree`,1)),
    `Disagree`=color_bar("#D8E2F1")(percent(Disagree,1)),
    `Strongly disagree`=color_bar("#D8E2F1")(percent(`Strongly disagree`,1))
  )%>% filter(N!=0) %>%
  kable(escape = F, format = "html",align='cclllll', booktabs=T) %>%
  kable_styling(c("hover","condensed"), full_width = T) %>%
  column_spec(1, bold=T,color="black",border_right = TRUE)%>%
  column_spec(2, bold=F,border_right = TRUE,color="black",italic=TRUE)%>%
  column_spec(c(3,4,5,6,7),color="black", border_right = TRUE,width = "2in")%>%
  row_spec(c(0), extra_css = "border-bottom: 1px solid")%>%
  row_spec(c(5,10,15,16,17), bold=TRUE,background="#F3F2F2")%>%
  pack_rows("Overall", 1, 5)%>%
  pack_rows("Gender: Male", 6, 10)%>%
  pack_rows("Gender: Female", 11, 15)%>%
  pack_rows("Age 40+", 16, 16)%>%
  pack_rows("Age < 40", 17, 17)

What am I missing here ?


Solution

  • This issue only occurred when I was viewing the results in Rstudio. IT worked perfectly fine when exported as an HTML. So there was no issue to be addressed here.