R Shiny - 修复了闪亮仪表板中的侧边栏和主标题

时间:2016-12-20 11:07:01

标签: css r shiny shinydashboard

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

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

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

它几乎可以工作......侧边栏和主标题是固定的,但是主标题有一个问题,它隐藏了第一个框的标题。我想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 :(得分:3)

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

.content {
  padding-top: 60px;
}

答案 1 :(得分:0)

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

.content {
  margin-top: 50px;
}

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

希望对子孙后代有所帮助