- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个实时日志文件在运行,它监听数据库并在顶部呈现最近更新的数据表。然而,在花了一些时间之后,我卡在了如何使用 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 将我的日志表更改为文本输出并在那里更改颜色库或其他一些工具,这里有一个很好的例子 ChatRoom在 Rshiny 画廊。
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))
最佳答案
您可以添加自定义消息,您可以使用 session$onFlushed
方法调用该消息。为了使示例简洁,我删除了格式和额外的标签。首先是脚本并调用 shiny。注意我们等同于 "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))
当您重新添加格式和额外的标签时,它看起来像:
关于javascript - 如何根据列中的条件更改数据表行背景颜色,Rshiny,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976860/
我使用shinyDirChoose保存了用户定义文件夹的路径。现在我想从该用户的文件夹上传文件,但我不知道该怎么做。1)全部在服务器端?2)以某种方式将文件路径提供给fileInput? 这就是我构建
这个问题在这里已经有了答案: Image not showing in Shiny app R (4 个回答) 4年前关闭。 我在 Shiny 应用程序中显示图像时遇到问题。代码保存在桌面中,其中我还
我有一个实时日志文件在运行,它监听数据库并在顶部呈现最近更新的数据表。然而,在花了一些时间之后,我卡在了如何使用 if 语句更改背景颜色上,因为我不熟悉 Javascript。 1) a) 当我的“测
我想在我的 Rshiny 应用程序中加入自定义字体。我有一种预感,代码会放在 tags$style 中,但还没有包含它的实际代码。 示例代码: ui % formatPercentage('a', 0
你好,我正在尝试使用 DT 格式化输出。但是,它显示错误:“数据”必须是二维的(例如数据框或矩阵)。如果有人能提供帮助,我将不胜感激! 下面是从数据结构 (a1) 创建的示例。 data: a1 in
你好,我正在尝试使用 DT 格式化输出。但是,它显示错误:“数据”必须是二维的(例如数据框或矩阵)。如果有人能提供帮助,我将不胜感激! 下面是从数据结构 (a1) 创建的示例。 data: a1 in
我希望在我的应用程序中构建暗模式。我意识到我们可以使用 bs_theme() 实现这一目标,但我的应用程序在外部 css 文件中有更多设置,当我使用 bs_theme() 时无法读取这些设置。 我想要
我有一个输出图,它依赖于名为 datasetInput 的 react 函数。每当我更改输入变量时,输出函数都会更新,但我在客户端上看到的图并没有最终改变。我输出了 ggplot 用来生成绘图的数据,
目前我有一个 R Shiny 的应用程序,运行它我打开 RStudio 并执行 setwd("C:/Users/Me/Desktop/R/ShinyProject2") library(shiny)
我计划在我的 RShiny 仪表板上有几个 verbatimTextOutputs。因此,为了帮助用户,我想通过在它们上面添加标题/名称来使它们与众不同。 但是,verbatimTextOutputs
我已经构建了一个 RShiny 小程序。现在,我希望 friend 们能够使用它,而无需安装 RStudio 或处理代码。 我正在考虑类似于 Java 小程序的东西,它可以包装在 .jar 文件中,并
我需要一些关于如何在 RShiny 中正确地将查询发送到我的数据库的说明... 我构建了一个应用程序,任何人都可以在其中创建一个帐户,然后在将这些行保存到我的数据库之前在数据框中写入一些信息。 该应用
我想在鼠标悬停或单击事件上显示一个放大的弹出图像,该图像显示在 rhandsontable 单元格中,类似于所示 here在 DT 表中。 我想显示在创建的表中显示的图像的弹出图像,如下所示: lib
我读过一些主题,说不可能用 Shiny Package 重置 actionButton 的值,但我找不到任何技巧来解决我的问题。 我想使用以下代码删除主面板中的文本和按钮: library(shiny
我将如何构建响应式 UI 来响应具有不同数据输入的 reactiveFileReader? 我有兴趣将一个 reactiveFileReader 集成到一个应用程序中,该应用程序在数据中绘制组图并逐组
我正在尝试在运行 rCharts 和 Shiny 的应用程序中复制此 ( jsfiddle ) highcharts 功能。我想要一个用户事件(更改文本框中的值)将绘图带添加到已渲染的绘图中(或者理想
正在使用的 R Shiny 代码是: library(shiny) library(shinyBS) ui <- fluidPage( headerPanel( list(tags$head(t
我是 Docker 的新手,我想创建一个图像,以便部署一个 Shiny 的应用程序,该应用程序使用“网状”包获取 python 脚本。 有什么地方可以让我开始构建这个 Docker 镜像或在一些现有镜
我正在学习我想要/正在尝试制作的与篮球相关的数据可视化 Web 应用程序的全栈开发。考虑到我的技术技能,我有一个关于解决我的数据问题的最佳方法的问题。 几个月前,我使用 R 创建了 following
我是一名优秀的程序员,十分优秀!