在另一个post中,假设表格不是renderUI函数的一部分,则回答相同的问题。R Shiny:renderUI中的表格条件格式
在下面的示例中,我试图调整相同的解决方案(使用JQuery),其中我想要有条件地格式化的表格属于renderUI函数。
library(shiny)
library(datasets)
script <- "$('tbody tr td:nth-child(5)').each(function() {
var cellValue = $(this).text();
if (cellValue > 50) {
$(this).css('background-color', '#0c0');
}
else if (cellValue <= 50) {
$(this).css('background-color', '#f00');
}
})"
shinyServer(function(input, output, session) {
session$onFlushed(function() {
session$sendCustomMessage(type='jsCode', list(value = script))
})
output$view <- renderTable({
head(rock, n = 20)
})
output$Test1 <- renderUI({
list(
tags$head(tags$script(HTML('Shiny.addCustomMessageHandler("jsCode", function(message) { eval(message.value); });'))),
tableOutput("view")
)
})
})
shinyUI(fluidPage(
tabsetPanel(
tabPanel("Test1",uiOutput("Test1")),
tabPanel("Test2")
)
))
在这个小示例的有条件的格式化并不适用于表
只是完美!我希望我可以以某种方式帮助你,但是看起来我不太可能知道你不知道的东西 – Christos 2014-10-03 13:10:05
@Christos以及我没有意识到'session $ onFlushed',直到你问你的问题,所以非常感谢;) – jdharrison 2014-10-03 13:18:38