Create Dropdown menu in Shiny - R using tags
drop-down-menu, html, r, shiny
Solution
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)
}
})
}
))
Problem
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 But I am using conditionalPanel and for which I am populating kind of inline dropdownmenus something like this 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.