在formattable中,背景色可以由其他列确定吗?

问题描述

我正在使用formattable,并且我喜欢色块的外观,但是我无法弄清楚如何在该列中的渐变以外的任何颜色上指定颜色。我希望在color_tile中看到的背景色由其他列中的值确定

col1 <- c("a","b","c","d")
col2 <- c(532,123,324,NA)
col3 <- c(532,NA)
col4 <- c(-1,+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")))))
            ))

Col2具有漂亮的颜色平铺,基于第4列中的值,col3的文本是正确的颜色,我如何将二者结合起来,从而基于不同列中的值获得不错的选择背景色。还有“ span”或“ color”选项吗?

解决方法

使用以下代码为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")))))))

enter image description here


我发现可以通过查看函数color_tile的代码来实现:

> color_tile
function (...) 
{
    formatter("span",style = function(x) style(display = "block",`background-color` = csscolor(gradient(as.numeric(x),...))))
}
<bytecode: 0x00000225fccd6e08>
<environment: namespace:formattable>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...