Home > OS >  Is there a way to collapse the sidebar by default
Is there a way to collapse the sidebar by default

Time:07-13

Is there a way to collapse the sidebar by default. Right now, it is showing by default once the application is open. Can we make it collapsed by default

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  navbarPage("",
             tabPanel("tab",
                      div( id ="Sidebar",sidebarPanel(
                      )),
                      
                      
                      mainPanel(actionButton("toggleSidebar", "Toggle sidebar")
                      )
             )
  )
)

server <-function(input, output, session) {
  observeEvent(input$toggleSidebar, {
    shinyjs::toggle(id = "Sidebar")
  })
}

shinyApp(ui, server)

CodePudding user response:

Here is a UI based solution which avoids flashing the sidebarPanel on startup:

library(shiny)
library(shinyjs)

ui <- fluidPage(useShinyjs(),
                navbarPage("",
                           tabPanel(
                             "tab",
                             div(id = "sidebarWrapper", sidebarPanel(), style = "display: none;"),
                             mainPanel(actionButton("toggleSidebar", "Toggle sidebar"))
                           )))

server <- function(input, output, session) {
  observeEvent(input$toggleSidebar, {
    shinyjs::toggle(id = "sidebarWrapper")
  })
}

shinyApp(ui, server)

PS: the same can be achived by using shinyjs::hidden(div(<...>)).

CodePudding user response:

You can use the ignoreNULL argument at FALSE to trigger at initialization:

  observeEvent(input$toggleSidebar, {
    shinyjs::toggle(id = "Sidebar")
  }, ignoreNULL = FALSE)

CodePudding user response:

With the Github version of shinyGizmo (which should be on CRAN soon) you can toggle the sidebar with amazing effects:

# remotes::install_github("r-world-devs/shinyGizmo")

library(shiny)
library(shinyGizmo)

ui <- fluidPage(
  navbarPage(
    "",
    tabPanel(
      "tab",
      conditionalJS(
        div(
          id = "Sidebar",
          sidebarPanel(
            sliderInput(
              "obs", "Number of observations:",
              min = 0, max = 1000, value = 500
            )
          )
        ),
        condition = "input.toggleSidebar % 2 === 1",
        jsCalls$animateVisibility("jello", "tada", duration = 1500)
      ),
      
      mainPanel(
        actionButton("toggleSidebar", "Toggle sidebar")
      )
    )
  )
)

server <-function(input, output, session) {
  
}

shinyApp(ui, server)

enter image description here

  • Related