【问题标题】:How to change Datatable row background colour based on the condition in a column, Rshiny如何根据列中的条件更改数据表行背景颜色,Rshiny
【发布时间】:2015-01-14 14:39:54
【问题描述】:

我有一个实时日志文件正在运行,它监听数据库并在顶部呈现最新更新的数据表。然而,在花了一些时间之后,我坚持如何使用 if 语句更改背景颜色,因为我不熟悉 Javascript。

1) a) 当我的“测试”列为“通过”时,如何将背景颜色更改为绿色。 b) “Aggr”时为红色 c) “Bad”时为灰色。我看过R shiny colour dataframeHow to have conditional formatting of data frames in R Shiny? 我可以将 scipt 修改为类似的东西

script <- "$('tbody tr td:nth-child(1)').each(function() {

          var cellValue = $(this).text();

          if (cellValue == "Pass") {
            $(this).parent().css('background-color', 'green');
          }
          else if (cellValue == "Aggr") {
            $(this).parent().css('background-color', 'red');
          }
          else if (cellValue == "Bad") {
            $(this).parent().css('background-color', 'grey');
          }

        })"

但这只会执行一次。我也查看了这个r shiny: highlight some cells,但是库给了我一个错误Error: package ‘ReporteRsjars’ could not be loaded,我也无法安装那个包。

可能的解决方案:

i) 我可以使用shinyBS 库或其他一些工具将我的日志表更改为文本输出并在那里更改颜色,这里有一个很好的例子,即 Rshiny 画廊中的ChatRoom

ii) 我可以使用googlevis 包,但是每次迭代时我都会面临重新打印表格的问题(与此处所做的相同,但它不是“引人注目的”)。

2) 如何仅在向其中添加新点时才呈现数据表输出。例如。如果没有任何变化,我不想再次重新打印数据表?

提前谢谢你...

我的示例代码如下

rm(list = ls())
library(shiny)
options(digits.secs=3) 

test_table <- cbind(rep(as.character(Sys.time()),2),rep('a',2),rep('b',2),rep('b',2),rep('c',2),rep('c',2),rep('d',2),rep('d',2),rep('e',2),rep('e',2))
colnames(test_table) <- c("Time","Test","T3","T4","T5","T6","T7","T8","T9","T10")

ui =navbarPage(inverse=TRUE,title = "Real-Time Logs",
               tabPanel("Logs",icon = icon("bell"),
                        mainPanel(htmlOutput("logs"))),
               tabPanel("Logs 2",icon = icon("bell")),
               tabPanel("Logs 3",icon = icon("bell")),
               tags$head(tags$style("#logs {height:70vh;width:1000px;!important;text-align:center;font-size:12px;}")),
               tags$style(type="text/css", "#logs td:nth-child(1) {height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(2) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(3) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(4) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(5) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(6) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(7) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(8) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(9) {width:70px;height:20px;font-size:12px;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(10) {width:70px;height:20px;font-size:12px;text-align:center}")
)
server <- (function(input, output, session) {
  autoInvalidate1 <- reactiveTimer(1000,session)

  my_test_table <- reactive({
    autoInvalidate1()
    other_data <- rbind(c(as.character(Sys.time()),(sample(c("Pass","Aggr","Bad"))[1]),round(c(rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1)),2)),  
                        (c(as.character(Sys.time()),(sample(c("Pass","Aggr","Bad"))[1]),round(c(rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1)),2))))
    test_table <<- rbind(apply(other_data, 2, rev),test_table)
    as.data.frame(test_table) 
  })
  output$logs <- renderTable({my_test_table()},include.rownames=FALSE)

})

runApp(list(ui = ui, server = server))

【问题讨论】:

    标签: javascript css r shiny


    【解决方案1】:

    您可以添加可以使用session$onFlushed 方法调用的自定义消息。为了保持示例简洁,我删除了格式和额外的选项卡。首先是脚本并调用闪亮。注意我们等同于 " Pass " 而不是 "Pass" 等,因为 xtable 似乎增加了额外的间距:

    library(shiny)
    options(digits.secs=3) 
    script <- "
    els = $('#logs tbody tr td:nth-child(2)');
    console.log(els.length);
    els.each(function() {
              var cellValue = $(this).text();
              if (cellValue == \" Pass \") {
                $(this).parent().css('background-color', 'green');
              }
              else if (cellValue == \" Aggr \") {
                $(this).parent().css('background-color', 'red');
              }
              else if (cellValue == \" Bad \") {
                $(this).parent().css('background-color', 'grey');
              }
            });"
    test_table <- cbind(rep(as.character(Sys.time()),2),rep('a',2),rep('b',2),rep('b',2),rep('c',2),rep('c',2),rep('d',2),rep('d',2),rep('e',2),rep('e',2))
    colnames(test_table) <- c("Time","Test","T3","T4","T5","T6","T7","T8","T9","T10")
    

    和应用程序

    ui =navbarPage(inverse=TRUE,title = "Real-Time Logs",
                   tabPanel("Logs",icon = icon("bell"),
                            mainPanel(
                              htmlOutput("logs"))
                            , tags$script(sprintf('
                              Shiny.addCustomMessageHandler("myCallback",
                                function(message) {
                                     %s
                                });
                              ', script)
                            )
                            )
    )
    server <- (function(input, output, session) {
      autoInvalidate1 <- reactiveTimer(3000,session)
      my_test_table <- reactive({
        autoInvalidate1()
        other_data <- rbind(c(as.character(Sys.time()),(sample(c("Pass","Aggr","Bad"))[1]),round(c(rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1)),2)),  
                            (c(as.character(Sys.time()),(sample(c("Pass","Aggr","Bad"))[1]),round(c(rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1)),2))))
        test_table <<- rbind(apply(other_data, 2, rev),test_table)
        session$onFlushed(function(){
          session$sendCustomMessage(type = "myCallback", "some message")
        })
        as.data.frame(test_table) 
      })
      output$logs <- renderTable({my_test_table()},include.rownames=FALSE)
    })
    
    runApp(list(ui = ui, server = server))
    

    当您重新添加格式和额外选项卡时,它看起来像:

    【讨论】:

    • 你很好,为你喝彩!鉴于您过去对数据表的回答有多少,我一直在等您!
    • 抱歉最近比较忙。你是一个非常有趣的问题。我认为使用数据表和renderDataTable 可能会更容易,您可以使用回调功能等,而不必通过自定义消息创建自己的。
    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多