4

I have a shiny App, I wanted to be reactive for the inputs choice and show the datatable when I press the "Go" button. For inputs I want to have the choice between "All value" of my variable and each value. I have some problem to fix my app.

First try

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)

second try (didn't work cause of rectivity problem and the code doesnt seem to be optimized)

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)
Mostafa90
  • 1,674
  • 1
  • 21
  • 39
  • Solution here : https://stackoverflow.com/questions/44570404/updating-filters-in-shiny-app/44639701#44639701 – Mostafa90 Jun 19 '17 at 23:20

1 Answers1

1

The problem lies in the filtering of the table. If nothing is selected input$filter1 has value 'All_BA', and the filter return no value, and similarly for the other inputs.

In fact the filter works if all 3 input values are selected.

Change it to:

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
})

(I worked on the first example).

Hope this helps

GGamba
  • 13,140
  • 3
  • 38
  • 47
  • Thanks your answer it seems to wrok but i have a problem in filters they are not updated when i choose specific filters can you help me pls – Mostafa90 Jun 14 '17 at 08:16