我需要用户单击一个按钮,然后转到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)

最佳答案

您可以使用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 Second 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)

关于r - 如何使用 Shiny 的按钮在NavBar选项卡之间切换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43552906/

10-13 02:06