Shiny ShowModal Issue

Hi folks,

I’m working on an app which uses modalDialogs, one for accepting T+Cs and one for a message box displayed at a click of a button. The first modalDialog is a splash screen that is coded in ui.R and displayed on start-up.
The 2nd is activated with a click of a button in the server function. However, when you press the button it displays the first message and not the second – see attached reproducible example!

If I comment out the first ModalDialog (“modalDialog(h3("This is the 1st ModalDialog…...”, etc) in the UI then the button behaves as expected!
Is this a bug or am I doing something stupid here?

library(shiny)

ui <- fluidPage(
  titlePanel("Multiple Modal Dialog Example Issue App"),
  
    mainPanel(
      modalDialog(h3("This is the 1st ModalDialog"),title = "This is the 1st ModalDialog", size = "l",  easyClose = FALSE),
      actionButton("goButton", "Display New ModalDialog", class = "btn-success")
      
    )
)


server <- function(input, output) {
  
  observeEvent(input$goButton,{
    showModal(modalDialog(h3("This is the 2nd ModalDialog"),title = "This is the 2nd ModalDialog", size = "l",  easyClose = FALSE))
  }
  )
  
}


shinyApp(ui = ui, server = server)

I wont pretend its the best solution, but its a solution.

library(shiny)

ui <- fluidPage(
  titlePanel("Multiple Modal Dialog Example Issue App"),

  mainPanel(
    actionButton("goButton", "Display New ModalDialog", class = "btn-success")
  )
)


server <- function(input, output) {
  modal_def <- reactiveVal(
    list(
      modal_content = h3("This is the 1st ModalDialog"),
      modal_title = "This is the 1st ModalDialog",
      modal_trigger =0 
    )
  )

  observeEvent(input$goButton, {
    modal_def(list(
      modal_content = h3("This is the 2nd ModalDialog"),
      modal_title = "This is the 2nd ModalDialog",
      modal_trigger = modal_def()$modal_trigger + 1
    ))
  })

  observeEvent(modal_def(), {
    print("triggered")
    print(modal_def()$modal_content)
    showModal(modalDialog(modal_def()$modal_content,
      title = modal_def()$modal_title
    ))
  })
}


shinyApp(ui = ui, server = server)

Many thanks nirgrahamuk this looks to work although I'm not sure why the issue arose in the first place. Do you think this a bug or the intended behaviour? If the former, I may add this as an issue on https://github.com/rstudio/shiny/issues

Thanks,

Wayne

Unlike other shiny output objects that have some form of unique id, modals don't seem to, or rather share one under the hood ?

Ive raised an issue: https://github.com/rstudio/shiny/issues/3290

Let's see what they say...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.