Search code examples
rshinyshinymodules

Having difficulty getting an actionButton in a nested Shiny module to work


In the following modularized shiny app the insertBtn_outer button works okay, but I am struggling to get the insertBtn_inner button to work. Many thanks for any insights that could help to resolve my issue.

library(shiny)

innerUI <- function(id){
  ns <- NS(id)
  actionButton(ns('insertBtn_inner'), 'Insert')
}

inner <- function(input,output,session){
  observeEvent(input$insertBtn_inner, {
    showModal(modalDialog(
      title = 'Debug message',
      paste0('The inner button works'),
      easyClose = TRUE,
      footer = NULL
    ))})
}

outerUI <- function(id){
  ns <- NS(id)
  tagList(
    actionButton(ns('insertBtn_outer'), 'Insert'),
    br(),
    tabsetPanel(id=ns('tabset'), type = 'tabs',
                innerUI('inner1')))
}

outer <- function(input,output,session){
  callModule(inner,'inner1')
  observeEvent(input$insertBtn_outer, {
    showModal(modalDialog(
      title = 'Debug message',
      paste0('The outer button works'),
      easyClose = TRUE,
      footer = NULL))})
}  


ui <- shiny::navbarPage('test_app',
                        shiny::tabPanel('Tab Panel',
                                        outerUI('test')))

server <- function(input, output) {
  callModule(outer,'test')
}

shinyApp(ui = ui, server = server)

Solution

  • Inside a tabsetPanel, you must use one or more tabPanel. In addition you have to call the innerUI with ns, that is innerUI(ns('inner1')).

    outerUI <- function(id){
      ns <- NS(id)
      tagList(
        actionButton(ns('insertBtn_outer'), 'Insert'),
        br(),
        tabsetPanel(tabPanel("XXX", innerUI(ns('inner1'))), 
                    id=ns('tabset'), type = 'tabs')
      )
    }