Shiny: Independently navigate two tabsetPanels

送分小仙女□ 提交于 2019-12-08 03:43:46

问题


Is it possible to navigate two menus independently in Shiny?

I would like to have two tabsetPanels (see the figure below), but if I change between tabs in the upper menu, I want the selected panel in the lower menu to persist.

One way to navigate multiple tabsetPanels is by simply running another Shiny app inside my main app. However, this is not the desired result, because switching tabs in the upper menu will now also affect which tab is open in the lower menu.

Example of what I'm using now:

library(shiny)

ui <- fluidPage(
  tabsetPanel(
    tabPanel("Normal", shinyAppDir("Distributions/Normal"))),
    tabPanel("Binomial", shinyAppDir("Distributions/Binomial"))
    # etc.
  )
)

Where each called app has its own tabsetPanel:

ui <- fluidPage(
  tabsetPanel(
              tabPanel("Theoretical distribution", plotOutput("plot1")),
              tabPanel("Draw Samples", plotOutput("plot2")),
              tabPanel("Explanation", verbatimTextOutput("explanation"))
  )
)

The result:


回答1:


You can use id, updateTabsetPanel and reactiveValues to achieve what you need.

Use reactiveValue to save what is your sub tabPanel number is chosen, then use updateTabsetPanel to change the default selected sub tabPanel when you switch the main tabPanel.

Try the following code:

You can use id, updateTabsetPanel and reactiveValues to achieve what you need.

Use reactiveValue to save what is your sub tabPanel number is chosen, then use updateTabsetPanel to change the default selected sub tabPanel when you switch the main tabPanel.

Try the following code:

shiny::runApp(list(
    ui = bootstrapPage(

      tabsetPanel(id = "mainTab",
                  tabPanel("Tab1", tabsetPanel(id = "subTab1", 
                                               tabPanel("subTab11",value=1),
                                               tabPanel("subTab12",value=2),
                                               tabPanel("subTab13",value=3),
                                               selected = 1
                  )),
                  tabPanel("Tab2", tabsetPanel(id = "subTab2", 
                                               tabPanel("subTab21",value=1),
                                               tabPanel("subTab22",value=2),
                                               tabPanel("subTab23",value=3),
                                               selected = 1
                  ))
      )

    ),
    server = function(input, output,session) {

      current_subtab<-reactiveValues(selected=1)


      # when either sub tabPanel change, save the current selected sub tabPanel to reactiveValues
      observeEvent(c(input$subTab1,input$subTab2),{
        if(input$mainTab=="Tab1"){
          current_subtab$selected<-input$subTab1
        }else if(input$mainTab=="Tab2"){
          current_subtab$selected<-input$subTab2
        }
      })

      # when main tabPanel changed, update the default select subPanel value
      observeEvent(input$mainTab,{
        if(input$mainTab=="Tab1"){
          updateTabsetPanel(session,"subTab1",selected = current_subtab$selected)
        }else if(input$mainTab=="Tab2"){
          updateTabsetPanel(session,"subTab2",selected = current_subtab$selected)
        }
      })
    }
  ))


来源:https://stackoverflow.com/questions/52547320/shiny-independently-navigate-two-tabsetpanels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!