Update Controlbar in bs4dash version 2.00 Shiny

673 Views Asked by At

I have moved over to the new bs4dash and I am having some issues with updating the control bar. For each different tab on my sidebar i would like a corresponding control bar. For example, if the sidebar tab is "Home", I would like to the controlbar to consist of multiple selectizeInputs. If however, the sidebar tab is "News", I would like the Control Bar to have different textOutputs.

Here is some of the code I am using

##UI
  controlbar = dashboardControlbar(
    id = "controlbar",
    collapsed = T
  
    )

##Server 

observeEvent(input$current_tab,{
if(input$current_tab == "home"){
updateControlbar(id = "controlbar", session = session,
selectizeInput("one", "one", choices = c(1,2,3)
), 
selectizeIntput("two", "two", choices = c(1,2,3)
} else if(input$current_tab == "News"){
updateControlbar(id = "controlbar", session = session,
textInput("news1"),
textInput("news2")
}
})

I have also tried many other combos but nothing seems to work. Thank you for your help

1

There are 1 best solutions below

0
On

You can combine conditional panels with reactive functions. This code snippet show a very trivial case.

library(shiny)
library(bs4Dash)

shinyApp(
    ui = dashboardPage(
        header = dashboardHeader(),
        sidebar = dashboardSidebar(uiOutput("sidebar")),
        body = dashboardBody(),
        controlbar = dashboardControlbar(uiOutput("controlbar"))
    ),
    server = function(input, output, session) {
        output$sidebar <- renderMenu({
            sidebarMenu(id = "main_menu",
                menuItem(text = "First page", tabName = "tab1"),
                menuItem(text = "Second page", tabName = "tab2")
            )
        })
        output$show_tab1 <- reactive({
            !is.null(input$main_menu) && input$main_menu == "tab1"
        })
        output$show_tab2 <- reactive({
            !is.null(input$main_menu) && input$main_menu == "tab2"
        })
        outputOptions(output, "show_tab1", suspendWhenHidden = FALSE)
        outputOptions(output, "show_tab2", suspendWhenHidden = FALSE)
        output$controlbar <- renderUI({
            div(
                conditionalPanel(
                    condition = "output.show_tab1",
                    p("Widgets for the first page")
                ),
                conditionalPanel(
                    condition = "output.show_tab2",
                    p("Widgets for the second page")
                )
            )
        })
    }
)