Observe Shiny module event form main shiny app

Hello,
I have been trying to create a contact module for my Shiny app where on a button click, it sends an e-mail. My problem comes from the fact that I can't figure out how to observe such event as the button is in the contact module UI and the observer is in the module server function.
The only way I found to make it work is to place the action button in the main app UI and add an observer event in the main app server which then calls the module, but it breaks the whole purpose of a module as I want the module to be self-contained and be able to manage the button click by itself and have the button inside its own UI.

Here is reproducible code:

# Main app UI
ui = fluidPage(
   contactPageUI("contact")
)

# Main app server
server = function(input, output, session) {
   # how to observe the module event from the main app??
   contact = callModule(contactPage, "contact")
}


# The module UI
contactPageUI = function(id) {
   ns = NS(id)
   
   tagList(
      fluidRow(
         fileInput(ns("attached_file"), label = "Attach a file to the mail.", multiple = T, placeholder = "No file selected."),
         textAreaInput(ns("mail_body"), placeholder = "Text area*", label = "", width = "500px", height = "300px")
      ),
      fluidRow(
         actionButton("send_mail", label = "Send e-mail", icon = icon("envelope", class = "color:#004a59", lib = "font-awesome")),
         div("Automatic mails require Outlook, if you don't have Outlook please use to contact mail adresses.", style = "font-size:0.7em;")
      )
   )
}

# Module server
contactPage = function(input, output, session) {
   ns <- session$ns

   observeEvent(input$send_mail, {
      print("E-mail sent.")
   })
}

shinyApp(ui = ui, server = server)

Could you please give me some hints about how to detect the click on the button in the module from the main app?

nah nevermind, found the way to make it work.
I forgot a ns() in the module UI and then just added an observe() in the main app server:

# Main app UI
ui = fluidPage(
   contactPageUI("contact")
)

# Main app server
server = function(input, output, session) {
  observe({callModule(contactPage, "contact")})
}


# The module UI
contactPageUI = function(id) {
   ns = NS(id)
   
   tagList(
      fluidRow(
         fileInput(ns("attached_file"), label = "Attach a file to the mail.", multiple = T, placeholder = "No file selected."),
         textAreaInput(ns("mail_body"), placeholder = "Text area*", label = "", width = "500px", height = "300px")
      ),
      fluidRow(
         actionButton(ns("send_mail"), label = "Send e-mail", icon = icon("envelope", class = "color:#004a59", lib = "font-awesome")),
         div("Automatic mails require Outlook, if you don't have Outlook please use to contact mail adresses.", style = "font-size:0.7em;")
      )
   )
}

# Module server
contactPage = function(input, output, session) {
   ns <- session$ns

   observeEvent(input$send_mail, {
      print("E-mail sent.")
   })
}

shinyApp(ui = ui, server = server)

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