shiny::runApp(list(
ui = basicPage(
tags$head(tags$script(type = "text/javascript",
"$(document).ready(function() {
alert(document.getElementById(
'test').value);
});")),
verbatimTextOutput("test")
),
server = function(input, output, session) {
output$test <- renderPrint({
return("test")
})
}
))
shiny::runApp(list(
ui = bootstrapPage(
tags$head(tags$script(type = "text/javascript", "$(document).ready(function() {
$('td').each(function() {
var color = $(this).text();
if (color == ' green ') {
$(this).css('background-color', '#0c0');
}
else if (color == ' red ') {
$(this).css('background-color', '#f00');
}
})
});")),
tableOutput("my_dataframe")
),
server = function(input, output) {
output$my_dataframe <- renderTable({
data.frame("Brand ID"=1:4,"Client1"=c("red", "green", "green", "green"),
"Client2"=c("green", "red", "green", "red"))
})
}
)
)
$(document).ready(function() {
$(document).on('click', 'td', function(evt) {
var color = $(this).text();
if (color == ' green ') {
$(this).css('background-color', '#0c0');
}
else if (color == ' red ') {
$(this).css('background-color', '#f00');
}
})
});
I think the easiest way is to render your table with renderUI and to send the javascript in this renderUI with session$sendCustomMessage.See here an example of session$sendCustomMessage : https://groups.google.com/d/topic/shiny-discuss/oJo3SiCfXTs/discussion
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
script <- "$('td').each(function() {
var cell = $(this).text();
if (cell == ' green ') {
$(this).css('background-color', '#0c0');
}
else if (cell == ' red ') {
$(this).css('background-color', '#f00');
}
})"
shiny::runApp(list(
ui = bootstrapPage(
tags$head(tags$script(HTML('Shiny.addCustomMessageHandler("jsCode", function(message) { eval(message.value); });'))),
tableOutput("test")
),
server = function(input, output, session) {
session$onFlushed(function() {
session$sendCustomMessage(type='jsCode', list(value = script))
})
output$test <- renderTable({
data.frame(c1 = c("green", "red", "green", "green", "red"))
})
}
))
I’m glad I found this thread but ended up doing something a little bit different, so here’s an alternative in case someone might find it useful: you can listen to the shiny:value event and when it occurs, prevent the default behavior (= just rendering the output from the server) and update the UI manually, incorporating your custom code:
$(document).on("shiny:value", function(e) {
if (e.name == "mytable") { // mytable is the name / id of the output element
e.preventDefault();
$("#mytable")
.removeClass("shiny-output-error") // get rid of potential previous error styling
.html(e.value) // render the output from the server
.find("td")
.each(colorize); // run custom javascript on the rendered output
}
});