闪亮的反应输入和 "Go" 按钮

Shiny with reactive inputs and "Go" button

我有一个闪亮的应用程序,我想对输入选择做出反应并在我按下 "Go" 按钮时显示数据表。 对于输入,我想在变量的 "All value" 和每个值之间进行选择。 我有一些问题需要修复我的应用程序。

第一次尝试

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

# my data
bdd <- tibble(BA = rep(LETTERS, 2), MA = as.character(1:52),
              YES = paste(BA, MA, sep = ""))



ui <- fluidPage(
  titlePanel("BA"),
  column(4,
         uiOutput("filter1"),
         uiOutput("filter2"),
         uiOutput("filter3"),
         actionButton("button", "GO")),
  column(8,
         DT::dataTableOutput("my_table"))
)

server <- function(input, output, session) {


  All_BA <- reactive({
    bdd %>% distinct(BA) 
  })
  # my reactives inputs for filter 1
  output$filter1 <- renderUI({
    selectInput("filter1", "Filtre numéro 1", 
                choices = c("All_BA", bdd %>% select(BA)))
  })

  All_MA <- reactive({
    bdd %>% filter(BA %in% input$filter1) %>%
      distinct(MA)
  }) 
  # my reactives inputs for filter 2
  output$filter2 <- renderUI({
    selectInput("filter2", "Filtre numéro 2",
                choices = c("All_MA", bdd %>% filter(BA %in% input$filter1) %>% select(MA)), 
                selected = "All_MA")
  })

  All_Y <- reactive({
    bdd %>% filter(BA %in% input$filter1 |
                     MA %in% input$filter2) %>% distinct(YES) 
  })
  # my reactives inputs for filter 3
  output$filter3 <- renderUI({
    selectInput("filter3", "Filtre numéro 3", 
                choices =  c("All_Y", bdd %>% filter(BA %in% input$filter1,
                                                     MA %in% input$filter2) %>% select(YES)),
                selected = "All_Y")
  })

  df <- eventReactive(input$button, {
    bdd %>% filter(BA %in% input$filter1,
                   MA %in% input$filter2,
                   YES %in% input$filter3)
  })


  output$my_table <- DT::renderDataTable({
    df()

  })

}

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

第二次尝试(没有成功,原因是 rectivity 问题,代码似乎没有优化)

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

# my data
bdd <- tibble(BA = rep(LETTERS, 2), MA = as.character(1:52),
              YES = paste(BA, MA, sep = ""))



ui <- fluidPage(
  titlePanel("BA"),
  column(4,
         uiOutput("filter1"),
         uiOutput("filter2"),
         uiOutput("filter3"),
         actionButton("button", "GO")),
  column(8,
         DT::dataTableOutput("my_table"))
)

server <- function(input, output, session) {


  All_BA <- reactive({
    bdd %>% distinct(BA) 
  })
  # my reactives inputs for filter 1
  if(input$filter1 == "All_BA"){
    bdd <- reactive({
      bdd %>%
        filter(BA %in% All_BA())
    })
  }else{
    bdd <- reactive({
      bdd %>%
        filter(BA %in% input$filter1)
    })
  }
  output$filter1 <- renderUI({
    selectInput("filter1", "Filtre numéro 1", 
                choices = c("All_BA", bdd() %>% select(BA)))
  })

  All_MA <- reactive({
    bdd() %>% filter(BA %in% input$filter1) %>%
      distinct(MA)
  }) 
  # my reactives inputs for filter 2
  if(input$filter2 == "All_MA"){
    bdd2 <- reactive({
      bdd() %>%
        filter(MA %in% All_MA())
    })
  }else{
    bdd2 <- reactive({
      bdd() %>%
        filter(MA %in% input$filter2)
    })
  }
  output$filter2 <- renderUI({
    selectInput("filter2", "Filtre numéro 2",
                choices = c("All_MA", bdd2() %>% select(MA)), 
                selected = "All_MA")
  })

  All_Y <- reactive({
    bdd2 %>% filter(BA %in% input$filter1 |
                      MA %in% input$filter2) %>% distinct(YES) 
  })
  # my reactives inputs for filter 3
  if(input$filter3 == "All_Y"){
    bdd3 <- reactive({
      bdd2() %>%
        filter(YES %in% All_Y())
    })
  }else{
    bdd3 <- reactive({
      bdd2() %>%
        filter(YES %in% input$filter3)
    })
  }
  output$filter3 <- renderUI({
    selectInput("filter3", "Filtre numéro 3", 
                choices =  c("All_Y", bdd3() %>% select(YES)),
                selected = "All_Y")
  })

  df <- eventReactive(input$button, {
    bdd %>% filter(BA %in% input$filter1,
                   MA %in% input$filter2,
                   YES %in% input$filter3)
  })


  output$my_table <- DT::renderDataTable({
    df()

  })

}

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

问题出在table的过滤上。 如果未选择任何内容 input$filter1 具有值 'All_BA',并且过滤器 return 没有值,对于其他 inputs.

也是如此

事实上,如果选择了所有 3 个输入值,过滤器就会起作用。

改为:

df <- eventReactive(input$button, {

    res <- bdd
    if(input$filter1 != "All_BA")
        res <- res %>% filter(BA %in% input$filter1)
    if(input$filter2 != "All_MA")
        res <- res %>% filter(MA %in% input$filter2)
    if(input$filter3 != "All_Y")
        res <- res %>% filter(MA %in% input$filter3)
    res
})

(我处理了第一个示例)。

希望对您有所帮助