testname123
testname123

Reputation: 1091

Dynamic UI Shiny Implementation

I have the following dataframe:

a<-rep(c("cat","dog","bird"),each=5)
b<-letters[1:length(a)]
c<-data.frame("pet"=a,"level"=b)

I'd like to make a shiny app that has a pull down menu for selecting pet and then have a dynamic set of checkboxes that appear beneath that have corresponding values of level for checkbox options.

So, selecting cat would bring up a checkbox group of a,b,c,d,e and then selecting dog would change those checkboxes to only show f,g,h,i,j, etc.

Thanks for the help

Upvotes: 2

Views: 338

Answers (1)

Julien Navarre
Julien Navarre

Reputation: 7840

You can use the updateCheckboxGroupInput function inside an observer (?observe The observe function "observe" input$petand and will automatically re-execute when input$pet changes, and then update the checkbox group).

For example :

a<-rep(c("cat","dog","bird"),each=5)
b<-letters[1:length(a)]
c<-data.frame("pet"=a,"level"=b)

runApp(list(
  ui = pageWithSidebar(
    headerPanel("Example"),

    sidebarPanel(
      selectInput("pet", "Select a pet", choices = levels(c$pet), selected = levels(c$pet)[1]),
      tags$hr(),
      checkboxGroupInput('levels', 'Levels', choices = c$level[c$pet == levels(c$pet)[1]])
    ),

    mainPanel()
  ),
  server = function(input, output, session) {

    observe({
      pet <- input$pet

      updateCheckboxGroupInput(session, "levels", choices = c$level[c$pet == pet])
    })
  }
))

Upvotes: 1

Related Questions