rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.86k forks source link

actionButton not working in modal inside shiny module despite having ns() #3723

Open abby-pfingsten opened 2 years ago

abby-pfingsten commented 2 years ago

I have this big app that is modularized - two of the tabs have a very similar structure wherein there is a datatable that is displayed, and the last column you are able to click which populates a modal. In the modal, you are able to select some stuff, and there is an actionButton to trigger saving some responses to a database. In tab 1, everything is working exactly as expected. In tab 2, everything WAS working at some point, but for a reason I cannot figure out, the actionButton stopped being triggered altogether, despite no direct changes having been made to this part of the code.

This is the part of the code which produces the modal (which is exactly the same as it is in tab 1, where it is working):

` modalDialog(

title = "Assign Advisor to Branch",

easyClose = FALSE,

size = "l",

footer = tagList(

  actionButton(ns("submit_assignment"),

    label = "Assign",

    style = "minimal",

    class = "modal-assign-button"

  ),

  modalButton("Cancel")

),

modal_data

)`

This is the part of the code which SHOULD be triggered when you click "Assign", but it is not. The browser is never triggered:

`observeEvent(input$submit_assignment, {

    browser()

    if (shiny::isTruthy(input$mgr_reportees) || shiny::isTruthy(input$search_names)) {

      removeModal()

      names <- names_from_bubbles()

. . .

})`

I'm running R version 3.6.0.

hedsnz commented 1 year ago

You will need to create a minimal reproducible example, because there's not enough information here to debug your issue. For the record, here's a minimal example showing that an actionButton in a module can trigger code in an observer, within a module:

library(shiny)

mod1_ui <- function(id) {
    ns <- NS(id)
    tagList(
        modalDialog(
            actionButton(ns("btn"), "Test")
        )
    )
}

mod1_server <- function(id) {
    moduleServer(id, function(input, output, session) {
        observeEvent(input$btn, {
            message("btn hit")
        })
    })
}

ui <- fluidPage(
    mod1_ui("mod1")
)

server <- function(input, output, session) {
    mod1_server("mod1")
}

shinyApp(ui, server)