R Shiny - Fixed sidebar and main header in a Shiny dashboard

6.5k views Asked by At

I have a simplified shiny dashboard (see the code below). I want to fix the sidebar and the main header. So, with the help of other posts here, I write a CSS file to address the problem.

.sidebar {
  color: #FFF;
  position: fixed;
  width: 220px;
  white-space: nowrap;
  overflow: visible;
}

.main-header {
  position: fixed;
  width:100%;
}

It almost works... The sidebar and the main header are fixed but there is a problem with the main header who hide the header of the first box. I guess that there is a few keyword in CSS to solve the problem but I didn't find them (newbie in CSS...).

The Shiny dashboard code is the following.

library(ggplot2)
library(shiny)
library(shinydashboard)

CountPlotFunction <- function(MyData)
{
  MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
    geom_bar(stat = "count", aes(fill = MyData)) +
    geom_text(stat = "count", aes(label = ..count..)) +
    scale_x_discrete(drop = FALSE) +
    scale_fill_discrete(drop = FALSE)
  return(MyPlot)
}

# The data
var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)

# The Shiny app 
Interface <- 
{
  dashboardPage(
    title = "Dashboard", skin = "yellow",
    dashboardHeader(title = "Dashboard"),

  dashboardSidebar(
    sidebarMenu(
      sidebarSearchForm(textId = "searchText", buttonId = "searchButton", label = "Search..."),
      menuItem(text = "Analysis", tabName = "Analysis", icon = icon("dashboard")))
  ),

  dashboardBody(
    tags$head(includeCSS('www/style.css')),

      tabItem(tabName = "Analysis",
              fluidPage(box(title = "Choose the questions", status = "warning", solidHeader = TRUE, collapsible = FALSE, width = 12,
                            checkboxGroupInput(inputId = "ChooseQuestion", label = NULL, choices = colnames(df),
                                        selected = colnames(df)[1])),
                        box(title = "Results", status = "warning", solidHeader = TRUE, collapsible = FALSE, width = 12,
                            uiOutput("ui_plot")))
      )
    )  
  )
}

Serveur <- function(input, output)
{
  # gen plot containers
  output$ui_plot <- renderUI({ 
    out <- list()
    if (length(input$ChooseQuestion)==0){return(NULL)}
    for (i in 1:length(input$ChooseQuestion)){
      out[[i]] <-  plotOutput(outputId = paste0("plot",i))
    }  
    return(out) 
  })

  # render plots
  observe({  
    for (i in 1:length(input$ChooseQuestion)){  
      local({
        ii <- i 
        output[[paste0('plot',ii)]] <- renderPlot({ 
          if ( length(input$ChooseQuestion) > ii-1 ){  
            return(CountPlotFunction(MyData = df[input$ChooseQuestion[[ii]]]))
          } 
          NULL
        })
      })
    }                                  

  })

}

shinyApp(ui = Interface, server = Serveur)
2

There are 2 answers

0
Xiongbing Jin On BEST ANSWER

Add the following to your CSS file and it should work.

.content {
  padding-top: 60px;
}
1
GregF On

I don't know if this is because things have changed since a year ago, but I had much better luck with:

.content {
  margin-top: 50px;
}

Having padding-top like the Xiongbing's answer left a weird blank space at the bottom (also, 50px not 60px).

Hope that's helpful for posterity