如何使用Button R Shiny在NavBar选项卡之间切换

时间:2017-04-21 22:26:58

标签: r shiny

我需要用户点击按钮,然后转到tabPanel的其他navbarPage。这是一个可以实现的简单应用程序。我怎样才能做到这一点?

ui <- shinyUI(
 navbarPage('Test App',
  tabPanel('Page 1',
   p('This is the first tab'), br(),
   actionButton('jumpToP2', 'Jump to Second Tab')
  ),

  tabPanel('Page 2',
   p('This is the second tab'),
   actionButton('jumpToP1', 'Jump to First Tab')
  )
 )
)

server <- shinyServer(function(input, output){
  observeEvent(input$jumpToP2,{
    ## Switch active tab to 'Page 2'
  })

  observeEvent(input$jumpToP1,{
    ## Switch active tab to 'Page 1'
  })
})

shinyApp(ui, server)

2 个答案:

答案 0 :(得分:11)

您可以使用updateTabsetPanel在不同的tabPanel之间切换。请参阅https://shiny.rstudio.com/reference/shiny/latest/updateTabsetPanel.html处的文档和示例代码。以下代码应满足您的要求。

ui <- navbarPage('Test App',id = "inTabset",
                 tabPanel(title = "Panel 1", value = "panel1", 
                          actionButton('jumpToP2', 'Jump to Secon Tab')),
                 tabPanel(title = "Panel 2", value = "panel2", 
                          actionButton('jumpToP1', 'Jump to First Tab'))
        )

server <- function(input, output, session) {
    observeEvent(input$jumpToP2, {
        updateTabsetPanel(session, "inTabset",
                          selected = "panel2")
    })

    observeEvent(input$jumpToP1, {
        updateTabsetPanel(session, "inTabset",
                          selected = "panel1")
    })

}

shinyApp(ui, server)

答案 1 :(得分:0)

我不太确定这真的是你设计你的最佳方式...你知道tabPanel()的行为方式与actionButton()的行为方式大致相同吗?案件?

ui <- navbarPage('Test App',id = "inTabset",
             tabPanel(title = "Panel 1", uiOutput("panel1")),                       
             tabPanel(title = "Panel 2", uiOutput("panel2")) 
                      )
server <- function(input, output, session) {
    output$panel1 <- renderUI({
    #your UI data
    })
    output$panel2 <- renderUI({
    #your UI data
    })
}
shinyApp(ui, server)