Change your call to session$onFlushed
to call your function every time shiny
flushes the reactive system by adding the argument once = FALSE
:
session$onFlushed(function() {
session$sendCustomMessage(type='jsCode', list(value = script))
}, once = FALSE)
in a self contained example:
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');
}
})"
runApp(list(server = function(input, output, session) {
session$onFlushed(function() {
session$sendCustomMessage(type='jsCode', list(value = script))
}, FALSE)
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")
)
})
}
, ui = fluidPage(
tabsetPanel(
tabPanel("Test1",uiOutput("Test1")),
tabPanel("Test2")
)
))
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…