Search code examples
rshinyshinydashboard

Update tabPanel content with selection in Sidebar control


I am making a very simple Shiny app, where I want to update tabPanel content with whatever is selected in sidebarMenu, no matter which tab is selected. Following is my sample code. Please let me know what I am doing wrong.

library(shinydashboard)

siderbar <- dashboardSidebar(
  sidebarMenu(
    # Add buttons to choose the way you want to select your data
    radioButtons("select_by", "Select by:",
                 c("Food Type" = "Food",
                   "Gym Type" = "Gym",
                   "TV show" = "TV"))
  )   
)

body <- dashboardBody(
  fluidRow(
    tabBox(
      side = "right",
      selected = "Tab3",
      tabPanel("Tab1", "Tab content 1"),
      tabPanel("Tab2", "Tab content 2"),
      tabPanel("Tab3", "Tab content 3")
    )
  ),
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "tabBoxes"),
    siderbar,
    body
  ),
  server = function(input, output) {
    
    getReaction <- reactive(input$select_by)
    
    # The currently selected tab from the first box
    output$tabset1Selected <- renderText({
      getReaction
    })
  }
)

Solution

  • You didn't define any textOutput. Please check the following:

    library(shiny)
    library(shinydashboard)
    
    siderbar <- dashboardSidebar(
      sidebarMenu(
        # Add buttons to choose the way you want to select your data
        radioButtons("select_by", "Select by:",
                     c("Food Type" = "Food",
                       "Gym Type" = "Gym",
                       "TV show" = "TV"))
      )   
    )
    
    body <- dashboardBody(
      fluidRow(
        tabBox(
          side = "right",
          selected = "Tab3",
          tabPanel("Tab1", "Tab content 1", textOutput("tabset1Selected")),
          tabPanel("Tab2", "Tab content 2", textOutput("tabset2Selected")),
          tabPanel("Tab3", "Tab content 3", textOutput("tabset3Selected"))
        )
      ),
    )
    
    shinyApp(
      ui = dashboardPage(
        dashboardHeader(title = "tabBoxes"),
        siderbar,
        body
      ),
      server = function(input, output) {
        # The currently selected tab from the first box
        output$tabset1Selected <- output$tabset2Selected <- output$tabset3Selected <- renderText({
          input$select_by
        })
      }
    )