Using {shinydashboard}
, we can access the state of the sidebar (collapsed or not) with input$sidebarCollapsed
:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
textOutput("res")
)
)
server <- function(input, output, session) {
output$res <- renderText({
if (input$sidebarCollapsed) {
"Sidebar is collapsed"
} else {
"Sidebar is expanded"
}
})
}
shinyApp(ui, server)
However with {bs4dash}
, this does not work :
library(shiny)
library(bs4Dash)
ui <- bs4Dash::dashboardPage(
bs4Dash::dashboardHeader(),
bs4Dash::dashboardSidebar(),
bs4Dash::dashboardBody(
shiny::textOutput("res")
)
)
server <- function(input, output, session) {
output$res <- shiny::renderText({
if (input$sidebarCollapsed) {
"Sidebar is collapsed"
} else {
"Sidebar is expanded"
}
})
}
shinyApp(ui, server)
The app returns Error: argument is of length zero
.
What am I missing ? Is there any alternative ? Accessing the sidebar state is useful for me, so that I can hide some widgets when the sidebar is minified (in order to only keep the sidebar icons visible and not widgets such as selectInputs which appear messy when minified).
I have also tried changing to sidebarMinified
or some other hopeless attempts...
Note that I can also use shiny::conditionalPanel()
with the condition written as 'input.sidebarCollapsed'
but the result is the same.
Thanks a lot for your help !
I was just wondering the same thing and, upon searching the bs4Dash how-to-start guide, found the answer.
id is used by the updateSidebar() function to programmatically toggle the sidebar on the server. input$ indicates the state of the sidebar: TRUE means open and FALSE means collapsed/minified.
So, if you set the id of your sidebar to be 'my_sidebar', then you can access the state of sidebar with input$my_sidebar
.