tabs preload problem with tabsetpanel

Hi All,

Here is the minimal working example which perfectly describes my problem. Just module inside of module which adds tabs to tabsetPanel.
Please tell me why appendTab works only inside of observeEvent . why when I'm trying preload (line 53) one tab when moduleB_server is launched it do nothing.

Thank you,
tj

library(shiny)
library(shinydashboard)


moduleC_ui <- function(id) {
  ns <- NS(id)
  fluidPage(
    fluidRow(
      verbatimTextOutput(ns("saveString"))
    ),
    fluidRow(
      hr(style = "border-top: 1px solid #000000;")
    ),
    fluidRow(
      actionButton(ns("save"), "Save"),    
    )
  )
}


moduleC_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$save,{
      output$saveString <- renderPrint({
        "saving..."
      }) 
    })
  })
}


moduleB_ui<- function(id) {
  ns <- NS(id)
  fluidPage(

    fluidRow(
      actionButton(ns("addTab"), "Add Tab"),

      tabsetPanel(id = ns("moduleB"))

    )
  )
  
}


moduleB_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    ns <- session$ns
    localVariables = reactiveValues(tabCounter = 2, tabsServers=list())  

    appendTab("moduleB", tabPanel("moduleC_tab_1", moduleC_ui(ns("moduleC_tab_1"))), select = TRUE)
    localVariables$tabsServers[["moduleC_tab_1"]] <- moduleC_server("moduleC_tab_1")

    observeEvent(input$addTab, {
        newTabName <- paste0("moduleC_tab_",localVariables$tabCounter)
        localVariables$tabCounter = localVariables$tabCounter+1
        appendTab("moduleB", tabPanel(newTabName, moduleC_ui(ns(newTabName))), select = TRUE)
        localVariables$tabsServers[[newTabName]] <- moduleC_server(newTabName)
    })
    
  })
  
}


moduleA_ui <- function(id) {
  ns <- NS(id)
  tabsetPanel(
    id = ns("tabsetPanelID"),
    tabPanel("moduleA_tab1",
             fluidRow(
               column(width = 12,
                      hr(style = "border-top: 1px solid #000000;"),
                      uiOutput(ns("layout_b"))
               )
             )    
    ),
    tabPanel("moduleA_tab1")
  )
  
  
}


moduleA_server <- function(id) {
  moduleServer(
    id,
    function(input,output,session){
      
      output$layout_b<-renderUI({
        ns <- session$ns
        moduleB_ui(ns("moduleB_tab"))
      })
      
      moduleB_server(id = "moduleB_tab")
      
    }
  )
}


ui <- dashboardPage(
  dashboardHeader(title = "tabs preload example"),
  dashboardSidebar(sidebarMenuOutput("menu")),
  dashboardBody(tabItems(
    tabItem(tabName = "tab_1", moduleA_ui("moduleA"))

  ))
)


server <- function(input, output) {


  observeEvent(input$tabs,{
    if(input$tabs=="tab_1"){
      moduleA_server(id = "moduleA")

    }
  }, ignoreNULL = TRUE, ignoreInit = TRUE)
  
  
  output$menu <- renderMenu({
    sidebarMenu(id = "tabs",
                menuItem(
                  "moduleA",
                  icon = icon("calendar"),
                  tabName = "tab_1"
                )
    )
  })
}


shinyApp(ui, server)

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.