如何使用闪亮的数据表按索引/rowname 格式化一行?

how to formaStyle a row by index /rowname with datatable in shiny?

我正在做一个闪亮的应用程序,但我很难添加一些样式和颜色。

我正在尝试为我的数据添加一些颜色table:我想为特定行着色。 该行的行名是“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 ,我终于找到了一种通用表达式来处理我所有的 table :

output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
                                    %>%formatStyle(0, target="row",backgroundColor =  styleEqual("Sum", "orange"))

我们可以在formatStyle中指定行的索引为0,并使用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)

-输出

我根据@akrun 回答的解决方案! ^^

  output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
                                        %>%formatStyle(0, target="row",backgroundColor =  styleEqual("Sum", "orange"))