使用 formattable 包,可以轻松地根据一列自身的值为其着色:
formattable::formattable(mtcars[1:3,1:2],list(mpg=color_text("blue","red")))
有没有办法根据 mpg 列中的值对 cyl 列进行着色? 谢谢!!
最佳答案
参见“列表”下的第二个参数:
library(formattable)
formattable::formattable(mtcars[1:3,1:2],
list(mpg = color_text("blue","red"),
cyl = formatter("span",
style = x ~ style(color = ifelse(mtcars$mpg[1:3] == 21, "blue", "red")))))
"...we define x as being the value by placing it to the left of the ~ and then use it in the function to the right (it is a lambda function, to use some jargon)" read more here
关于r - 使用 formattable 根据另一列的值对一列进行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51709995/