闪亮的无功输入和“开始”按钮

我有一个闪亮的应用程序,我想对输入选择进行反应,并在按下“开始”按钮时显示数据表.

对于输入,我想在我的变量的“所有值”和每个值之间进行选择.

我有一些问题需要修复我的应用.

第一次尝试

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)

第二次尝试(没有工作原因的累积问题和代码似乎没有被优化)

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)

最佳答案 问题在于过滤表格.

如果未选择任何内容,则输入$filter1具有值’All_BA’,并且过滤器不返回任何值,对于其他输入也是如此.

实际上,如果选择了所有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
})

(我在第一个例子上工作).

希望这可以帮助

点赞