【发布时间】:2015-01-14 14:39:54
【问题描述】:
我有一个实时日志文件正在运行,它监听数据库并在顶部呈现最新更新的数据表。然而,在花了一些时间之后,我坚持如何使用 if 语句更改背景颜色,因为我不熟悉 Javascript。
1) a) 当我的“测试”列为“通过”时,如何将背景颜色更改为绿色。 b) “Aggr”时为红色 c) “Bad”时为灰色。我看过R shiny colour dataframe 和 How 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