firmo23
firmo23

Reputation: 8404

Create a selectInput() with maximum number of choices based on a numericInput()

I have a simple shiny app:

#ui.r
navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(
               uiOutput("book1")
             ),
             mainPanel(
               uiOutput("book10")
             )
           )))
#server.
library(shiny)
library(DT)
server <- function(input, output,session) {
  output$book1<-renderUI({
    numericInput("bk1", 
                 "Items in test", 
                 value = 1,
                 min = 1)
  })
  output$book10<-renderUI({

    selectInput("bk10", 
                "Select Items", 
                choices=1:10000,multiple =T,selected = 1)
  })
}

In the sidebar I have a numricInput() which I want to use as limit for the number of choices displayed in the selectInput() in the main panel. For example if the numericImput() is set to 5 then the selectInput() may display choices only 5 choices like: 21,22,23,45,67.

Upvotes: 1

Views: 1383

Answers (1)

A. Suliman
A. Suliman

Reputation: 13125

server <- function(input, output,session) {
        output$book1<-renderUI({
          numericInput("bk1", 
                       "Items in test", 
                       value = 1,
                       min = 1)
        })

        output$book10<-renderUI({
          selectizeInput(
            "bk10", "Max number of items to select", choices =1:1000,multiple =T,selected = 1,
            options = list(maxItems = input$bk1))
          #selectizeInput(
          #  "bk10", "Select Items", choices =1:1000,multiple =T,selected = 1,
          #  options = list(maxOptions = input$bk1))
        })

      }

For more options, you can check here. I hope it helps.

Upvotes: 2

Related Questions