Adding interactive tabs


#1

I would like to setup a R Shiny web page that upon selecting a Disease and a Nation, and then clicking the addTab button, a new tab is created that contains more selection options. I have code (illustrated below) that mostly works. However when I create a new tab, the input values for the other tabs disappear. Any suggestions?

ui.R
# Global variables
nTabs <<- 1

shinyUI(
 fluidPage(
   
   fluidRow(
     column(3,uiOutput("selectDisease")),
     column(3,uiOutput("selectNation")),
     column(3, actionButton("addTab", label = "Add Tab"),
               actionButton("removeTab", label = "Remove Active Tab"))
   ),

   mainPanel(
      # …….
    )
  )
)

server.R 
  observeEvent (input$addTab, {
    thisTab <- paste(input$diseaseSelected, "Tab", nTabs)
    
    appendTab (inputId = "tabs", tabPanel(thisTab, {
        sidebarLayout(
          fluid = TRUE,
          sidebarPanel (
            width = 4,
            h5 (paste("Disease:", input$diseaseSelected)),
            h5 (paste("Nation:", input$nationSelected)),
            
            renderUI (sideLayout(nTabs))

          ),
          mainPanel(
            # ....
          )
        )

    }
    ))
    updateTabsetPanel(session = session, inputId = "tabs", selected = thisTab )
    nTabs <<- nTabs + 1
  })


 sideLayout <- function (tabNumber) {

# ……….

    # seasonSelected
    choices <- currentTabData$years 
    seasonList <- selectInput( paste0("seasonSelected", tabNumber), label = "Select a season", 
                  choices = choices, selected = max(choices))

    #periodSelected
    choices <- sort(currentTabData$weeks[[i]], decreasing = TRUE)
    periodList <- selectInput(paste0("periodSelected", tabNumber), label = "Select a week", 
                  choices = choices, selected = max(choices))

    list(seasonList, periodList)
  }

#2

Figured it out. Operator error. The nTabs <<- nTabs + 1 runs before the sideLayout(nTags) function. This made me think that the input values were disappearing. Best to use input$addTab[[1]] instead of nTabs.