使用KableExtra无法使表格列左对齐

时间:2020-08-26 00:24:03

标签: r dt kable kableextra formattable

我有一张正在尝试使用KableExtra和formattable包可视化的表。我面临的问题是,尽管指定了要保留的列的对齐方式,但某些列却没有对齐。在此处查看屏幕截图:

enter image description here

这是我正在使用的代码:

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)

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

仅当我在Rstudio中查看结果时,才出现此问题。以HTML格式导出时,IT可以完美地工作。因此,这里没有要解决的问题。