'Change Shiny navbarPage tabPanel programmatically
I have a Shiny app with several tabPanels within a navbarPage.
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")
)
)
)
When the app loads, "First Panel" will be active. I would like to be able to add some server functions that toggle the panels when certain tasks complete. How could I toggle between panels programmatically?
Solution 1:[1]
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)
Solution 2:[2]
@jpdugo 's answer above is correct.
However, if you want to change tab on page load (for example after parsing a GET request), be sure to add ignoreInit = TRUE, in any other observed event that changes the tab.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jpdugo17 |
| Solution 2 | Evangelos Karatzas |
