I hope the title is self explanatory. I saw similar questions answered with JavaScript, but it would be great if it could be done with R. Here is a minimal example of what i tried and did not work (also tried updateNavbarPage()). I am trying to set a link in tabpanel B to go from tabpanel B to tabpanel D.
library(shiny)
# Define UI
ui <- fluidPage(
navbarPage(
"A",
tabPanel("A1",
sidebarPanel(),
mainPanel(
tabsetPanel(type = "tabs",
id = "panels",
# Tabsets in mainPanel----
tabPanel("B",
hr(),
HTML("This is panel B <br>"),
hr(),
actionLink("link_to_D", "Go to D")
), # Tabpanel
tabPanel("C",
hr(),
HTML("This is panel C"))
)# tabsetPanel
)# mainPanel
),#tabPanel
navbarMenu("About",
tabPanel("D",
hr(),
HTML("This is panel D")),
tabPanel("E" ,
hr(),
HTML("This is panel E"))
) #NavbarMenu
) # NavbarPage
) # fluidPage
# Define server
server <- function(input, output,session) {
# Link to D
observeEvent(input$link_to_D, {
updateTabsetPanel(session, "C", "D")
})
} # server
# Run the application
shinyApp(ui = ui, server = server)
CodePudding user response:
You were close. The issue is only that in SHiny you do not update the tab panel directly. Instead you update the "mother" element which is either a tabSetPanel
or - in your case - the navbarPage
. So you need to give the navbarPage
an id and call updateNavbarPage
.
Example:
library(shiny)
# Define UI
ui <- fluidPage(
navbarPage(
"A-Title",
tabPanel("A1",
sidebarPanel(),
mainPanel(
tabsetPanel(type = "tabs",
id = "panels",
# Tabsets in mainPanel----
tabPanel("B",
hr(),
actionLink("link_to_D", "Go to D")
), # Tabpanel
tabPanel("C")
)# tabsetPanel
)# mainPanel
),#tabPanel
navbarMenu("About",
tabPanel("D", "I am D"),
tabPanel("E", "I am E")
), #NavbarMenu
id="MainNavBar" # Step 1: Give the navbarPage an id
) # NavbarPage
) # fluidPage
# Define server
server <- function(input, output,session) {
# Link to D
observeEvent(input$link_to_D, {
#--updateTabsetPanel(session, "C", "D")
# update selected tab to "D"
updateNavbarPage(inputId="MainNavBar", selected = "D")
})
} # server
# Run the application
shinyApp(ui = ui, server = server)