【问题标题】:color a column based on the values of another column with formattable使用格式化表根据另一列的值为列着色
【发布时间】:2018-08-06 14:31:41
【问题描述】:

使用 formattable 包,可以很容易地根据自己的值为一列着色:

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

有没有办法根据列 mpg 中的值来为列 cyl 着色? 谢谢!!

【问题讨论】:

    标签: r formattable


    【解决方案1】:

    参见“列表”下的第二个参数:

    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")))))
    

    "...我们将 x 定义为值,将其放在 ~ 的左侧,然后在右侧的函数中使用它(它是一个 lambda 函数,使用一些行话)" read more here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多