css - 使用 CallBack 或其他方式突出显示 Shiny DataTable 中的行

标签 css r

我想知道你是否可以帮忙做点什么。我有一个呈现数据表的简单 Shiny 应用程序,我想根据某些条件突出显示行的颜色。

这是我的代码,您可以运行它:

图书馆( Shiny )

runApp(list(
  ui = shinyUI(fluidPage(
    sidebarPanel(),
    mainPanel(dataTableOutput("Table")  )
  )),
  server = function(input, output, session) {
    output$Table<- renderDataTable({ 
      Data<-data.frame(RowNumber= c(1,2,3,4,5,6),Type= c("R","E", "R","E","R","G"), YN =c("N","N","Y","N","Y","N"),P = c(500,100,500,900,0,900))
      print(Data[Data$P== 0,])
      Data
    })
  }
))

我完全没有在 shiny 中使用过 CSS,但我想根据某些标准为数据表中的每一行着色。标准是:

(1) If "Type" = R AND "YN"= N AND "P" >0 then color the entire row orange 
(2) If "Type" = R AND "YN"= Y AND "P" >0 then color the entire row blue
(3) If "Type" = R AND "YN"= Y AND "P" = 0 then color the entire row yellow

当然,这种着色必须与内置的排序和搜索功能等一起使用。

我见过这个解决方案,但无法让它工作:

How to change Datatable row background colour based on the condition in a column, Rshiny

那里的作者提到了 DataTable 的“回调”功能。你知道在这种情况下如何使用它吗?

你能帮忙吗?

最佳答案

您需要一个在创建表后调用的回调函数来为行着色。

以下是您可以执行的操作:

library(shiny)
        ui <- shinyUI(fluidPage(
                sidebarPanel(),
                mainPanel(dataTableOutput("Table"))
        ))
        server <- function(input, output, session) {
                output$Table<- renderDataTable({ 
                        Data<-data.frame(RowNumber= c(1,2,3,4,5,6),Type= c("R","E", "R","E","R","G"), YN =c("N","N","Y","N","Y","N"),P = c(500,100,500,900,0,900))
                        Data},
                        options = list(rowCallback = I(
                                'function(row, data) {
        if (data[1] == "R" & data[2]=="N" & data[3]>0 )
          $("td", row).css("background", "orange");
        else if (data[1] == "R" & data[2]=="Y" & data[3]>0 )
          $("td", row).css("background", "blue");
        else if (data[1] == "R" & data[2]=="Y" & data[3]==0 )
          $("td", row).css("background", "yellow");
      }'
                        ))
                )
        }

shinyApp(ui = ui, server = server)

如果您想了解有关回调的更多信息,可以查看 this

关于css - 使用 CallBack 或其他方式突出显示 Shiny DataTable 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359626/

相关文章:

javascript - 尝试应用自定义滚动条

twitter-bootstrap - Bootstrap 4 中列的高度相等

R:LaTex 的 t.test 输出

r - DT 和 Shiny : Formatting numbers in a datatable with filters

html - 为什么我的 div 会相互重叠?

javascript - jquery .css 脚本不工作

html - 如何只增加一行中的一个字的字号?

java - JAR 文件中的 JRI(Java/R 接口(interface))程序可以在没有安装 R 的系统上运行吗?

c - Rcpp代码错误?

r - 在 32 位 Ubuntu 上安装 RHadoop