首页 文章

R Shiny - 固定侧边栏和闪亮仪表板中的主 Headers

提问于
浏览
3

我有一个简化的闪亮仪表板(见下面的代码) . 我想修复侧边栏和主 Headers . 所以,在这里的其他帖子的帮助下,我写了一个CSS文件来解决这个问题 .

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

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

它几乎可以工作......侧边栏和主 Headers 是固定的,但主 Headers 有一个问题,隐藏第一个框的 Headers . 我想在CSS中有一些关键字可以解决问题,但我没有找到它们(CSS中的新手......) .

Shiny仪表板代码如下 .

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 回答

  • 0

    将以下内容添加到CSS文件中,它应该可以正常工作 .

    .content {
      padding-top: 60px;
    }
    
  • 3

    我不知道这是因为一年前事情发生了变化,但我运气好多了:

    .content {
      margin-top: 50px;
    }
    

    像熊兵的答案那样 padding-top 在底部留下了一个奇怪的空白区域(也就是50px而不是60px) .

    希望这对后代有所帮助

相关问题