Use updatePickerInput in loop with observeEvent

Hello, I'm trying to update pickerInput choices using loop and observeEvent. Below approach works with assigning reactive values, but not updatePickerInput.

library(shiny)

ui <- fluidPage(
  fluidRow(
    column(
      width = 6,
      lapply(
        X = 1:6,
        FUN = function(i) {
          sliderInput(inputId = paste0("num", i), label = i, min = 0, max = 10, value = i)
        }
      )
    ),
    column(
      width = 6,
      lapply(
        X = 1:6,
        FUN = function(i) {
          pickerInput(inputId = paste0("pick", i), label = i, choices = seq(1, i))
        }
      )
    )
  )
)

server <- function(input, output, session){
  
  lapply(
    X = 1:6,
    FUN = function(i){
      observeEvent(input[[paste0("num", i)]], {
        updatePickerInput(
          session,
          input[[paste0("pick", i)]],
          choices = seq(1:input[[paste0("num", i)]])
        )
      })
    }
  )
  
}

shinyApp(ui = ui, server = server)

Any kind of guideline would be very much helpful.

The second argument of updatePickerInput requires the id of the input, so swap what you currently have with:

updatePickerInput(
          session,
          #input[[paste0("pick", i)]],
          paste0("pick", i),
          choices = seq(1:input[[paste0("num", i)]])
        )

It worked! Thanks a lot.

This topic was automatically closed 7 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.