Marton Kovacs
Marton Kovacs

Reputation: 123

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

I am working on a larger shiny app and I would like to include a module that works as a menu. In this module, you can choose between other standalone modules and activate one with an action button click. The chosen module will pop up as a shiny modal.

However, I cannot find a good way to pass the action button input from one module to another, because if I pass it as a reactive value it will only work once but not the second time after closing the module. I believe this is because the observeEvent does not record any change in the passed reactive value.

Do you have any suggestions on how to fix this problem? Do I need a totally different approach?

Here is a working example:

# Load packages
library(shiny)

# mod_1 ui
# This modules task is to pass the button press to mod_3 in order to activate a modal there.
mod_1_ui <- function(id){

  ns <- NS(id)
  tagList(
    actionButton(ns("actionbtn_1"), label = "actionbtn_1")
  )
}

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

    fruit <- reactiveVal(value = NULL)

    observeEvent(input$actionbtn_1, {
      fruit("banana")
    })

    return(list(
      activate_1 = reactive({fruit()})
      ))
}

# mod_2 has no ui
# This modules' task is to recieve the action button press from mod_1 and activate the modal if it is pressed.
mod_2 <- function(input, output, session, activate_1, activate_2){

  modal <- function() {
    ns <- session$ns

    modalDialog(
      footer = actionButton(ns("close_modal"), label = "Close modal"),
      textOutput(ns("fruit"))
    )
  }

  observeEvent(activate_1(), {
    if(activate_1() == "banana") {
      showModal(modal())}   

    })

  observeEvent(input$close_modal, {

    removeModal()
  })

  output$fruit <- renderText({
    paste("first mod:", activate_1())
  })

}

# app ui
app_ui <- 
    fluidPage(
      fluidRow(
      mod_1_ui("mod_1")
      )
    )

# app server
app_server <- function(input, output,session) {
  rv <- callModule(mod_1, "mod_1")
  callModule(mod_2, "mod_2", activate_1 = rv$activate_1)
}

# running the app
shinyApp(app_ui, app_server)

Upvotes: 1

Views: 972

Answers (1)

Related Questions