【问题标题】:How to conditionally color table elements using formattable and color_tile?如何使用 formattable 和 color_tile 有条件地为表格元素着色?
【发布时间】:2019-06-26 16:22:31
【问题描述】:

我正在尝试设置一个带有彩色图块的表格,这些图块根据每列的平均值有条件地着色。基本上,如果该值低于平均值,则该图块将为红色,如果高于平均值,则该图块为绿色。我在下面的函数中使用了一个简单的 if else 语句。

我最终将使用“formattable”包和包中的 color_tile 函数。

我尝试编写自己的函数来选择颜色,但它只成功标记了我的数据框中的第一行。

#library(formattable) # not used in this example
green <- "#71CA97"
red <- "#ff7f7f"

col1 <- c(1.2, 4.2, 5.6, 7.1)
col2 <- c(5.0, 1.3, 10.3, 6.0)
col3 <- c(4.7, 6.3, 1.5, 6.3)
mydata <- data.frame(col1, col2, col3)

colorPicker <- function(x) {
     if(x <= 5) {return("red")}
     else {return("green")}
}

tile.colors <- lapply(c(mydata), colorPicker)

警告信息: 如果 (x 1,并且只使用第一个元素

"tile.colors" 返回正确的颜色,但仅限于第一行。

我最终会在可格式化函数中调用“tile.colors”,但现在我只是试图让颜色选择函数正确。

有没有更有效的方法来完成这项任务?

【问题讨论】:

    标签: r function colors conditional-formatting formattable


    【解决方案1】:

    我改编了与color_tile 相关的this answer to another question,以使这个函数在调用时根据列的平均值使图块着色:

     color_tile_mean <- function (...) {
      formatter("span", style = function(x) {
        style(display = "block",
              padding = "0 4px", 
              `border-radius` = "4px", 
              `background-color` = ifelse(x < mean(x) , "lightpink", "lightgreen"))
      })}
    
    

    我不知道如何让它接受自定义颜色,所以要更改它们,只需修改函数中的ifelse 条件即可。在你的情况下:

    library(formattable)
    
    # Set custom colors
    green <- "#71CA97"
    red <- "#ff7f7f"
    
    # Make example table
    col1 <- c(1.2, 4.2, 5.6, 7.1)
    col2 <- c(5.0, 1.3, 10.3, 6.0)
    col3 <- c(4.7, 6.3, 1.5, 6.3)
    mydata <- data.frame(col1, col2, col3)
    
    # Define color_tile_mean function
     color_tile_mean <- function (...) {
      formatter("span", style = function(x) {
        style(display = "block",
              padding = "0 4px", 
              `border-radius` = "4px", 
              `background-color` = ifelse(x < mean(x) , red, green)) # Remember to change the colors!
      })}
    
    # Use it just like color_tile but without colors
    formattable(mydata, align=c("c", "c", "c"),list(
      col1=color_tile_mean(),
      col2=color_tile_mean(),
      col3=color_tile_mean()
      )
    )
    

    Result

    我希望这很有用!

    【讨论】:

    • 我一直在到处寻找这样的东西!感谢分享!它工作得很好! :)
    【解决方案2】:

    你知道包DT吗?

    它允许格式化表格,您可以使用formatStyle()styleInterval() 等函数来根据间隔或平均值对值进行着色。

    library(DT)
    library(dplyr)
    
    datatable(mydata) %>% 
      formatStyle(columns = colnames(mydata), color = styleInterval(c(5), c(green, red)))
    

    datatable(mydata) %>% 
      formatStyle(columns = colnames(mydata), backgroundColor = styleInterval(c(5), c(green, red)))
    

    【讨论】:

    • 我想给值后面的“图块”上色,而不是值本身。你知道这是否可以使用“格式化”来重现,还是我最好使用 DT?我唯一担心的是我喜欢表格在 formattable 中的外观。
    • 您可以使用backgroundColor = 代替color =。这里有一些例子rstudio.github.io/DT/010-style.html。我不知道库可格式化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2019-11-12
    • 2014-05-04
    • 2023-04-07
    • 2021-07-16
    • 2017-12-04
    相关资源
    最近更新 更多