简体   繁体   English

Shiny App中的可折叠盒子

[英]Collapsible box in Shiny App

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  includeCSS(path = "AdminLTE.css"), #added 
  includeCSS(path = "shinydashboard.css"), #added

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         box(plotOutput("distPlot"), solidHeader = T, collapsible = T, title = "collapsible box not collapsing", status = "primary")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

This result is 结果是

在此输入图像描述

In the above image the collpasible box is not getting collapsed when clicked on mininize button. 在上图中,单击mininize按钮时,可碰撞框不会折叠。

I have added the addtional AdminLTE.css and shinydashboard.css file in working directory, but still problem persists. 我在工作目录中添加了AdminLTE.cssshinydashboard.css文件,但问题仍然存在。

To use a collapsible box within shiny only. 仅使用闪亮的可折叠盒子。 We need to add the required javascript. 我们需要添加所需的javascript。 Just after adding css we add this file as well. 在添加css之后,我们也添加了这个文件。

  includeCSS(path = "AdminLTE.css"), #added 
  includeCSS(path = "shinydashboard.css"), #added

  #add this file and collapsible nature should work.
  includeScript(path = "app.js"), # 

If you don't have a restriction to use shinydashboard , just create a dash board page without the header and the sidebar. 如果您没有使用shinydashboard的限制,只需创建一个没有标题和侧边栏的仪表板页面。 It will enable all the features of shinydashboard and it will looks like a basic shiny app. 它将启用shinydashboard所有功能,它看起来像一个基本的闪亮应用程序。 In the code below the box collapse/uncollapse when you click on the minimize/maximize button. 在单击下面的代码中,单击最小化/最大化按钮时折叠/解除折叠。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(disable = TRUE),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
  # Application title
  titlePanel("Old Faithful Geyser Data"),
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
      sidebarPanel(
         sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
      ),
      # Show a plot of the generated distribution
      mainPanel(
         box(plotOutput("distPlot"), solidHeader = T, collapsible = T, 
             title = "collapsible box not collapsing", status = "primary")
      )
    ) 
  )
)
# Define server logic required to draw a histogram
server <- function(input, output) {
   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM