Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reduce the width of the sidebar panel in an R Shiny web app?

Tags:

r

shiny

library(shiny)

ui <- fluidPage(

    titlePanel("Hello"),
    sidebarLayout(
        sidebarPanel("Hello SideBar"),
        mainPanel("Hello MainPanel")
    )
)

server <- function(input, output) {

    
}

shinyApp(ui = ui, server = server)

Just by eyeballing I can tell right now my sidebar panel takes up about 33% of the width of the screen. Any idea how I can reduce the width of the sidebar so that the main Panel is larger?

like image 542
SampyKIshan Avatar asked Oct 14 '25 07:10

SampyKIshan


2 Answers

sidebarPanel has a width argument

width: The width of the sidebar. For fluid layouts this is out of 12 total units; for fixed layouts it is out of whatever the width of the sidebar's parent column is.

The default width is 4, which confirms your eyeballing estimate that 4/12 is one third. So to make it e.g 1/2 the current width you would do:

ui <- fluidPage(

    titlePanel("Hello"),
    sidebarLayout(
        sidebarPanel("Hello SideBar", width=2),
        mainPanel("Hello MainPanel")
    )
)
like image 116
dww Avatar answered Oct 20 '25 17:10

dww


Setting the width argument to a value between 1-12 causes a bug for me where the sidebar disappears, however, this can be fixed by defining the width in pixels (e.g. "250px" is the default value).

The code below sets width = "150px to reduce the sidebar width. Set width to a value greater than 250px to increase the sidebar width.

ui <- function() {
  bslib::page_sidebar(
    title = "Testing Sidebar",
    sidebar = bslib::sidebar(
      title = "Sidebar", width = "150px",
      shiny::HTML("Text Here")
    ),
    shiny::fluidPage(
      shiny::fluidRow(
        shiny::HTML("Hello World!")
      )
    )
  )
}

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

shiny::shinyApp(ui, server)

ShinyApp Output from the given code

like image 30
ben Avatar answered Oct 20 '25 16:10

ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!