r - DT 数据表 R Shiny 中的条件格式

标签 r shiny dt

我有一个表,其中 5 列和第一列作为字符,其他四列作为数字。我正在使用 DT 数据表在 Shiny App 中显示相同的内容。现在,我需要比较每行的四个列中的每一个和 颜色编码具有最大值的行单元格 .寻找方法来做同样的事情。也看过这个链接 StylingCells但这里所有的列都是数字。

代码

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)))
      )
    })
  }
)

所以你需要做的是创建一个最大值向量。然后在辅助函数中使用它 styleEqual()formatStyle()如上面的代码所示。

关于r - DT 数据表 R Shiny 中的条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45936714/

相关文章:

r - 在绘图上添加一个框

r - 合并两个 data.frames 并用 df2 的值替换 df1 某些列的值

r - 数据如何从 react 性 Shiny 表达式传递到 ggvis 图?

r - 使用 R DataTable 包添加加载微调器

r - 根据预定义单词列表突出显示单元格内容中字符串的一部分

r - Shiny + DT : Single Cell Selection

r - 设置证书验证位置时出错,install_github

R Shiny - 将用户输入(文本和上传的图像)作为参数传递以生成报告

r - stateSave 不保留 Shiny 数据表中的过滤器

r - 将 posixlt 作为新列添加到数据框