Search code examples
rpopupshinyshinybs

R Shiny BSModal Popup to Show Selected Input


I would like to create a popup that displays the current dropdown choice. My code seems to work for the first click, but when clicked a second time, the first modal appears, and I am unable to close the popup. Example code is pasted below, and any suggestions would be greatly appreciated.

library(shinyBS)
shinyApp(

ui = basicPage(
actionButton("show", "Show modal dialog"),
uiOutput("Box1"),
uiOutput("modal")
),

server = function(session, input, output) {

observeEvent(input$show,{
output$text <- renderText(input$select1)
output$modal <- renderUI({
bsModal(paste("model", input$show, sep = ''), "Choice", "show", size =     "small", textOutput("text"))
})
toggleModal(session,paste("model", input$show, sep = ''), "close")
})

output$Box1 <- renderUI({
selectizeInput("select1","Select",c("A","B","C"))
})

})

Solution

  • Simplifying the code makes it work:

    shinyApp(
      ui = basicPage(
        actionButton("show", "Show modal dialog"),
        selectizeInput("select1","Select",c("A","B","C")),
        bsModal("model", "Choice", "show", size ="small", textOutput("text"))
      ),
    
      server = function(session, input, output) {
        output$text <- renderText(input$select1)
      })