簡體   English   中英

DT數據表R Shiny中的條件格式

[英]Conditional formatting in DT Data Table R Shiny

我有一個表有5個cols和第1列作為字符,其他四個作為數字。 我正在使用DT數據表在Shiny App中顯示相同的內容。 現在,我需要比較每行的四個列中的每一個,並對具有最大值的行單元進行顏色編碼 尋找方法來做同樣的事情。 看看這個鏈接以及StylingCells,但所有的cols都是數字。

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

        )

      )

    })
  }
)

以下是我的問題解決方案:

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()里面的輔助函數styleEqual()使用它,如上面的代碼所示。

暫無
暫無

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

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