簡體   English   中英

使用DT格式化列的單個/多個條件

[英]Single/multiple conditions for columns formating with DT

這與這個問題有關。Shiny中是否有類似的方法可以對excel中的多列進行條件格式設置,提供的解決方案效果很好,但是我在如何擴展代碼以滿足新要求方面有些棧。 因此,如果我具有以下數據框,並希望根據以下條件更改五列的背景色:

  • 對於X,Y列
  • 如果-4 <X <4並且Y <10的X,Y顏色是粉紅色
  • 否則,如果Y> X,Y的10色是藍色
  • 否則X =“”或Y =“”則X,Y的顏色為白色

  • 對於A,B,C列

  • 如果“ A” <3,則“ A”為綠色,否則為粉紅色
  • 如果“ B” <3,則“ B”為綠色,否則為粉紅色
  • 如果“ C” <3,則“ C”為綠色,否則為粉紅色

我試圖在此處擴展提供的解決方案,但是沒有用。 誰能幫助您解決這個問題。

output$contents <- renderDataTable({
    df <- data.frame(
      id = 1:10, 
      X = c(-2, 4, 40, -0.1228, 2.9, 9, 2.7, 2.7, 31, -30),
      Y = c(-18.9, -19.5, 19.6, 12, 11.1, 73, 4.3, 39, 2.5, 1.6),
      A = c(-7.3, 5.1 ,0.12, 15, 21, 1.2, -0,07, 4.3, 39, 2.5) 
      B = c(-18.9, 0.12, 15, 11.1, 73, -2, 4, 40, -19.5, 19.6)
      C = c(4.3, 39, 2.5, 1.6, -7.3, 6, 5.1 ,0.12, -0.07, 4.3)
    library(DT)
    datatable(df) %>% formatStyle(
      'A',
      target = 'cell',
      backgroundColor = styleInterval(3, c('green','pink')))
    %>% formatStyle(
      'B',
      target = 'cell',
      backgroundColor = styleInterval(3, c('green','pink'))
    )%>% formatStyle(
      'C',
      target = 'cell',
      backgroundColor = styleInterval(3, c('green','pink'))
    )

    colors <- with(df, ifelse(X > -4 & X < 4 & Y < 10, 
                              "pink", 
                              ifelse(Y > 10, 
                                     "blue", "white")))

    rgbcolors <- apply(grDevices::col2rgb(colors), 2, 
                       function(rgb) sprintf("rgb(%s)", paste(rgb, collapse=",")))
    columns <- c(2,3) # columns X and Y
    jscode <- 
      paste("function(row, data, index) {",  
            sprintf("var colors=%s;\n%s", 
                    sprintf("[%s]", 
                            paste(sprintf("'%s'", rgbcolors), collapse=", ")), 
                    paste(sprintf("$(this.api().cell(index, %s).node()).css('background-color', colors[index]);", 
                                  columns), collapse="\n")), 
            "}", sep="\n")
    datatable(df, escape=FALSE, 
              options = list(rowCallback=JS(jscode))
    )
    jscode <- "function(row, data, index) {
  var colors = ['rgb(255,192,203)', 'rgb(255,255,255)', 'rgb(0,0,255)', 'rgb(0,0,255)', 'rgb(0,0,255)', 'rgb(0,0,255)', 'rgb(255,192,203)', 'rgb(0,0,255)', 'rgb(255,255,255)', 'rgb(255,255,255)'];
  $(this.api().cell(index, 2).node()).css('background-color', colors[index]);
  $(this.api().cell(index, 3).node()).css('background-color', colors[index]);
}"

先感謝您

從...開始

datatable(df, escape=FALSE, 
              options = list(rowCallback=JS(jscode)))

並添加formatStyle

library(DT)
df <- data.frame(
  id = 1:10, 
  X = c(-2, 4, 40, -0.1228, 2.9, 9, 2.7, 2.7, 31, -30),
  Y = c(-18.9, -19.5, 19.6, 12, 11.1, 73, 4.3, 39, 2.5, 1.6),
  A = c(-7.3, 5.1 ,0.12, 15, 21, 1.2, -0,07, 4.3, 39),
  B = c(-18.9, 0.12, 15, 11.1, 73, -2, 4, 40, -19.5, 19.6),
  C = c(4.3, 39, 2.5, 1.6, -7.3, 6, 5.1 ,0.12, -0.07, 4.3)
)

colors <- with(df, ifelse(X > -4 & X < 4 & Y < 10, 
                          "pink", 
                          ifelse(Y > 10, 
                                 "blue", "white")))
rgbcolors <- apply(grDevices::col2rgb(colors), 2, 
                   function(rgb) sprintf("rgb(%s)", paste(rgb, collapse=",")))
columns <- c(2,3) # columns X and Y
jscode <- 
  paste("function(row, data, index) {",  
        sprintf("var colors=%s;\n%s", 
                sprintf("[%s]", 
                        paste(sprintf("'%s'", rgbcolors), collapse=", ")), 
                paste(sprintf("$(this.api().cell(index, %s).node()).css('background-color', colors[index]);", 
                              columns), collapse="\n")), 
        "}", sep="\n")

datatable(df, escape=FALSE, 
          options = list(rowCallback=JS(jscode))) %>% 
  formatStyle(
    'A',
    target = 'cell',
    backgroundColor = styleInterval(3, c('green','pink'))) %>% 
  formatStyle(
    'B',
    target = 'cell',
    backgroundColor = styleInterval(3, c('green','pink'))) %>% 
  formatStyle(
    'C',
    target = 'cell',
    backgroundColor = styleInterval(3, c('green','pink'))
  )

在此處輸入圖片說明

我提供的第一個解決方案可以正常工作,但是代碼不可讀。 這是一個更清潔的解決方案。

library(DT)
df <- data.frame(
  id = 1:10, 
  X = c(-2, 4, 40, -0.1228, 2.9, 9, 2.7, 2.7, 31, -30),
  Y = c(-18.9, -19.5, 19.6, 12, 11.1, 73, 4.3, 39, 2.5, 1.6),
  A = c(-7.3, 5.1 ,0.12, 15, 21, 1.2, -0,07, 4.3, 39),
  B = c(-18.9, 0.12, 15, 11.1, 73, -2, 4, 40, -19.5, 19.6),
  C = c(4.3, 39, 2.5, 1.6, -7.3, 6, 5.1 ,0.12, -0.07, 4.3)
)
jscode <- "function(settings) {
  var table = settings.oInstance.api();
  var nrows = table.rows().count();
  for(var i=0; i<nrows; i++){
    var cell1 = table.cell(i,2);
    var cell2 = table.cell(i,3);
    var X = cell1.data(); var Y = cell2.data();
    var bgcolor = 'white';
    if(X > -4 && X < 4 && Y < 10){
      bgcolor = 'pink';
    }else if(Y > 10){
      bgcolor = 'blue';
    }
    cell1.node().style.backgroundColor = bgcolor;
    cell2.node().style.backgroundColor = bgcolor;
  }
}"
datatable(df, escape=FALSE, 
          options = list(initComplete=JS(jscode))) %>% 
  formatStyle(
    'A',
    target = 'cell',
    backgroundColor = styleInterval(3, c('green','pink'))) %>% 
  formatStyle(
    'B',
    target = 'cell',
    backgroundColor = styleInterval(3, c('green','pink'))) %>% 
  formatStyle(
    'C',
    target = 'cell',
    backgroundColor = styleInterval(3, c('green','pink'))
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM