Create Dropdown menu in Shiny - R using tags

12,740

You can supply a list of tags to tagList. The tags you need are option tags with value attributes You can construct these using mapply

library(shiny)

runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Enter 3 for condition', 3, 0, 10),
    conditionalPanel(condition="input.n==3",
                     div(style="display:inline-block",
                         tags$label('Menu1', `for` = 'Sample'), 
                         tags$select(id = 'Sample', class="input-small",
                                     tagList(mapply(tags$option, value = 1:10, 
                                                    paste0(letters[1:10], 1:10), 
                                                    SIMPLIFY=FALSE)))
                     ),
                     div(style="display:inline-block",
                         tags$label('Menu2', `for` = 'Sample1'), 
                         tags$select(id = 'Sample1', class="input-small",
                                     tagList(mapply(tags$option, value = 1:2, 
                                                    paste0(letters[1:2], 1:2), 
                                                    SIMPLIFY=FALSE)))
                     )
    )
    , textOutput("cond")
  ),
  server = function(input, output) {
    output$cond <- renderText({
      if(input$n == 3){
        paste0("Sample value selected =", input$Sample, " Sample1 value selected =",input$Sample1)
      }
    })
  }
))

Of course you can just use selectInput inside the div for example:

library(shiny)

runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Enter 3 for condition', 3, 0, 10),
    conditionalPanel(condition="input.n==3",
                     div(style="display:inline-block",
                         selectInput("Sample", "Choose you Input:", choices = c('a1'='1','b2'='2'))
                     ),
                     div(style="display:inline-block",
                         tags$label('Menu2', `for` = 'Sample1'), 
                         tags$select(id = 'Sample1', class="input-small",
                                     tagList(mapply(tags$option, value = 1:2, 
                                                    paste0(letters[1:2], 1:2), 
                                                    SIMPLIFY=FALSE)))
                     )
    )
    , textOutput("cond")
  ),
  server = function(input, output) {
    output$cond <- renderText({
      if(input$n == 3){
        paste0("Sample value selected =", input$Sample, " Sample1 value selected =",input$Sample1)
      }
    })
  }
))
Share:
12,740
vrajs5
Author by

vrajs5

Vishnu is a data scientist currently working at Venus Jewel. He has 11+ years of experience, out of which 8+ years in data science. He engages with CXOs, Directors, and various stakeholders on regular basis to identify and provide data-driven solutions to different practices &amp; domains and, so far, he has worked for companies like KPIT, General Mills, Cognizant, and Wipro. Vishnu contributes to projects related to data science, data analytics, data mining, natural language processing, deep learning, and operation optimization.

Updated on July 26, 2022

Comments

  • vrajs5
    vrajs5 almost 2 years

    I know using following code I can create normal dropdown menu in shiny,

    selectInput("Input1", "Choose you Input:", choices = c('a1'='1','b2'='2'))
    

    which will create following dropdown

    enter image description here

    But I am using conditionalPanel and for which I am populating kind of inline dropdownmenus something like this

    enter image description here

    I am using following code to generate these menus.

    conditionalPanel(condition="input.conditionedPanels==3",
                         div(style="display:inline-block",
                             tags$label('Menu1', `for` = 'Sample'), 
                             tags$select(id = 'Sample', class="input-small")),
                         div(style="display:inline-block",
                             tags$label('Menu2', `for` = 'Sample1'), 
                             tags$select(id = 'Sample1', class="input-small")))
    

    My problem is I am not able to add items to these dropdown menu. I tried values or options, but that did't change anything.

    I hope I have provided enough information, let me know if more information is required.