【问题标题】:Highlight rows in Shiny DataTable using CallBack or something eles使用 CallBack 或其他方法突出显示 Shiny DataTable 中的行
【发布时间】:2015-02-06 06:13:49
【问题描述】:

我想知道你是否可以帮忙。我有一个简单的闪亮应用程序,它呈现一个数据表,我想根据一些标准突出显示行的颜色。

这是我可以运行的代码:

图书馆(闪亮)

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

我根本没有使用过闪亮的 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 的“回调”功能。你知道在这种情况下如何使用它吗?

你能帮忙吗?

【问题讨论】:

    标签: css r


    【解决方案1】:

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

    你可以这样做:

    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

    【讨论】:

      猜你喜欢
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多