Shiny: How to get choices of dynamic shiny input

...I have different fields of SelectInput which are dynamically generated. I am trying to get choices of each SelectInput but failed like below:

input[[ paste0("FilterField","_",rowNumber)]]$choices

getting like: NA NA NA NA.

I have a created 4 selectInput's here, I want to get choices of first dropdown. Please help me on getting this.

library(shiny)


ui = fluidPage(
  box(
    uiOutput("InputUI"),
    
    actionButton(
      inputId = "ENTER",
      style = "simple",
      size = "sm",
      label="Print Number of Choices in Console"
    )
  )
 
)
server = function(input, output) {
  filterRow=reactiveValues(
    CurrentRow=4
  )
  
  output$InputUI=renderUI({
    box(
      div(
        selectInput(
          inputId=paste("FilterField",1,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      ),
      
      div(
        selectInput(
          inputId=paste("FilterField",2,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      ),
      
      div(
        selectInput(
          inputId=paste("FilterField",3,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      ),
      
      div(
        selectInput(
          inputId=paste("FilterField",4,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      )
    )
    
  })
  
  observeEvent(input$ENTER,{
     print(input[[ paste0("FilterField","_",1)]]["choices"])
  })
  
}
shinyApp(ui,server,options=list(launch.browser=F))

I have update latest code, please help on this.

Hi,

Your problem is that input$FilterField_1 returns the selected value, not a data structure representing the selectInput tag. So input$FilterField_1 is string that does not have a choices slot. To get the choises (options in html) you will need to do some javascript magic or DOM manipulation. Here's a hint on how to do it in js, not sure how you would get it into R: https://stackoverflow.com/questions/18113495/javascript-get-list-of-all-values-in-select-box.

Maybe a better solution is to track the choices in the server code (in your code it is not clear where "choices" come from) instead of trying to get it from the rendered html.

Cheers
Steen

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.