我正在做一个闪亮的应用程序,我有麻烦,以采取一些风格和颜色。
我正在尝试在datatable上添加一些颜色:我想给一个特定的行着色。此行的行名为"Sum“。
我还想将列"Sum“涂上颜色,并成功地做到了这一点:因此,我可以为一个名为"Sum”的特定列着色,如下所示:
output$data_1<-renderDataTable(datatable(data(),options = list(dom = 't',pageLength=100))%>%formatStyle("Sum", backgroundColor = "orange")但我不知道我怎么能用我的争吵做同样的事?
编辑:我的“求和”行并不总是数据中的最后一行。
谢谢你的帮助!)
编辑:
一个简单的例子:
library(shiny)
library(DT)
data_example<-data.frame("A"=c(40,10,20,10,5,85),"B"=c(10,20,10,20,5,65),"Sum"=c(50,30,30,30,10,150), row.names = c("1","2","3", "4", "5", "Sum"))
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Example"),
dataTableOutput("table")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange"))
}
# Run the application
shinyApp(ui = ui, server = server)

编辑:
多亏了akrun,我终于找到了一种通用的表达式,可以用我所有的表来完成它:
output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
%>%formatStyle(0, target="row",backgroundColor = styleEqual("Sum", "orange"))发布于 2021-12-10 18:24:35
我们可以将行的索引指定为0在formatStyle中,并使用styleEqual来匹配和替换创建的“cols1”
server <- function(input, output) {
v1 <- row.names(data_example)
cols1 <- ifelse(v1 =='Sum','orange','')
output$table <- renderDataTable(datatable(data_example)%>%
formatStyle(0, target = "row",
backgroundColor = styleEqual(v1, cols1)))
}
shinyApp(ui = ui, server = server)-output

发布于 2021-12-10 18:55:36
我的答案是@akrun答案!^^
output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
%>%formatStyle(0, target="row",backgroundColor = styleEqual("Sum", "orange"))https://stackoverflow.com/questions/70308017
复制相似问题