简体   繁体   English

闪亮的DT选择行,保持单元格颜色

[英]Shiny with DT Select rows, keep cell color

I have a DT datatable that has cells colored according to a different variable. 我有一个DT数据表,其中的单元格根据不同的变量着色。 When you click on a row, it highlights values in a corresponding plot, exactly like in the example here . 单击某一行时,它会突出显示相应图表中的值,与此处的示例完全相同。 However, when you select a row, the new color that highlights the row overrides my existing colors. 但是,当您选择一行时,突出显示该行的新颜色将覆盖我现有的颜色。 I'd like for the row to be highlighted, but the individual cell to maintain its color if it was already colored. 我希望突出显示行,但如果已经着色,则单个单元格保持其颜色。

The screenshots below show what I'm getting and what I want. 下面的截图显示了我得到的和我想要的东西。 I modified Yihui's code to make a reproducible example below the screenshots. 我修改了Yihui的代码,在屏幕截图下面做了一个可重现的例子。 Any help would be appreciated! 任何帮助,将不胜感激!

有色细胞

颜色被覆盖

library(shiny)
library(DT)

ui <- fluidPage(

  title = 'Select Table Rows',

  fluidRow(
    column(6, DT::dataTableOutput('x1')),
    column(6, plotOutput('x2', height = 500))
  )


)

server <- function(input, output) {
  cars <- cars %>% 
    mutate(low_speed = ifelse(speed < 5, 1, 0))

  output$x1 <- renderDataTable({
    datatable(cars,
              options = list(columnDefs = list(list(targets = 3,
                                                    visible = FALSE)))) %>% 
      formatStyle("speed", "low_speed",
                  backgroundColor = styleEqual(c(0, 1), 
                                             c("transparent", "#E34755")))
  })

  # highlight selected rows in the scatterplot
  output$x2 <- renderPlot({
    s <- input$x1_rows_selected
    par(mar = c(4, 4, 1, .1))
    plot(cars[ ,-3])
    if (length(s)) points(cars[s, , drop = FALSE], pch = 19, cex = 2)
  })



}
shinyApp(ui, server)

You can define a CSS class for the background color ( red below) and add it to the desired cells with a rowCallback . 您可以为背景颜色定义CSS类(下面的red ),并使用rowCallback将其添加到所需的单元格。 Then add this CSS: 然后添加这个CSS:

.red {
  background-color: #e34755;
}
table.dataTable tr.selected td.red {
  background-color: #e34755 !important;
}

The app: 该应用程序:

library(shiny)
library(DT)

rowCallback <- c(
  "function(row, dat, displayNum, index){",
  "  if(dat[1] < 5){",
  "    $('td:eq(1)', row).addClass('red');",
  "  }",
  "}"
)

css <- "
.red {
  background-color: #e34755;
}
table.dataTable tr.selected td.red {
  background-color: #e34755 !important;
}
"

ui <- fluidPage(

  tags$head(
    tags$style(HTML(css))
  ),

  title = 'Select Table Rows',

  fluidRow(
    column(6, DTOutput('x1')),
    column(6, plotOutput('x2', height = 500))
  )
)

server <- function(input, output) {

  output$x1 <- renderDT({
    datatable(cars,
              options = list(
                columnDefs = list(list(targets = 3,visible = FALSE)),
                rowCallback = JS(rowCallback)
              )
    )
  })

  # highlight selected rows in the scatterplot
  output$x2 <- renderPlot({
    s <- input$x1_rows_selected
    par(mar = c(4, 4, 1, .1))
    plot(cars[ ,-3])
    if (length(s)) points(cars[s, , drop = FALSE], pch = 19, cex = 2)
  })
}

shinyApp(ui, server)

在此输入图像描述

You can make this happen with some custom CSS. 您可以使用一些自定义CSS实现此目的。 Add this code block to your fluidPage : 将此代码块添加到fluidPage

  tags$head(
    tags$style(
      HTML(
      "table.dataTable tbody tr.selected td {
       color: white !important;
       background-color: #E34755 !important;}"
      )
      )
  ),

You could also drop that CSS snippet into a standalone file and place it in the www directory alongside your app file(s). 您还可以将该CSS片段放入独立文件中,并将其放在应用程序文件旁边的www目录中。 See here for more Shiny CSS info . 在这里查看更多有光泽的CSS信息

Live Demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM