1

I have a button in one module. When I click the button a panel should appear. This panel is placed in another module. I tried to pass button's 'click' action between modules using some reactivity but my code doesn't work properly. Here is a workable example:

library(shiny)
library(shinyjs)

# UI #

mod_btn_UI <- function(id) {
    
    ns <- NS(id)
    actionButton(ns("btn"), "Click me!")
    
}


mod_btn_server <- function(id){
    moduleServer(id, function(input, output, session) {
        
        btnPanel <- eventReactive(input$btn,{
            return()
        })
    })
}


mod_body_UI <- function(id) {
    ns <- NS(id)
    
    shinyjs::useShinyjs()
    shinyjs::hidden(absolutePanel(ns("panel"),
             tagList(shinyWidgets::actionBttn(inputId = "XYZ", icon = icon("chart-line")))))
    
}

# Server #

mod_body_server <- function(id, btnOne){
    moduleServer(id, function(input, output, session) {
        
        observeEvent(btnOne(), {
            shinyjs::toggle(id = "panel")
        })
    })
}


# App #

ui <- fluidPage(
    
    shinyjs::useShinyjs(),

    tagList(
    mod_btn_UI("test-btn"),
    mod_body_UI("test-body")
    )
)

server <- function(input, output, session) {
   btnVal <- mod_btn_server("test-btn")
   mod_body_server("test-body", btnOne = btnVal$btnPanel)
}


shinyApp(ui = ui, server = server)

I tried various methods to pass button 'click' to another module but it doesn't work. I also checked this eventReactive in shiny module but in my example I don't want to return value as output, just make some GUI to appear.

mustafa00
  • 751
  • 1
  • 7
  • 28
  • Perhaps the answer [here](https://community.rstudio.com/t/best-way-to-pass-action-button-input-from-one-module-to-another/60089/4) can help you. – YBS Feb 11 '21 at 14:20
  • I saw this post. I also tried to use this pattern but this example is a bit different than mine, I guess. I'm new to shiny and modules that's why it seems a little complex to me. – mustafa00 Feb 11 '21 at 14:43

1 Answers1

2

Your btn pass is working. Also, shinyjs::toggle is now working. Try this

library(shiny)
library(shinyjs)

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

# UI #
mod_btn_UI <- function(id) {

  ns <- NS(id)
  tagList(
    actionButton(ns("btn"), "Click me!")
  )
}


mod_btn_server <- function(id){
  moduleServer(id, function(input, output, session) {

    # btnPanel <- eventReactive(input$btn,{
    #   cars ## return()
    # })
    b1 <- reactive(input$btn)

  })
}


mod_body_UI <- function(id) {
  ns <- NS(id)

  shinyjs::useShinyjs()
  div(id = "advanced" ,
      tagList(
        shinyjs::hidden(plotOutput(ns("plot2")))
      ))
}

# Server #

mod_body_server <- function(id, btnOne){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    output$plot2 <- renderPlot({hist(rnorm(500))})
    
    observeEvent(btnOne(), {
      shinyjs::toggle(id = "plot2")   ## toggle is now working
    })

  })
}


# App #

ui <- fluidPage(

  shinyjs::useShinyjs(),
  tagList(
    mod_btn_UI("test-btn"),
    mod_body_UI("test-body")
  )
)

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

  btnVal <- mod_btn_server("test-btn")
  #mod_body_server("test-body", btnOne = btnVal$btnPanel)
  mod_body_server("test-body", btnVal )

}

shinyApp(ui = ui, server = server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • It works, thank you but there's one issue - I need to toggle some UI e.g. panel or chart. So on each click it should show/hide. It's crucial for me, that's why I used shiny::toggle. Is there any way to make it work? – mustafa00 Feb 18 '21 at 20:40
  • Thanks! What's the reason for using `btnTwo` in `mod_body_server <- function(id, btnOne, btnTwo)`? And when I click button first time it doesn't work, I need to click second time and then it starts working, why is that? – mustafa00 Feb 19 '21 at 17:36
  • 1
    Sorry for the oversight. I have removed btnTwo. Also, code is updated to generate alternatively. – YBS Feb 19 '21 at 18:45
  • Great! Thank you for your kind help! Now I can move forward with my app:) – mustafa00 Feb 19 '21 at 19:02