Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Dropdown menu in Shiny - R using tags

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.

like image 878
vrajs5 Avatar asked Jun 05 '14 07:06

vrajs5


1 Answers

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)
      }
    })
  }
))
like image 90
jdharrison Avatar answered Sep 29 '22 00:09

jdharrison