With Excel, you can easily apply conditional formatting over cells:
Is there any chance you can do something like this with Shiny? I've gone through the tutorials, but this apparently is not covered.
For instance, I'd like to conditionally colour the perm
row in runExample("02_text")
:
You can conditionnal formatting your table using jQuery.
For 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(
ui = basicPage(
tags$head(tags$script(HTML('Shiny.addCustomMessageHandler("jsCode", function(message) { eval(message.value); });'))),
tableOutput("view")
),
server = function(input, output, session) {
session$onFlushed(function() {
session$sendCustomMessage(type='jsCode', list(value = script))
})
output$view <- renderTable({
head(rock, n = 20)
})
}
))
In tbody tr td:nth-child(5)
I precise nth-child(5)
To loop on each td
of the 5th column only (perms).
We need session$onFlushed(function() {
session$sendCustomMessage(type='jsCode', list(value = script))
})
because if you put the script in the head, it will be executed before the table output rendered and then nothing will be formatting.
If you want more formatting I suggest you to create css classes and use addClass
:
### In the UI :
tags$head(tags$style(
".greenCell {
background-color: #0c0;
}
.redCell {
background-color: #f00;
}"))
### In th script
### use .addClass instead of .css(...)
$(this).addClass('greenCell')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With