在formattable中,背景颜色可以由不同的列确定吗
我在formattable中工作,我喜欢彩色瓷砖的外观,但除了该列中的渐变之外,我不知道如何指定其他颜色。我希望彩色瓷砖中的背景色由不同列中的值确定在formattable中,背景颜色可以由不同的列确定吗,r,formattable,R,Formattable,我在formattable中工作,我喜欢彩色瓷砖的外观,但除了该列中的渐变之外,我不知道如何指定其他颜色。我希望彩色瓷砖中的背景色由不同列中的值确定 col1 <- c("a", "b", "c", "d") col2 <- c(532, 123, 324, NA) col3 <- c(532, 123, 324, NA) col4 <- c(-1, 0, +1, NA) df &
col1 <- c("a", "b", "c", "d")
col2 <- c(532, 123, 324, NA)
col3 <- c(532, 123, 324, NA)
col4 <- c(-1, 0, +1, NA)
df <- data.frame(col1, col2, col3, col4)
formattable(df,
list(col1 = formatter("span", style = ~ style(color = "black",font.weight = "bold")),
col2 = color_tile("lightseagreen", "lightskyblue"),
col3 = formatter("span",
style = ~style(color = ifelse(col4 == 1, "red",
ifelse(col4 == 0, "green",
ifelse(col4 == -1, "yellow", "blue")))))
))
col1使用下面的代码为col3
设置您最喜欢的背景颜色。现在,我离开了你选择的那个
formattable(df,
list(col1 = formatter("span", style = ~ style(color = "black",font.weight = "bold")),
col2 = color_tile("lightseagreen", "lightskyblue"),
col3 = formatter("span",
style = ~style(display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = ifelse(col4 == 1, "red",
ifelse(col4 == 0, "green",
ifelse(col4 == -1, "yellow", "blue")))))))
通过查看函数color\u tile
的代码,我发现这是可能的:
> color_tile
function (...)
{
formatter("span", style = function(x) style(display = "block",
padding = "0 4px", `border-radius` = "4px",
`background-color` = csscolor(gradient(as.numeric(x),
...))))
}
<bytecode: 0x00000225fccd6e08>
<environment: namespace:formattable>
>彩色瓷砖
函数(…)
{
格式化程序(“span”,style=function(x)style(display=“block”,
padding=“0 4px”、`border radius`=“4px”,
`背景色`=csscolor(渐变色)(如数字(x)),
...))))
}
使用下面的代码为col3
设置您最喜欢的背景颜色。现在,我离开了你选择的那个
formattable(df,
list(col1 = formatter("span", style = ~ style(color = "black",font.weight = "bold")),
col2 = color_tile("lightseagreen", "lightskyblue"),
col3 = formatter("span",
style = ~style(display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = ifelse(col4 == 1, "red",
ifelse(col4 == 0, "green",
ifelse(col4 == -1, "yellow", "blue")))))))
通过查看函数color\u tile
的代码,我发现这是可能的:
> color_tile
function (...)
{
formatter("span", style = function(x) style(display = "block",
padding = "0 4px", `border-radius` = "4px",
`background-color` = csscolor(gradient(as.numeric(x),
...))))
}
<bytecode: 0x00000225fccd6e08>
<environment: namespace:formattable>
>彩色瓷砖
函数(…)
{
格式化程序(“span”,style=function(x)style(display=“block”,
padding=“0 4px”、`border radius`=“4px”,
`背景色`=csscolor(渐变色)(如数字(x)),
...))))
}
太好了。非常感谢,太好了。非常感谢。