shiny dashboard tab input

...I'm trying to add an action button to swap tabs in shiny but it doesn't seem to work :frowning:

library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)

# Application title
header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")

# Sidebar with a slider input for number of bins 
sidebar <- dashboardSidebar(
  sidebarMenu(id = "tabs",
              menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")),
              menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard"))
  ))
              
              
              
   body <- dashboardBody(
          tabItems(
            tabItem(tabName = "Marketing",
                    h3("Annual Marketing Costs")                    , 
                    actionButton("switchtab", "Look at Customer Retention")),
                    
            tabItem(tabName = "CustomerRetention",
                    h3("Annual Costs Spent on Existing Customers"))))
   
   ui <- dashboardPage(skin="blue",
                       header,
                       sidebar,
                       body
   )
   
   server <- function(input, output) {
   
   observeEvent(input$switchtab, {
     newtab <- switch(input$tabs, 
                      "CustomerRetention" = "Marketing",
                      "Marketing" = "CustomerRetention")

     
     
     updateTabItems(session, "tabs", newtab)
   })
   }

                                    shinyApp(ui = ui, server = server)

Hi, you could directly specify the menuItem text in updateTabItems. Try this:

library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)

# Application title
header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")

# Sidebar with a slider input for number of bins 
sidebar <- dashboardSidebar(
  sidebarMenu(id = "tabs",
              menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")),
              menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard"))
  ))



body <- dashboardBody(
  tabItems(
    tabItem(tabName = "Marketing",
            h3("Annual Marketing Costs")                    , 
            actionButton("switchtab", "Look at Customer Retention")),
    
    tabItem(tabName = "CustomerRetention",
            h3("Annual Costs Spent on Existing Customers"))))

ui <- dashboardPage(skin="blue",
                    header,
                    sidebar,
                    body
)

server <- function(input, output, session) {
  
  observeEvent(input$switchtab, {
    updateTabItems(session, "tabs", "CustomerRetention")
  })
}

shinyApp(ui = ui, server = server)
2 Likes

I've tried importing your code into my script but it doesn't change it so I must have done something wrong in the rest of the program which I can't see and it's long

I've imported it in here but not sure what I've done:


library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)

# Application title
header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")

# Sidebar with a slider input for number of bins 
sidebar <- dashboardSidebar(
  sidebarMenu(id = "tabs",
              menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")),
              menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard"))
  ))
              
              
              
   body <- dashboardBody(
          tabItems(
            tabItem(tabName = "Marketing",
                    h3("Annual Marketing Costs")                    , 
                    actionButton("switchtab", "Look at Customer Retention")),
                    
            tabItem(tabName = "CustomerRetention",
                    h3("Annual Costs Spent on Existing Customers"))))
   
   ui <- dashboardPage(skin="blue",
                       header,
                       sidebar,
                       body
   )
   
   server <- function(input, output) {
   
     server <- function(input, output, session) {
       
       observeEvent(input$switchtab, {
         updateTabItems(session, "tabs", "CustomerRetention")
       })
     }
   }

   shinyApp(ui = ui, server = server)

you have duplicated the server function. Try after removing the outer server function which is not required.

1 Like

sorry silly error, i haven't done that in my original script, would you mind if I post it for you to have a quick look at?

1 Like

I've done it!!! :sunny:

I added my script to your working code one bit at a time and the only thing that was different was that the observe event for the action button was first rather than in the middle of the server function.

Thank you for your help :smile:

1 Like

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

1 Like