How to toggle between modules?

Hi,
I have a button in one module and some UI part which I want to toggle (using this button) in second module. I'm using shinyjs::toggleto do so but seems it doesn't work here. When I use it without modules it works but when I' using modules then it doesn't work. Reactive expression passing button 'click' works properly, I checked on some other examples. Here is my workable example:

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) {
        
        return(
            list(
                btn1 = reactive({input$btn})
            )
        )        
    })
}


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


# Server #

mod_body_server <- function(id, btnOne){
    moduleServer(id, function(input, output, session) {
        ns <- session$ns
        
        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", btnVal$btn1 )
    
}

shinyApp(ui = ui, server = server) 

Even if I wrap id ="panel" in ns("panel") it still doesn't work. I need to toggle somehow this panel UI, just show/hide every time I click the button. Do you have an idea how to solve it and why shinyjs::toggle doesn't work?

in mod_body_ui use ns() on panel


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

  shinyjs::useShinyjs()
  shinyjs::hidden(absolutePanel(
    id = ns("panel"),
    tagList(
      shinyWidgets::actionBttn(inputId = ns("XYZ"), icon = icon("chart-line"))
    )
  ))
}
1 Like

Thanks! It works perfectly:)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.