簡體   English   中英

R-“所有”值的閃亮過濾器

[英]R - Shiny Filter for 'All' values

有沒有辦法在Shiny App上放置過濾器,但是Shiny知道是否沒有選擇顯示所有數據的方法? 例如,使用ggplot2 diamonds數據,我已經建立了以下內容:

library(ggplot2)
library(shiny)
library(dplyr)
diamonds <- diamonds

#Unique cut dropdown
cut <- sort(unique(diamonds$cut))

#Unique color
color <- sort(unique(diamonds$color))

#Unique clarity
clarity <- sort(unique(diamonds$clarity))

#Shiny App

ui <- fluidPage(
  titlePanel("diamonds"),
  fluidRow(
  column(3,
       selectizeInput("CutSelect", "Cut", cut, selected = NULL, multiple = TRUE),
       br(),
       selectInput("ColorSelect", "Color", color),
       br(),
       selectInput("ClaritySelect", "Clarity", clarity)
),
tableOutput("results")
)
)
server <- function(input, output) {
output$results <- renderTable({
filtered <- 
  diamonds %>%
  filter(cut == input$CutSelect,
         color == input$ColorSelect,
         clarity == input$ClaritySelect)
filtered
})
}

shinyApp(ui = ui, server = server)

生成的應用程序如下所示:

在此處輸入圖片說明

在不清除數據的情況下,是否有辦法在過濾器中添加“全部”運算符,使應用程序知道即使該維度有一個過濾器,在某些情況下“全部”也會派上用場?

對於剪切,具體來說,如果我將selectizeInput留為空白,則希望將其作為“全部”或“無過濾器”類型處理。

樂意提供更多有用的信息。

謝謝。

我建議在顯示/過濾數據時使用DT DT具有內置的過濾器選項,使您可以過濾表中的一個或多個值或值范圍。 您可以將過濾器放在表格的頂部或底部。 這是滿足您目標的代碼:

library(ggplot2)
library(shiny)
library(DT)
diamonds <- diamonds

#Shiny App

ui <- fluidPage(
           titlePanel("diamonds"),
        fluidRow(
           dataTableOutput("results")
        )
      )
server <- function(input, output) {
    output$results <- DT::renderDataTable(filter='top',{
       diamonds
    })
}

shinyApp(ui = ui, server = server)

空的selectInput返回NULL因此只有在selectInput不為NULL (即已選擇一個或多個值)的情況下,才可以進行過濾:

server <- function(input, output) {
  output$results <- renderTable({
    filtered <- diamonds
    if (!is.null(input$CutSelect)) {
      filtered <- filtered %>% filter(cut == input$CutSelect)
    }
    if (!is.null(input$ColorSelect)) {
      filtered <- filtered %>% filter(color == input$ColorSelect)
    }
    if (!is.null(input$ClaritySelect)) {
      filtered <- filtered %>% filter(clarity == input$ClaritySelect)
    }
    filtered
  })
}

這樣,如果selectInput為空( NULL ),則不會對該維度執行過濾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM