r - 使用 formattable 根据另一列的值对一列进行着色

标签 r formattable

使用 formattable 包,可以轻松地根据一列自身的值为其着色:

formattable::formattable(mtcars[1:3,1:2],list(mpg=color_text("blue","red")))

enter image description here

有没有办法根据 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/

相关文章:

css - kableExtra with Formattable 格式化问题

R Formattable 将两种格式应用于一个区域

r - 更改随机选择的 data.table 子集的值

r - 使用TTR包计算指数移动平均线

r - Shiny 的 DT 数据表 - 重置过滤器

powershell - 如何创建与格式表配合良好的 PowerShell 数据结构

r - 在 R 中为可格式化数据表着色 - 库存数据

r - 针织和 Beamer 与图中的叠加

用于匹配模式的每第 n 次出现的正则表达式

r - 如何使用 formattable 折叠表中的行值组?