2017-08-29 38 views
1

我有一個表,其中有5列和第1列作爲字符,其他4列作爲數字。我正在使用DT數據表在Shiny App中顯示相同的內容。現在,我需要比較每行的四列和顏色代碼中具有最大值的行單元格。尋找方法來做同樣的事情。看看這個鏈接,以及StylingCells,但所有的列在這裏是數字。DT數據表R中的條件格式發光

代碼

entity <- c('entity1', 'entity2', 'entity3') 
value1 <- c(21000, 23400, 26800) 
value2 <- c(21234, 23445, 26834) 
value3 <- c(21123, 234789, 26811) 
value4 <- c(27000, 23400, 26811) 
entity.data <- data.frame(entity, value1, value2, value3, value4) 

header <- dashboardHeader() 
sidebar <- dashboardSidebar() 
body <- dashboardBody(DT::dataTableOutput("entity.dataTable")) 

shinyApp(
    ui = dashboardPage(header, sidebar, body), 
    server = function(input, output) { 
    output$entity.dataTable <- renderDataTable({ 
     DT::datatable(
     entity.data, 
     selection = "single", 
     filter = 'bottom', 

     extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'), 
     rownames = FALSE, 
     options = list(
      dom = 'Bfrtip', 
      searching = T, 
      pageLength = 25, 
      searchHighlight = TRUE, 
      colReorder = TRUE, 
      fixedHeader = TRUE, 
      filter = 'top', 
      buttons = c('copy', 'csv', 'excel', 'print'), 
      paging = TRUE, 
      deferRender = TRUE, 
      scroller = TRUE, 
      scrollX = TRUE, 
      scrollY = 550 

     ) 

    ) 

    }) 
    } 
) 

回答

0

這裏是我的解決問題的方法:

library(shinydashboard) 
library(DT) 
library(magrittr) 

entity <- c('entity1', 'entity2', 'entity3') 
value1 <- c(21000, 23400, 26800) 
value2 <- c(21234, 23445, 26834) 
value3 <- c(21123, 234789, 26811) 
value4 <- c(27000, 23400, 26811) 
entity.data <- data.frame(entity, value1, value2, value3, value4) 

# Create a vector of max values 
max_val <- apply(entity.data[, -1], 1, max) 

header <- dashboardHeader() 
sidebar <- dashboardSidebar() 
body <- dashboardBody(DT::dataTableOutput("entity.dataTable")) 

shinyApp(
    ui = dashboardPage(header, sidebar, body), 
    server = function(input, output) { 
    output$entity.dataTable <- renderDataTable({ 
     DT::datatable(
     entity.data, 
     selection = "single", 
     filter = 'bottom', 
     extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'), 
     rownames = FALSE, 
     options = list(
      dom = 'Bfrtip', 
      searching = T, 
      pageLength = 25, 
      searchHighlight = TRUE, 
      colReorder = TRUE, 
      fixedHeader = TRUE, 
      filter = 'top', 
      buttons = c('copy', 'csv', 'excel', 'print'), 
      paging = TRUE, 
      deferRender = TRUE, 
      scroller = TRUE, 
      scrollX = TRUE, 
      scrollY = 550 
     ) 
    ) %>% # Style cells with max_val vector 
     formatStyle(
     columns = 2:5, 
     backgroundColor = styleEqual(levels = max_val, values = rep("yellow", length(max_val))) 
    ) 
    }) 
    } 
) 

所以,你需要做的是創造最大價值的載體。然後將其用於內的幫助函數formatStyle(),如上面的代碼所示。

+0

謝謝你的迴應。您提出的當前解決方案是對列之間的最大值進行顏色編碼,以及我期望獲得跨行的最大值並對它們進行顏色編碼。例如,根據樣本數據 - 第一行最大值:27000,第二行最大值:234789 \t,第三行最大值:26834。請您指導如何實現相同。 – string

+0

是的,這很容易做到。只需將apply函數的MARGIN參數更改爲1.我已更改代碼以反映此更改。 – jsb

+0

謝謝:) :)它工作完美。 max_val < - apply(entity.data [,-1],2,max) - 獲取最大列值和max_val < - apply(entity.data [,-1],1,max) - 獲取最大列值。只是添加了這個以使其他人有用。 – string