2 years ago
#20956

Alexandre Sanches
Whats the equivalent of data_color from gt package in kable?
I am trying to do this table in kable
package:
To do this, I am using data_color
function from gt
package.
I would like to know if there is any option to do the same in kable
package.
My code:
tbl_ipca %>%
gt() %>%
data_color(
columns = 1:3,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[1:3])), max(abs(tbl_ipca[1:3])))
)
) %>%
data_color(
columns = 4:6,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[4:6])), max(abs(tbl_ipca[4:6])))
)
) %>%
data_color(
columns = 7:9,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[7:9])), max(abs(tbl_ipca[7:9])))
)
) %>%
data_color(
columns = 10:12,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[10:12])), max(abs(tbl_ipca[10:12])))
)
)
My dput()
:
structure(list(`Set/21` = c(1.1, 1.1, 1.2, 0.9, 1.9, 0.7, 1.3,
0.3, 0.6), `Out/21` = c(1.2, 1.4, 1.2, 1.2, 1.4, 1.2, 1.3, 1.6,
1), `Nov/21` = c(0.6, 1.7, 0.9, 0.4, 2.3, 0.3, 0, 1, 0.3), `Set/21` = c(2.8,
3.5, 3, 2.4, 4.6, 2.2, 3.2, 2.1, 1.7), `Out/21` = c(3.4, 3.9,
3.3, 3, 4.3, 2.6, 3.9, 2.9, 2.1), `Nov/21` = c(2.9, 4.2, 3.4,
2.6, 5.7, 2.3, 2.6, 2.9, 2), `Set/21` = c(5.1, 5.9, 4.8, 3.6,
8.1, 2.4, 5, 4.6, 1.8), `Out/21` = c(5.9, 6.5, 5.7, 4.5, 9.2,
3.5, 6, 5.7, 2.8), `Nov/21` = c(5.5, 7.2, 5.9, 4.6, 9.4, 4, 5.2,
5.8, 3.3), `Set/21` = c(12.5, 11, 10.2, 8.4, 15.7, 5.4, 13.9,
7.8, 4.4), `Out/21` = c(12, 11.5, 10.7, 8.5, 17, 6, 13.1, 8.4,
4.9), `Nov/21` = c(10.8, 12.4, 10.7, 7.8, 19.2, 5.7, 10.4, 9.2,
4.8)), row.names = c("Comercializáveis", "Duráveis", "IPCA",
"Livres", "Monitorados", "Não Comercializáveis", "Não Duráveis",
"Semi-Duráveis", "Serviços"), class = "data.frame")
r
kable
gt
0 Answers
Your Answer