Open 'modalDialog' dialogbox when a Tab is selected in Shinyapp

Viewed 806

I have created a Shiny app with navbarPage having 3 tabs namely 'A1', 'A2', & 'A3'. Now I wanted to create a modal-dialogbox when user select 'A2'. Below is my code:

ui <- shinyUI(fluidPage(
    wellPanel(
          navbarPage(id = "AC", "AC :",
              tabPanel(tabName = "A1", h6("A1"), fluidRow()),
              tabPanel(tabName = "A2", h6("A2"), fluidRow()),
              tabPanel(tabName = "A3", h6("A3"), fluidRow())
            )
  )
))

server <- function(input, output, session) {


observe({
   if (input$AC == "A2")  {
     showModal(modalDialog(
        title = "Important message",
        div(id = "aa", style = "width: 1100px; height: 100px;", HTML("<b>This is </b>an important message!")),
        easyClose = TRUE
      ))
  }
  })
}

shinyApp(ui, server)

However, above App doesn't open a modal-dialogbox when 2nd Tab is selected. Any pointer to the right approach will be highly appreciated.

Thanks,

1 Answers

You almost had it, the input to AC is a tab panel, with HTML coding so the input$AC=="<h6>A2</h6>", not A2

observeEvent(input$AC, {
    print(input$AC)
    if (input$AC == "<h6>A2</h6>")  {
      showModal(modalDialog(
        title = "Important message",
        div(id = "aa", style = "width: 1100px; height: 100px;", HTML("<b>This is 
        </b>an important message!")),
        easyClose = TRUE
      ))
    }else{}

Don't mind me using observeEvent() it was simply for debugging purposes

Related