R语言 根据条件更改数据表单元格的背景色

f45qwnt8  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(156)

我想在shiny应用程序中,当datatable第二列的每个单元格的值等于dataframe第三列同一行的单元格的值时,更改其背景色。

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
comp<-structure(list(Source = c("dates1", "dates2", "dates3", "dates4", 
                                "dates5", "dates6", "dates7"), Counts = c(12L, 15L, 17L, 10L, 
                                                                          12L, 7L, 9L), Comparison = c(15, 15, 15, 15, 15, 15, 15), Difference = c(3, 
                                                                                                                                                   0, -2, 5, 3, 8, 6)), row.names = c(NA, -7L), class = "data.frame")

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    datatableOutput("table")
  )
)

server <- function(input, output) {
  
  output$table<-renderDataTable({
    datatable(comp)
  })
}

shinyApp(ui, server)
dffbzjpn

dffbzjpn1#

js <- "
function(row, data) {
  if(data[2] == data[3]) {
    $('td:eq(2)', row).css('background-color', 'orange');
  }
}
"

datatable(
  comp, 
  options = list(
    "rowCallback" = JS(js)
  )
)

相关问题