How to change Datatable background color based on condition in column, Rshiny

I have a real-time log file that listens on a database and displays the latest updates from above. However, after spending some time on this, I was stuck on how to change the background color using the if statement, since I am not familiar with Javascript.

1) a) How to change the background color to green when my “Test” column is “Pass”. b) red when its "Aggr" c) and gray when its "bad." I looked at R a brilliant color graphic file and How to have conditional formatting of data frames in R Shiny? and I can change scipt to something like this

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'); } })" 

But this is only once. I also looked at this r brilliant: highlight some cells , however the library gives me an Error: package 'ReporteRsjars' could not be loaded error Error: package 'ReporteRsjars' could not be loaded , and I cannot install this package to go this route.

Possible solutions:

i) I can change the log table in textoutput and change the colors there using the shinyBS library or some other tools, theres a great example here is ChatRoom in the Rshiny gallery.

ii) I can go with the googlevis package, but I would have had problems re-publishing the table for each iteration (same as here, but not as “noticeable”).

2) . How can I display my data output only when a new point is added to it. For instance. I do not want to retype the data if nothing has changed?

Thanks in advance...

My sample code below

 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)) 
+3
source share
1 answer

You can add a custom message that you can call using the session$onFlushed . To save the example, I removed the formatting and additional tabs. First a script and a call to brilliant. We equate the note to " Pass " , and not to "Pass" , etc., since xtable adds an additional interval:

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

and application

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

When you add back to the formatting and additional tabs, it looks like this:

enter image description here

+2
source

Source: https://habr.com/ru/post/1244803/


All Articles