简体   繁体   English

如果值出现在另一组中,则闪亮的DT高亮显示单元格

[英]Shiny DT Highlight Cells if Value Appears in Another Set

Issue: 问题:

I have a data frame where row A is the names of people in my organization. 我有一个数据框,其中A行是我组织中人员的名字。 I have a separate data frame that is a subset of row A in the original table. 我有一个单独的数据框,它是原始表中A行的子集。 I would like to highlight all rows in the first data table that match names in the second table. 我想突出显示第一个数据表中与第二个表中的名称匹配的所有行。 Essentially, I have two sets. 本质上,我有两套。 Set A and Set B. Both are names, I would like to highlight the data table for all names in Set A that match Set B. However, I keep getting an error: length(levels) must be equal to length(values) 集合A和集合B。两者都是名称,我想突出显示集合A中与集合B匹配的所有名称的数据表。但是,我不断收到错误消息: length(levels) must be equal to length(values)

How would I avoid receiving this error? 我如何避免收到此错误?

Reproducible Example: 可重现的示例:

I have a data frame of mtcars. 我有一个mtcars数据框。 I am filtering the mtcars dataset based on a slider input for mpg. 我正在根据mpg的滑块输入过滤mtcars数据集。 I would like to highlight the data frame of mtcars that meet the filtering criteria. 我想强调符合过滤条件的mtcar的数据框。 In effect, this would mean highlighting the output table for all observations where the mpg are <= the slider input mpg. 实际上,这意味着要突出显示所有观测值的输出表,其中mpg <=滑块输入mpg。

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Highlight Cell Test (Sets)"),

   sidebarLayout(
     sidebarPanel = 'side',
     sliderInput('slider', 'slider input', 1, 30, 20)),

      # Show a plot of the generated distribution
      mainPanel(
         dataTableOutput("test")
      )
   )

# Define server logic required to draw a histogram
server <- function(input, output) {


  subset <- reactive({
    mtcars %>%
      filter(mpg <= input$slider)
  })

  output$test <- DT::renderDataTable(
    mtcars %>%
      DT::datatable(
        options = list(
          dom = 'ftipr',
          searching = TRUE
        ) %>%
          formatStyle(
            'test',
            background = styleEqual(
              (subset()$mpg %in% mtcars$mpg), 'lightgreen'))
      )
  )

}

# Run the application
shinyApp(ui = ui, server = server)

Any help is much appreciated. 任何帮助深表感谢。 Thanks in advance. 提前致谢。

You can do this via rowCallback like so: 您可以像这样通过rowCallback

library(shiny)
library(dplyr)
library(DT)
fnc <- JS('function(row, data, index, rowId) {','console.log(rowId)','if(rowId >= ONE && rowId < TWO) {','row.style.backgroundColor = "lightgreen";','}','}')

ui <- fluidPage(

  # Application title
  titlePanel("Highlight Cell Test (Sets)"),

  sidebarLayout(
    sidebarPanel = 'side',
    sliderInput('slider', 'slider input', 1, 30, 16)),

  # Show a plot of the generated distribution
  mainPanel(
    dataTableOutput("test")
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  subset <- reactive({
    mtcars %>% filter(mpg <= input$slider)
  })

  Coloring <- eventReactive(subset(),{
    a <- which(subset()$mpg %in% mtcars$mpg)
    print(a)
    if(length(a) <= 0){
      return()
    }
    fnc <- sub("ONE",a[1],fnc)
    fnc <- sub("TWO",max(a),fnc)
    fnc
  })


  output$test <- DT::renderDataTable(
    mtcars %>%
      DT::datatable(options = list(dom = 'ftipr',searching = TRUE,pageLength = 20, scrollY = "400px",rowCallback = Coloring()))
  )
}

shinyApp(ui = ui, server = server)

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

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