以编程方式更改Shiny navbarPage tabPanel

时间:2017-05-15 17:09:21

标签: r shiny

我有一个Shiny应用程序,在navbarPage中有几个tabPanel。

ui<-shinyUI(
  navbarPage(
  theme = "cyborgBootstrap.css",
  "Shiny panel append",
    tabPanel(
      "First Panel",
      strong("panel 1")
      ),
    tabPanel(
      "Second Panel",
      strong("panel 1")
      ),
    tabPanel(
      "Third Panel",
      strong("panel 1")
      )
    )
  )

当应用加载时,&#34; First Panel&#34;将是活跃的。我希望能够添加一些服务器功能,以便在某些任务完成时切换面板。我怎样才能以编程方式在面板之间切换?

1 个答案:

答案 0 :(得分:0)

library(shiny)
library(purrr)
data(iris)

ui<-
    navbarPage(
        theme = "cyborgBootstrap.css",
        "Shiny panel append",
        tabPanel(
            "First Panel",
            #upon clicking a dataset will loaded and the tab will switch after completition
            actionButton('load_iris', 'Load Iris Dataset'), 
            dataTableOutput('iris')
        ),
        tabPanel(
            "Second Panel",
            strong("panel 2"),
            value = 'secondPanel' ##added value to tell updateNavbarPage where to go.
        ),
        tabPanel(
            "Third Panel",
            strong("panel 3")
        ), id = 'navbar'
    )


server <- function(input, output, session) {
    
    #upon clicking in 'load Iris' the dataset is rendered and after that process is complete the second tab will be selected
    observeEvent(input$load_iris, {
        output$iris <- renderDataTable(withProgress({ #simulate to perform a time consuming action
            map(rep(.1, 10), ~{
            Sys.sleep(.1)
            incProgress(.x, 'performing task')})
            iris},
            min = 0, max = 1,value = .1) )
        #now tell the app to select the secondPanel
        updateNavbarPage(session, 'navbar', selected = 'secondPanel')
    })
    
   
    
}

shinyApp(ui, server)