Home > Back-end >  Move from one tab to another using actionButton when then name of the tab is dynamic
Move from one tab to another using actionButton when then name of the tab is dynamic

Time:09-06

How can I move from one tab to another using actionButton() when then name of the tab is dynamic in a shiny app?

library(shiny)
ui <- fluidPage(
  tabsetPanel(
    id="inTabset",
    tabPanel("Tab 1",textInput("name","Name"),actionButton("switch_tab", "Go to the third tab")
    ),
    tabPanel("Tab 2", "there!"),
    tabPanel(textOutput("TAB3")))
  
)

server <- function(input, output, session) {
  
  observeEvent(input$switch_tab, {
    updateTabsetPanel(session, "inTabset",selected = "Tab 3")
  })
  output$TAB3<-renderText({
    if(input$name==""){
      "Tab 3"
    }
    else{
        paste(input$name)
    }
      
    
    
  })
}
shinyApp(ui = ui, server = server)

CodePudding user response:

Just use tabPanel's value parameter:

From ?tabPanel:

value - The value that should be sent when tabsetPanel reports that this tab is selected. If omitted and tabsetPanel has an id, then the title will be used.

library(shiny)
ui <- fluidPage(tabsetPanel(
  id = "inTabset",
  tabPanel(
    "Tab 1",
    textInput("name", "Name"),
    actionButton("switch_tab", "Go to the third tab")
  ),
  tabPanel("Tab 2", "there!"),
  tabPanel(textOutput("TAB3"), value = "TAB3")
))

server <- function(input, output, session) {
  observeEvent(input$switch_tab, {
    updateTabsetPanel(session, "inTabset", selected = "TAB3")
  })
  
  output$TAB3 <- renderText({
    if (input$name == "") {
      "Tab 3"
    } else{
      paste(input$name)
    }
  })
}
shinyApp(ui = ui, server = server)
  • Related