最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Filtering out Styled Rows in R Datatable - Stack Overflow

programmeradmin0浏览0评论

I am trying to implement a kind of validation system inside R shiny where rows meeting a particular criteria are flagged (highlighted in red).

The problem I face is that I cannot filter out styled rows unless I make the styling conditional (applying ifelse inside StyleRow e.g StyleRow(rows = ifelse())) where I have to specify another row to take on styling. This is not ideal as this new row in turn becomes unfilterable.

Here is an example:

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr) # added needed libraries
ui <- shinydashboard::dashboardPage(
  
  dashboardHeader(title = 'test'),
  dashboardSidebar(
    menuItem("Settings", tabName = 'info', icon = icon('gears'),badgeLabel = 'new')
  ),
  dashboardBody(
    tabItem('info',
            column(2,
                   sidebarMenu(
                     selectInput('filter', label = 'Select bcd', choices = c('Able', 'Table', 'Caleb'))
                    )
                   ),
           
            
            column(12,
                   dataTableOutput('test_dt')
                   )
            )
  )
  
)

server <- function(input, output, session){
  
  output$test_dt <- renderDataTable({
    
    df <- data.frame(abc = c(1,2,3),
                     bcd = c('Able', 'Table', 'Caleb'))%>%
      dplyr::filter(!bcd %in% input$filter)
    
    datatable(df)%>%
      formatStyle(
        columns = 'bcd',
        target = 'row',
        backgroundColor = styleRow(rows = which(df$bcd == 'Table'), 'red')
      )
  })
  
}

shinyApp(ui = ui, server = server)

I am trying to implement a kind of validation system inside R shiny where rows meeting a particular criteria are flagged (highlighted in red).

The problem I face is that I cannot filter out styled rows unless I make the styling conditional (applying ifelse inside StyleRow e.g StyleRow(rows = ifelse())) where I have to specify another row to take on styling. This is not ideal as this new row in turn becomes unfilterable.

Here is an example:

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr) # added needed libraries
ui <- shinydashboard::dashboardPage(
  
  dashboardHeader(title = 'test'),
  dashboardSidebar(
    menuItem("Settings", tabName = 'info', icon = icon('gears'),badgeLabel = 'new')
  ),
  dashboardBody(
    tabItem('info',
            column(2,
                   sidebarMenu(
                     selectInput('filter', label = 'Select bcd', choices = c('Able', 'Table', 'Caleb'))
                    )
                   ),
           
            
            column(12,
                   dataTableOutput('test_dt')
                   )
            )
  )
  
)

server <- function(input, output, session){
  
  output$test_dt <- renderDataTable({
    
    df <- data.frame(abc = c(1,2,3),
                     bcd = c('Able', 'Table', 'Caleb'))%>%
      dplyr::filter(!bcd %in% input$filter)
    
    datatable(df)%>%
      formatStyle(
        columns = 'bcd',
        target = 'row',
        backgroundColor = styleRow(rows = which(df$bcd == 'Table'), 'red')
      )
  })
  
}

shinyApp(ui = ui, server = server)
Share Improve this question edited Feb 7 at 12:36 dog 3,0312 silver badges14 bronze badges asked Feb 7 at 10:57 Cebo_ SACebo_ SA 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can use styleEqual() which takes the value to match against ('Table' in this case) and the style to apply ('red' in this case). Your table will remain filterable and the style will only be applied, if the value "Table" is found in "bcd".

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)

ui <- dashboardPage(
  dashboardHeader(title = 'test'),
  dashboardSidebar(
    menuItem(
      "Settings",
      tabName = 'info',
      icon = icon('gears'),
      badgeLabel = 'new'
    )
  ),
  dashboardBody(tabItem(
    'info', column(2, sidebarMenu(
      selectInput(
        'filter',
        label = 'Select bcd',
        choices = c('Able', 'Table', 'Caleb')
      )
    )), column(12, dataTableOutput('test_dt'))
  ))
)

server <- function(input, output, session) {
  output$test_dt <- renderDataTable({
    df <- data.frame(abc = c(1, 2, 3),
                     bcd = c('Able', 'Table', 'Caleb')) %>%
      dplyr::filter(!bcd %in% input$filter)
    
    datatable(df) %>%
      formatStyle('bcd',
                  backgroundColor = styleEqual('Table', 'red'),
                  target = 'row')
  })
}

shinyApp(ui = ui, server = server)
发布评论

评论列表(0)

  1. 暂无评论