Best way to pass action button input from one module to another

Here's what I came up with — the returned value of an action button already increments each time you push it, so I don't think you need any additional gymnastics. I also switched to modern module style, and gave the modules informative names:

library(shiny)
moduleServer <- function(id, module) {
  callModule(module, id)
}

button_ui <- function(id) {
  actionButton(NS(id, "btn"), label = "Go")
}

button_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    reactive(input$btn)
  })
}

module_server <- function(id, button) {
  moduleServer(id, function(input, output, session) {
    modal <- modalDialog(
      "Let's start a dialog",
      footer = actionButton(NS(id, "close_modal"), label = "Close"),
      textOutput(NS(id, "fruit"))
    )
    observeEvent(input$close_modal, {
      removeModal()
    })
    observeEvent(button(), {
      showModal(modal)
    })
  })
}

ui <- fluidPage(
  fluidRow(
    button_ui("mod1")
  )
)
server <- function(input, output, session) {
  button <- button_server("mod1")
  module_server("mod_2", button)
}
shinyApp(ui, server)
2 Likes