如何更改 formattable 中的颜色渐变?

How to change color gradient in formattable?

我正在创建以下格式表:

我在Var1上设置了渐变,值越高它越红。我想知道是否有可能改变它,让它变得越来越绿,价值越高。我喜欢它的一切,我只希望渐变从黑色到绿色而不是从黑色到红色。我曾尝试调整 background-color 命令中的数字,但似乎无法正确处理。有可能解决这个问题吗?是否有我可能遗漏的关于数字颜色渐变的指南?

library(formattable)

ex <- structure(list(Group = 1:3, Var1 = c(0, 0.4, 0.8)), class = "data.frame", row.names = c(NA, 
-3L))

ex <- formattable(ex, 
                  align = c("l","l", "c", "c", "c", "c"),
                  list(Var1 = formatter("span",
                                                  style = x ~ style(display = "block",
                                                                    "border-radius" = "4px",
                                                                    "padding-right" = "4px",
                                                                    color = "white",
                                                                    "background-color" = rgb(ex$Var1/max(ex$Var1), 0, 0, 1)))))

您可以使用csscolor(gradient())定义渐变颜色。 - 这也是 formattable.

color_tile 函数中使用的函数
ex <- formattable(ex, 
                  align = c("l","l", "c", "c", "c", "c"),
                  list(Var1 = formatter("span",
                                        style = x ~ style(display = "block",
                                                          "border-radius" = "4px",
                                                          "padding-right" = "4px",
                                                          color = "white",
                                                          "background-color" = csscolor(gradient(as.numeric(x), "black", "green"))))))

ex