【问题标题】:Programmatically Color Format Numeric Columns by Each Column Range in Datatable按数据表中的每个列范围以编程方式为数字列设置颜色格式
【发布时间】:2019-03-01 19:12:49
【问题描述】:

我在这里开了一个关于如何在数据表中添加范围栏的帖子:Programmatically color format numeric columns in a datatable。

但是,我不想根据整个数据框拟合范围,而是根据每个单独列的范围进行格式化。我想出了一些有效的代码,但是,它绝对是令人生畏的,而不是程序化的。

library(magrittr)
library(DT)

# Specify numeric columns
foo <- sapply(iris, is.numeric)

datatable(iris, filter = 'top', options = list(pageLength = 5, autoWidth = TRUE)) %>%
  formatStyle(names(iris)[foo][1],
              background = styleColorBar(range(iris[, 1]), 'lightblue'),
              backgroundSize = '98% 88%',
              backgroundRepeat = 'no-repeat',
              backgroundPosition = 'center') %>%
  formatStyle(names(iris)[foo][2],
              background = styleColorBar(range(iris[, 2]), 'green'),
              backgroundSize = '98% 88%',
              backgroundRepeat = 'no-repeat',
              backgroundPosition = 'center')

【问题讨论】:

    标签: r shiny datatables dt


    【解决方案1】:

    这是一个开始,它为数据集中的每个数字列绘制它。您可以修改以适应特定的用例。

    library(tidyverse)
    library(DT)
    library(RColorBrewer)
    
    colorbarTable = function(table,colorscale){
      num = sapply(table,is.numeric) #Find which columns are numeric
      colors = brewer.pal(sum(num),colorscale) #Define the number of colors
    
      DT = datatable(table,filter = 'top',options = list(pageLength = 5, autoWidth = TRUE)) #Define the base data table
    
      for(i in seq_along(num)){
        if(num[i]){
          #If numeric add to the datatabls
          DT = DT%>%
            formatStyle(names(table)[i],
                        background = styleColorBar(range(table[,i]), colors[i]),
                        backgroundSize = '98% 88%',
                        backgroundRepeat = 'no-repeat',
                        backgroundPosition = 'center')
        }
      }
    
      return(DT)
    }
    
    
    colorbarTable(mtcars,"Pastel1")
    

    使用iris 和mtcars 数据集对其进行了测试。

    【讨论】:

      【解决方案2】:

      为@Sada93 的精彩回答添加更多内容。

      如果你想在选定的列上应用不同的颜色,那么试试这个。

      library(tidyverse)
      library(DT)
      
      
      colorbarTable = function(table, 
                               colorVarList){
      
          # TESTING
          # table = tempDf
          # vcolorVarList = list(lightblue = c('var2'), lightgreen = c('var3'), pink =  c('ID')
      
          index <- enframe(colorVarList) %>% 
              unnest %>%
              rename(color = name, colName = value) %>%
              as.data.frame
      
          dTable <- DT::datatable(table, 
                    filter ='top',
                    extensions = list('Buttons', 'FixedColumns'), 
                    options = list(
                        list(dom = 'Bfrtip',
                                   buttons = list('copy', 'print', 
                                                  list(extend = 'collection', buttons = c('csv', 'excel'), text ='Download')
                                                  )),
                        list(dom = 't', 
                             scrollX = F,
                             fixedColumns = list(leftColumns = 1, rightColumns = 2)))) 
      
        for(i in 1:nrow(index)){
      
            # TESTING 
            # i = 1   
        colName <- index[i,"colName"]; colorName <- index[i,"color"]
            dTable <-  dTable%>%
              formatStyle(colName,
                          background = styleColorBar(base::range(table[,colName]), colorName),
                          backgroundSize = '98% 88%',
                          backgroundRepeat = 'no-repeat',
                          backgroundPosition = 'center')
      
          }
      
        return(dTable)
      }
      
      
      tempDf <- data.frame(ID = c(1, 2, 3, 4, 5),
                        var1 = c('a', 'b', 'c', 'd', 'e'),
                        var2 = c(1, 1, 0, 0, 1),
                        var3 = c(1,2,3,3,5))
      
      colorbarTable(table = tempDf,
                    colorVarList = list(lightblue = c('var2'),
                                        lightgreen =  c('var3'),
                                        pink =  c('ID')))
      
      
      

      【讨论】:

        猜你喜欢
        • 2019-07-23
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-29
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多