mch*_*hen 12 formatting conditional r shiny
使用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)
})
}
))
Run Code Online (Sandbox Code Playgroud)
在tbody tr td:nth-child(5)我精确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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6691 次 |
| 最近记录: |