DT数据表R Shiny中的条件格式 [英] Conditional formatting in DT Data Table R Shiny

查看:159
本文介绍了DT数据表R Shiny中的条件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中有5个列和第1列作为字符,另四个列是数字。我正在使用DT数据表在Shiny App中显示相同的内容。现在,我需要比较每行的四个列,并对具有最大值的行单元格进行颜色编码。寻找做同样的方法。也查看了此链接 StylingCells ,但所有

代码



I have a table with 5 cols and 1st column as character and other four as numeric. I am using DT Data Table to display the same in Shiny App. Now, I need to compare each of the four cols for each row and color code the row cell which has maximum value. Looking for ways to do the same. Had a look on this link as well StylingCells but all the cols are numeric here.

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

        )

      )

    })
  }
)


推荐答案

这是我对您的问题的解决方案:

Here is my solution to your problem:

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()中使用它。

So what you need to do is to create a vector of max values. Then use it in the helper function styleEqual() inside formatStyle() as shown in the code above.

这篇关于DT数据表R Shiny中的条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆