使用Excel,您可以轻松地对单元格应用条件格式:
您是否有可能使用Shiny做类似的事情?我已经看完了这些教程,但是显然没有涉及。
例如,我想有条件地为perm
行着色runExample("02_text")
:
您可以使用jQuery有条件地格式化表格。
例如 :
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)
})
}
))
在tbody tr td:nth-child(5)
I精确nth-child(5)
To循环中,td
仅第5列中的每一个(权限)。
我们需要这样做,session$onFlushed(function() { session$sendCustomMessage(type='jsCode', list(value = script)) })
因为如果将脚本放在最前面,它将在呈现表输出之前执行,然后什么都不会格式化。
如果您想要更多格式,建议您创建css类并使用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')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句