0
votes

I know there are already some questions regarding this topics but I am still struggling with my own project.

In my shiny there are three select input widgets with should be reactive to each other. Only combinations that make sense should be possible.

the Code is now update to use mtcars. some variables names are weird now but this should matter.

library(shiny)

ui <- fluidPage(
  selectInput(inputId = "INseason", label = "Season", choices = c("ALL", sort(unique(mtcars$am), decreasing = TRUE)), selected = NULL),
  selectInput(inputId = "INcategory",  label = "Category", choices = c("ALL", sort(unique(mtcars$gear))), selected = NULL),
  selectInput(inputId = "INteams", label = "Team", choices = c("ALL", sort(unique(mtcars$carb))), selected = NULL)

)

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

  rv <- reactiveValues()
  rv$data <- mtcars

  observe({

    if(input$INseason != "ALL") {
      rv$data <- filter(rv$data, am == input$INseason)
    }
    if(input$INcategory != "ALL") {
      rv$data <- filter(rv$data, gear == input$INcategory)
    }
    if(input$INteams != "ALL") {
      rv$data <- filter(rv$data, carb == input$INteams)
    }

  })

  observe({
    updateSelectInput(session, "INseason", choices = c("ALL", sort(unique(rv$data$am), decreasing = TRUE)))
    updateSelectInput(session, "INcategory", choices = c("ALL", sort(unique(rv$data$gear))))
    updateSelectInput(session, "INteams", choices = c("ALL", sort(unique(rv$data$carb))))
  })

}

shinyApp(ui, server)

With this code i am not able to select something because "ALL" is already preselected.

Thank you for your help!

Best

1
you should either provide the sample dataset for Shot_Data.rds or use open source data such as mtcarsPork Chop
thanks for your hint. i changed the code to mtcarsiPasc

1 Answers

0
votes

You have to pass the selected argument back to updateSelectInput, otherwise it will default to the first category always (even if something was selected).

Replace the last block with:

  observe({
    updateSelectInput(session, "INseason", 
                      choices = c("ALL", sort(unique(rv$data$am), decreasing = TRUE)),
                      selected = if(is.null(input$INseason)) "ALL" else input$INseason)
    updateSelectInput(session, "INcategory", 
                      choices = c("ALL", sort(unique(rv$data$gear))),
                      selected = if(is.null(input$INcategory)) "ALL" else input$INcategory)
    updateSelectInput(session, "INteams",
                      choices = c("ALL", sort(unique(rv$data$carb))),
                      selected = if(is.null(input$INteams)) "ALL" else input$INteams)
  })

(this code can be tidier, but it works!)