Plato_of_the_Wilds
Plato_of_the_Wilds

Reputation: 11

selectInput choices dependent on another selectInput for R Shiny Flexdashboard

I have an interesting problem with a shiny app I'm trying to build using Flexdashboard. Essentially I have a sidebar with two selectInput drop downs. I would like the choices in the second selectInput to be dependent upon the choice in the first. My approach thus far has been to switch between two dynamically generated options for the second input. This Dynamic UI shiny example is essentially exactly what I'm trying to do, but I think I'm having trouble translating from the server/ui format to a Rmarkdown format.

At this point I have the following simplified code that produces the first selectInput, but instead of a second selectInput it's just drawing a block of html code.

---
title: "Shiny Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---

Tab1
===============================================================================  

Inputs {.sidebar}
-------------------------------------------------------------------------------

```{r}
selectInput('type', label = 'Input 1',
            choices = c('choice1', 'choice2'))

reactive({
  switch(input$type,
         'choice1' = selectInput('select2', 
                                 label = 'Input 2',
                                 choices = c('choice3', 'choice4')),
         'choice2' = selectInput('select3', label = 'Input 3',
                                 choices = c('choice5', 'choice6')))
  })
```

Column {data-width=600}
-------------------------------------------------------------------------------

### Test Output

```{r}

```

Test picture

I have a feeling that I'm missing something very simple, but I can't seem to find anything that deals with this problem in the context of Flexdashboard. Any advice would be greatly appreciated!

Upvotes: 1

Views: 1610

Answers (1)

YBS
YBS

Reputation: 21349

Try this

ui <- basicPage(
  selectInput('type', label = 'Input 1',
              choices = c('choice1', 'choice2')),
  uiOutput("mychoices")
)

server <- function(input,output){

  output$mychoices <- renderUI({
    switch(input$type,
           'choice1' = selectInput('select2', 
                                   label = 'Input 2',
                                   choices = c('choice3', 'choice4')),
           'choice2' = selectInput('select3', label = 'Input 3',
                                   choices = c('choice5', 'choice6')))
  })
  
}

shinyApp(ui,server)

Upvotes: 2

Related Questions