锁定 R shiny dashboard 侧边栏 (shinydashboard)

Locking R shiny dashboard sidebar (shinydashboard)

我在 R 中构建 Shiny Dashboard 时卡住了(使用 shinydashboard 包)。我想锁定侧边栏,使其在我浏览选项卡内容时不会滚动,但我不确定如何实现。

例如,以下代码块将创建一个长滚动仪表板。最好锁定侧边栏,这样您在滚动浏览超长数据时仍然可以看到菜单选项卡 table。

library(ggplot2)  ## for mpg dataset
library(shinydashboard)

## ui
ui <- dashboardPage(
  dashboardHeader(title="MPG Data"), 
  dashboardSidebar(sidebarMenu(menuItem("MPG", tabName="mpg"))), 
  dashboardBody(tabItems(tabItem(tabName="mpg", fluidRow(tableOutput("mpgTable"))))))

## server
server <- function(input, output) {
  output$mpgTable <- renderTable({mpg})
}

## launch dashboard 
shinyApp(ui, server)

**免责声明:我不是 css 任何方面的专家

您可以在 DT 中为实际 table 设置该选项,但如果您希望实际仪表板中的选项卡从侧边栏中自由滚动(假设您没有使用基于您的代码的导航栏) 试一试:

css 看起来像这样:

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

如果您的 'www' 文件夹中有一个 .css 文件,您可以从 ui 中使用许多函数调用它;所以我们假设您的文件名为 "style.css"。

ui 现在看起来像这样:

dashboardPage(
  dashboardHeader(title="MPG Data"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("MPG",tabName="mpg")
    )
  ),
  dashboardBody(
    #here's where you throw the css into the header
    tags$head(
      includeCSS('www/style.css')
      ),
    tabItems(
      tabItem(tabName="mpg",
        fluidRow(tableOutput("mpgTable"))
        )
      )
    )
  )

服务器端没有任何变化,但您可能希望使用 DT 或 table 中的设置选项进行检查,以便更轻松地处理数据。 DT 包参考。

希望对您有所帮助。

@HipHopPhysician 你能post你想运行吗?否则,这是使用 DT 作为解决方法的最简单方法……有很多选项可供设置;所以我将给出默认值:

library(ggplot2)
library(DT)
library(shinydashboard)

ui <- 
  dashboardPage(
    dashboardHeader(title="MPG Data"),
    dashboardSidebar(
      sidebarMenu(
        menuItem("MPG",tabName="mpg")
        )
      ),
    dashboardBody(
      #here's where you throw the css into the header
      tags$head(
        includeCSS('www/style.css')
      ),
      tabItems(
        tabItem(tabName="mpg",
                dataTableOutput("mpgTable"))
      )
    )
  )

server <- function(input, output) {
  output$mpgTable <- renderDataTable({ mpg })
}

您应该只在 ui 边栏添加样式 = "position:fixed; overflow: visible"。

library(ggplot2)  ## for mpg dataset
library(shinydashboard)

## ui
ui <- dashboardPage(
  dashboardHeader(title="MPG Data"), 
  dashboardSidebar(
    sidebarMenu(style = "position: fixed; overflow: visible;",
      menuItem("MPG", tabName="mpg"))), 
  dashboardBody(
    tabItems(
      tabItem(tabName="mpg", 
       fluidRow(tableOutput("mpgTable"))))))

## server
server <- function(input, output) {
  output$mpgTable <- renderTable({mpg})
}

## launch dashboard 
shinyApp(ui, server)