我想知道你是否可以提供帮助。我有一个简单的闪亮应用程序,它呈现一个dataTable,我想根据一些标准突出显示行的颜色。
以下是我可以运行的代码:
库(有光泽)
runApp(list(
ui = shinyUI(fluidPage(
sidebarPanel(),
mainPanel(dataTableOutput("Table") )
)),
server = function(input, output, session) {
output$Table<- renderDataTable({
Data<-data.frame(RowNumber= c(1,2,3,4,5,6),Type= c("R","E", "R","E","R","G"), YN =c("N","N","Y","N","Y","N"),P = c(500,100,500,900,0,900))
print(Data[Data$P== 0,])
Data
})
}
))
我根本没有使用过CSS,但我想根据一些标准为数据表中的每一行着色。标准是:
(1) If "Type" = R AND "YN"= N AND "P" >0 then color the entire row orange
(2) If "Type" = R AND "YN"= Y AND "P" >0 then color the entire row blue
(3) If "Type" = R AND "YN"= Y AND "P" = 0 then color the entire row yellow
当然,这种着色必须与内置的排序和搜索功能等一起使用。
我已经看过这个解决方案,但无法让它发挥作用:
How to change Datatable row background colour based on the condition in a column, Rshiny
那里的作者提到了&#34;回调&#34; DataTable的功能。你知道在这种情况下如何使用它吗?
你能帮忙吗?
答案 0 :(得分:4)
您需要一个在创建表格后调用的回调函数来为行着色。
以下是您可以做的事情:
library(shiny)
ui <- shinyUI(fluidPage(
sidebarPanel(),
mainPanel(dataTableOutput("Table"))
))
server <- function(input, output, session) {
output$Table<- renderDataTable({
Data<-data.frame(RowNumber= c(1,2,3,4,5,6),Type= c("R","E", "R","E","R","G"), YN =c("N","N","Y","N","Y","N"),P = c(500,100,500,900,0,900))
Data},
options = list(rowCallback = I(
'function(row, data) {
if (data[1] == "R" & data[2]=="N" & data[3]>0 )
$("td", row).css("background", "orange");
else if (data[1] == "R" & data[2]=="Y" & data[3]>0 )
$("td", row).css("background", "blue");
else if (data[1] == "R" & data[2]=="Y" & data[3]==0 )
$("td", row).css("background", "yellow");
}'
))
)
}
shinyApp(ui = ui, server = server)
如果您想了解有关回调的更多信息,可以查看this