问题
Both kableExtra and formattable have fantastic features and I'll like to be able to incorporate them both in my tables.
Here is the table formatted using formattable:
Health_status = c("Very good", "Good", "Fair", "Bad", "Very bad", "Not stated", "Total")
Number = c(1032169, 453975, 125502, 22095, 5019, 73528, 1712288)
Percent = c(60.3, 26.5, 7.3, 1.3, 0.3, 4.3, 100)
Change = c(37672, 15231, 6536, 1988, 525, 30315, 92267)
Percent_Change = c(3.8, 3.5, 5.5, 9.9, 11.7, 70.2, 5.7)
df <- data.frame(Health_status, Number, Percent, Change, Percent_Change)
df %>%
formattable(align = c("l", "r", "r", "r", "r"),
list(Health_status = formatter("span", style = style(color = "grey")),
Change = color_bar("#71CA97")))
Which produces this table:
I would like to add other features (like a table heading) that I can't seem to do with formattable but can do with kable. However, when I include a the kable package the entire table loses is formattble changes:
df %>%
formattable(align = c("l", "r", "r", "r", "r"),
list(Health_status = formatter("span", style = style(color = "grey")),
Change = color_bar("#71CA97"))) %>%
kable(caption = "Table1")
Is there a way to include a table heading using just formattable? (I don't think that there is) and if not, is there a way to use both formattable and kable together?
Help appreciated.
回答1:
Found an example using kableExtra
and formattable
that was helpful here.
library(formattable)
library(kableExtra)
df %>%
mutate(
Change = color_bar("#71CA97")(Change),
Health_status = cell_spec(Health_status, "html", color = "grey")
) %>%
kable("html", escape = F, caption = "Table 1", align = c("l", "r", "r", "r", "r")) %>%
kable_styling("hover", full_width = F) %>%
column_spec(4, width = "3cm")
来源:https://stackoverflow.com/questions/59413937/how-do-i-get-kable-and-formattable-to-work-together-when-creating-a-table-in-r